#! /GNU/1998/bin/perl

# The aim of this software is to make a screen saver with the Xv programm
# It will take all jpeg files included in the directory $image
# and every $delay_time will change it
# the PATH of xv shall be in the path


# author Pierre Roullet

# version 1.0 date : 06th July 99
# first release version with adding new features

# version 0.1 date : 12 may 99
# creation

# all this program is copyright under GNU Licence

if ( (scalar @ARGV) == 0)
{
	die(" Syntaxe saver.pl  image_directory delay_in_second  \n");
} 


my $image = @ARGV[0];
my $temp1 = "/tmp/image.tmp";
my $start_time = 0;
my $current_time = 0;
my $delay_time = @ARGV[1] ;

# a system command can be used

RunCmd("rm $temp1"); 
if (RunCmd("which xv") ne "OK" )
{ 
die "Xv in not on your path please asks your administrator ";
}

#put all the name of the file in a temporary file

system ("ls $image\*\.jpg > $temp1");

#open of the file in reading mode only

open (INFILE, "< $temp1");
@l_file = <INFILE>;
close <INFILE>;

chomp @l_file;

$j = @l_file;
$j2 = 0 ;
$start_time = time;
 RunCmd ("xv -rmode 5 -quit $l_file[$j2]");

while (1)
{
 $current_time = time ;
 if ($current_time == ($start_time + $delay_time))
 {
 	$j2++;
	$start_time = $current_time;
	if ( $j2 >= $j)
	{
	$j2=0;
	}
  RunCmd ("xv -rmode 5 -quit $l_file[$j2]");
  }
}




# This routine can be used to call system function and
# to control the result
# - RunCmd :
#   In-parameter  : - The command to run.
#   Out-parameter : - Error message or OK.
#
# - RunCmdOut :
#   In-parameter  : - The command to run.
#                   - Out reference list for the command STDOUT.
#   Out-parameter : - Error message or OK.

sub RunCmd
{
    my ($cmd) = @_;
	my ($SaveSTDERR,$foncname);
    my (@llresult);
	$foncname = "RunCmd";
	# We redirect STDERR.
	open SaveSTDERR, ">&STDERR";
	close STDERR;
	# We execute the in-parameter command.
	@llresult = `$cmd ; exit \$?`;
	# We restore original STDERR.
	open STDERR, ">&SaveSTDERR";
	if ($? >> 8)
	# The command does not
	# succeed.
	{
	 return
	"!ERROR,0003,$foncname,$cmd";
	}
	return "OK";
}



