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

use Vhffs::Services::Web;
use Vhffs::Constants;
use Vhffs::Functions;
use Vhffs::Robots;

sub create_web
{
	use Vhffs::Group;

	my $main = shift;
	my $web = shift;

	if( defined $web ) 
	{
		$web->add_history( "Ok, the robot take the creation of this object. Web directory will be created");
		my $webdir = $main->get_config->get_datadir . "/" . Vhffs::Constants::WEBDIR . "/" . Vhffs::Functions::hash_webdir( $web->get_servername );
		
		Vhffs::Functions::create_dir( $webdir );
#		chown( $web->get_owner_uid , $web->get_owner_gid , $webdir  ); 
		Vhffs::Functions::create_dir( $webdir . "/htdocs");
		chown( $web->get_owner_uid , $web->get_owner_gid , $webdir . "/htdocs" ); 
		chmod( 02775 , $webdir . "/htdocs" );
		Vhffs::Functions::create_dir( $webdir . "/php-include");

		chown( $web->get_owner_uid , $web->get_owner_gid , $webdir . "/php-include" ); 
		chmod( 02775 , $webdir . "/php-include" );
		Vhffs::Functions::create_dir( $webdir . "/tmp");
		chown( $web->get_owner_uid , $web->get_owner_gid , $webdir . "/tmp" ); 
		chmod( 02775 , $webdir . "/tmp" );
	
		my $group = Vhffs::Group::get_by_gid( $main , $web->get_owner_gid );
		if(Vhffs::Robots::link_to_group( $webdir,$group,$web->get_servername.Vhffs::Constants::WEB_SUFFIX,$main) < 0)
		{
			$web->add_history( "Can't link the webdirectory to the specified group");
		}
		else
		{
			$web->add_history( "Ok, robots finished to create webdir. Webdir is now linked to the group directory");
		}
		$web->set_status( Vhffs::Constants::ACTIVATED );	
		$web->commit;
	}
}


sub delete
{
	use Vhffs::Group;
	use File::Path;
	use File::Basename;

	my( $main , $web ) = @_;

	my $dir;	
	if(defined $web)
	{
		$web->add_history( "Ok, the robot will delete this object. Web directory will be removed");
		my $hash = Vhffs::Functions::hash_webdir( $web->get_servername );

		return -1 if( ( ! defined $hash ) || ( length( $hash ) < 2 ) );
		
		my $webdir = $main->get_config->get_datadir . "/" . Vhffs::Constants::WEBDIR . "/" . $hash ;
	
		my $group = $web->get_group;
		if(Vhffs::Robots::unlink_to_group( $group,$web->get_servername.Vhffs::Constants::WEB_SUFFIX,$main) < 0)
		{
			$web->add_history( "Can't unlink the webdirectory from the specified group");
		}
		else
		{
			$web->add_history( "Webdir is now unlinked from the group directory");
		}

		File::Path::rmtree( $webdir );
		my $parent = dirname($webdir);
		rmdir $parent;
		$parent = dirname($parent);
		rmdir $parent;
		$parent = dirname($parent);
		rmdir $parent;

		$web->add_history( "Webdir is now erased.");
		$web->delete;	
	}
}



1;

