#!%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::Repository;

use Vhffs::Services::Repository;
use Vhffs::Constants;
use Vhffs::Functions;


sub create_repository
{
	my $repo = shift;

	if( defined $repo )
	{
		if( create_repositoryondisk( $repo ) > 0 )
		{
			$repo->set_status( Vhffs::Constants::ACTIVATED );
			$repo->commit;
			$repo->add_history("Download repository is now created !" );
		}
		else
		{
			$repo->add_history("The bot encounter an error during creation of this download repository, the repository is NOT created !" );
		}
	}
}


sub delete_repository
{
	my $repo = shift;

	if( defined $repo )
	{
		my $vhffs = $repo->{'main'};

		# erase all files
		my $dir = $repo->get_dir;
		my $cmd = "rm -rf $dir";
		system( $cmd );

		# remove the link in group directory
		my $group = Vhffs::Group::get_by_gid( $vhffs , $repo->get_owner_gid );
		Vhffs::Robots::unlink_to_group( $group, $repo->get_name.Vhffs::Constants::REPOSITORY_SUFFIX, $vhffs );

		$repo->delete;
	}
}



sub create_repositoryondisk
{
	my $repo = shift;
	return if( ! defined $repo );
	my $name = $repo->get_name;
	my $vhffs = $repo->{'main'};

	my $datadir = $vhffs->get_config->get_datadir;
	
	my $repodir = $repo->get_dir;
	return -1 if( $repodir =~ /\./ );

	Vhffs::Functions::create_dir( $repodir ) if( ! -d $repodir );
	return -2 if( ! -d $repodir );

	chown( $repo->get_owner_uid , $repo->get_owner_gid , $repodir );
	chmod( 02775 , $repodir );

	my $group = Vhffs::Group::get_by_gid( $vhffs , $repo->get_owner_gid );

	if(Vhffs::Robots::link_to_group( $repodir, $group, $repo->get_name.Vhffs::Constants::REPOSITORY_SUFFIX, $vhffs ) < 0)
	{
		$repo->add_history( "Can't link the download repository to the specified group");
		return -3;
	}
	else
	{
		$repo->add_history( "OK, download repository is now linked with the group");
	}

	return 1;
}

1;

