#!/usr/local/bin/perl
#
# $1 = /afs/slac/g/cd/soft/images
# Get the directory where the current file is located.
# 

($directory) = @ARGV;

@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
           "Oct","Nov","Dec");

# Get the local system time

@time_array = localtime;
$month = @months[$time_array[4]];
$day = $time_array[3];
$year = 1900 + $time_array[5];

# Concatenate the filenames
#    Old file is: <directory from input arg> + test.pl (in this case)
#    New file is: <directory from input arg> + "/daily/daily_<ddmmmyyyy>.pdf"

$yesterday = sprintf("%s/daily/yesterday.pdf", $directory);
$twodaysago  = sprintf("%s/daily/2daysago.pdf", $directory);
$threedaysago  = sprintf("%s/daily/3daysago.pdf", $directory);
$fourdaysago  = sprintf("%s/daily/4daysago.pdf", $directory);

$old_file = sprintf("%s/lcls.pdf", $directory);
$new_file = sprintf("%s/daily/lcls_%02d%02s%04d.pdf", $directory, $day, $month, $year );

# Wait 10 minutes, so that midnight goes by and the new PDF is available

sleep 600;

# Copy the old file to the new file
# added the "4daysago, 3daysago, 2daysago, yesterday" for ease of use
# from the webpage.
# 
system("cp $threedaysago $fourdaysago");
system("cp $twodaysago $threedaysago");
system("cp $yesterday $twodaysago");
system("cp $old_file $new_file");
system("cp $new_file $yesterday");
 






