#!%PERL%
# Copyright (c) vhffs project and its contributors
# Copyright (c) 2007 Julien Danjou <julien@danjou.info>
# 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::Git;

use Vhffs::Services::Git;
use Vhffs::Constants;
use Vhffs::Functions;

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

	my $dir = $git->get_dir;

	if( -e $dir ) {
		$git->set_status( Vhffs::Constants::CREATING_ERROR );
		$git->commit();
		$git->add_history('Error, directory of this git 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 , $git->get_owner_uid , $git->get_owner_gid );
	$git->add_history('Ok, robots find the empty directory and will create git repository');

	system('cd '.$dir.' && git init --shared=all --bare > /dev/null');

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

	# Write a description to enhance pushed mails.
	open DESCRIPTION, '>'.$dir.'/description';
	print DESCRIPTION $git->get_reponame."\n";
	close DESCRIPTION;

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

	# './hooks' directory must be owned by root to prevent abuse of servers
	Vhffs::Functions::chmod_recur( $dir.'/hooks' , 0644 , 0755 );
	Vhffs::Functions::change_owner_recur( $dir.'/hooks' , 0 , 0 );

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

	return 0;
}

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

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

	# Always unlink since git init create a dummy post-receive
	unlink $dir.'/hooks/post-receive';

	if( $git->get_ml_name !~ /^\s*$/ ) {
		system('cp %VHFFS_BOTS_DIR%/misc/git_post-receive '.$dir.'/hooks/post-receive' );
		system('git config -f '.$dir.'/config hooks.mailinglist '.$git->{ml_name} );
		system('git config -f '.$dir.'/config hooks.envelopesender '.$mail_from );
	}

	return 0;
}

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

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

	return 0;
}


1;
