#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

package Vhffs::Robots::Mercurial;

use Vhffs::Services::Mercurial;
use Vhffs::Constants;
use Vhffs::Functions;

sub create_repo
{
	my $mercurial = shift;
	return -1 unless defined $mercurial;
	return -1 if $mercurial->get_status != Vhffs::Constants::WAITING_FOR_CREATION;

	my $dir = $mercurial->get_dir;

	if( -e $dir ) {
		$mercurial->set_status( Vhffs::Constants::CREATING_ERROR );
		$mercurial->commit();
		$mercurial->add_history('Error, directory of this mercurial repository already exists! Administrators must fix the problem.');
		return -1;
	}

	Vhffs::Functions::create_dir( $dir );
	Vhffs::Functions::chmod_recur( $dir , 0664 , 02775 );
	Vhffs::Functions::change_owner_recur( $dir , $mercurial->get_owner_uid , $mercurial->get_owner_gid );
	$mercurial->add_history('Ok, robots find the empty directory and will create mercurial repository');

	system('cd '.$dir.' && hg init > /dev/null');

	unless( -d $dir.'/.hg' ) {
		$mercurial->set_status( Vhffs::Constants::CREATING_ERROR );
		$mercurial->commit();
		$mercurial->add_history('Error, nothing was created after calling the mercurial binary, something is wrong.');
		return -1;
	}

	Vhffs::Functions::chmod_recur( $dir , 0664 , 02775 );
	Vhffs::Functions::change_owner_recur( $dir , $mercurial->get_owner_uid , $mercurial->get_owner_gid );

	change_conf( $mercurial );

	$mercurial->add_history('The Robots created the mercurial repository');
	$mercurial->set_status( Vhffs::Constants::ACTIVATED );
	$mercurial->commit;

	return 0;
}

sub change_conf {
	my $mercurial = shift;
	return -1 unless defined $mercurial;

	my $dir = $mercurial->get_dir;
	my $mail_from = $mercurial->get_main->get_config->get_service('mercurial')->{notify_from};
	my $mladdress = $mercurial->{ml_name};

	open( my $rcfileout , "> ", $dir . '/.hg/hgrc' );

	unless (defined $rcfileout) {
		$mercurial->add_history('An error occured while opening the repository configuration file.');
		return -1;
	}

	my $description = $mercurial->get_description;
	# multi line values should be indented
	$description =~ s/(\r)?\n/\n  /g;

	print $rcfileout "[web]\n";
	print $rcfileout "description =\n  " . $description . "\n";
	my $mg = init Vhffs::Services::MailGroup( $mercurial->get_main , $mercurial->get_group );
	if( defined $mg )
	{
		print $rcfileout "contact = " . $mercurial->get_group->get_groupname . '@' . $mg->{config}->{domain} . "\n";
	}
	print $rcfileout "\n";

	if( $mladdress !~ /^\s*$/ ) {
		# Create the rc file
		open( my $rcfilein , "< ", "%VHFFS_BOTS_DIR%/misc/mercurial_notify.rc" );

		unless (defined $rcfilein) {
			$mercurial->add_history("Can't read the configuration template file. This is wrong, very wrong.");
			return -1;
		}
		
		while( my $line = <$rcfilein> )
		{
			$line =~ s/MY_MLADDRESS/$mladdress/g;
			$line =~ s/MY_FROMADDRESS/$mail_from/g;
			
			print $rcfileout $line;
		}
		
		close( $rcfilein );
	}

	close( $rcfileout );

	return 0;
}

sub delete_repo
{
	use File::Path;
	use File::Basename;
	my $mercurial = shift;
	return -1 unless defined $mercurial;

	# Remove Mercurial dir and group dir if empty
	my $dir = $mercurial->get_dir;
	File::Path::rmtree($dir) if(-d $dir);
	my $groupdir = dirname($dir);
	rmdir($groupdir);

	return 0;
}


1;
