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

package Vhffs::Robots;

no warnings 'redefine';

use strict;
use utf8;

require Exporter;
my @ISA    = qw(Exporter);
my @EXPORT = qw(vhffs_log lock unlock);

use Vhffs::Functions;
use Vhffs::Main;
use LockFile::Simple qw(lock trylock unlock);
use constant
{
	LOCKFILE => "/var/lock/vhffs",
	LOGFILE  => "/var/log/vhffs"
};


sub lock
{
	my $vhffs = shift;
	my $name = shift;
	return 1 unless defined $vhffs;

	my $robotconf = $vhffs->get_config->get_robots;
	return 0 if( $robotconf->{'use_lock'} ne 'yes' );

	my $lockfile = LOCKFILE.'.'.$name;
	$lockfile = $robotconf->{'lockfile'}.'.'.$name if( $robotconf->{'lockfile'} );

	LockFile::Simple::lock( $lockfile ) or exit( 1 );
	return 0;
}


sub unlock
{
	my $vhffs = shift;
	my $name = shift;
	return 1 unless defined $vhffs;

	my $robotconf = $vhffs->get_config->get_robots;
	return 0 if( $robotconf->{'use_lock'} ne 'yes' );

	my $lockfile = LOCKFILE.'.'.$name;
	$lockfile = $robotconf->{'lockfile'}.'.'.$name if( $robotconf->{'lockfile'} );

	LockFile::Simple::unlock( $lockfile );
	return 0;
}

sub vhffs_log
{
	my $sent = shift;
	my $vhffs = shift;
	my $logfile;
	return 1 unless defined $vhffs;

	my $robotconf = $vhffs->get_config->get_robots;
	return 0 if( $robotconf->{'use_logging'} ne 'yes' );

	my ($seconds,$minutes,$hours,$day,$month,$year) = localtime(time);
	my $timestamp = sprintf ('[ %.4u/%.2u/%.2u %.2u:%.2u:%.2u ]',$year+1900,$month + 1,$day,$hours,$minutes,$seconds);

	$logfile = $robotconf->{'logfile'};
	$logfile = LOGFILE unless defined $logfile;
	open( FILE , ">>$logfile" ) or return -1;
	print FILE $timestamp." - ".$sent."\n";
	close FILE ;

	return 0;
}

sub link_to_group
{
	my $dir = shift;
	my $group = shift;
	my $linkname = shift;
	my $main = shift;

	return -1 unless( defined $group );

	return 1 if( $main->get_config->use_vhffsfs );

	my $path = Vhffs::Functions::hash_groupname( $group->get_groupname , $main )."/".$linkname;

	return -1 if ( symlink( $dir , $path ) == 0 );
	
	return 1;
}


sub unlink_to_group
{
	my $group = shift;
	my $linkname = shift;
	my $main = shift;

	return -1 unless( defined $group );

	return 1 if( $main->get_config->use_vhffsfs );

	my $path = Vhffs::Functions::hash_groupname( $group->get_groupname , $main )."/".$linkname;

	return -1 if ( unlink(  $path ) == 0 );
	
	return 1;
}

1;

__END__

=head1 NAME

Vhffs::Robots - common interface for VHFFS Robots

=head1 SYNOPSIS

	use Vhffs::Robots;

	Vhffs::Robots::link_to_group( "/path/to" , Vhffs::Group , "linkname" , Vhffs::Main);

	Vhffs::Robots::unlink_to_group( Vhffs::Group , "linkname" , Vhffs::Main);

	Vhffs::Robots::lock();

	Vhffs::Robots::unlock();

	Vhffs::Robots::vhffs_log( "My log message" );

=head1 DESCRIPTION

This class contains static methods commonly used by VHFFS robots.

=head1

lock() : Creates a lockfile. Other process that call lock method are stalled until unlock is created.

unlock() : Delete the lock file. Other process, whose are stalled due to the lock file can continue the execution

link_to_group() : create a symlink from a directory to a group directory.

unlink_to_group() : unlink a symlink in a group directory

=head1 AUTHOR

	Julien Delange <julien at gunnm dot org>

=head1 COPYRIGHT

	(c) Julien Delange
