#!%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.


# This is a part of VHFFS platform
# Please consider the entire platform
# See http://vhffs.org

# by sod

use strict;
use utf8;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Robots;
use Vhffs::Functions;
use Vhffs::Constants;
use Vhffs::Services::Cvs;


my $vhffs = init Vhffs::Main;

Vhffs::Robots::lock( $vhffs , 'cvs' );

my $cvss = Vhffs::Services::Cvs::getall( $vhffs , Vhffs::Constants::WAITING_FOR_MODIFICATION );
foreach my $cvs ( @{$cvss} )
{
	my $readers_file = $cvs->get_dir.'/CVSROOT/readers';
	my $passwd_file = $cvs->get_dir.'/CVSROOT/passwd';

	if( $cvs->is_public == 1 )
	{
		Vhffs::Robots::vhffs_log( sprintf( 'CVS fixperm (public) for %s', $cvs->get_cvsroot ), $vhffs);
		$cvs->add_history('CVS is now public');
		chmod 02775 , $cvs->get_dir;

		# fix readers file
		Vhffs::Robots::vhffs_log( sprintf('CVS, put repository %s public' , $cvs->get_cvsroot), $vhffs);
		open( FILE , '>'.$readers_file ) or die( 'cannot open '.$readers_file );
		print FILE 'anonymous'."\n";
		close( FILE );
		chown $cvs->get_owner_uid , $cvs->get_owner_gid , $readers_file;

		# fix passwd file
		open( FILE , '>'.$passwd_file ) or die( 'cannot open '.$passwd_file );
		print FILE 'anonymous::'.$cvs->get_user->get_username."\n";
		close( FILE );
		chown $cvs->get_owner_uid , $cvs->get_owner_gid , $passwd_file;
	}
	else
	{
		Vhffs::Robots::vhffs_log( sprintf( 'CVS fixperm (private) for %s', $cvs->get_cvsroot ), $vhffs);
		$cvs->add_history('CVS is now private');
		chmod 02770 , $cvs->get_dir;

		# delete reader_file if it exists
		unlink $readers_file if -f $readers_file;
	}

	$cvs->set_status( Vhffs::Constants::ACTIVATED );
	$cvs->commit;
}

Vhffs::Robots::unlock( $vhffs , 'cvs' );

exit 0;
