#!/usr/bin/perl -w
#
use File::Copy;
use Time::Local;


#---------------------------------------------------------
# Determine the date range

my ( $day, $mon, $year ) = ( localtime )[3,4,5];
$year2 = $year+1900;

my @monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 );
sub DaysinMonth {
     return 29   if  2 == $mon  and
          0 == $year%4  &&  0 == $year%100  ||  0 == $year%400;
    return $monthDays[$mon-1];
}

$start_date = "20080408";
$end_date   = sprintf("%d%02d%02d", $year2, $mon, DaysinMonth);


#---------------------------------------------------------
# Copy the latest 30 layer NPROVS NARCS file

#printf("\nCopying the NPROVS 30 layer NARCS file...\n");

#$from_file  = "/data/www/data/nprovs/monitoring/narcs/nprovs_30layers.narcs";
#$narcs_file = "/data/data065/nprovs/scripts/web_mp/narcs_nprovs_all.dat";

#copy ($from_file, $narcs_file);

#printf("Narcs file copied\n\n");


#---------------------------------------------------------
# Run the program to create the Highchart file

$source_dir = "/data/data065/nprovs/source/narcs/NarcsToHighchartsHeatmap";

$input_file = "/data/www/data/nprovs/monitoring/narcs/nprovs_100levels.narcs";

$output_file = "/data/www/smcd/opdb/nprovs/js/nprovs_heatmap.js";
$settings_file = "/data/data065/nprovs/source/narcs/NarcsToHighchartsHeatmap/nprovs_heatmap_settings.xml";

unlink $output_file;

symlink $input_file, "input.file" or warn "Cannot link the input file";
symlink $output_file, "output.file" or warn "Cannot link the output file";
symlink $settings_file, "settings.file" or warn "Cannot link the setting file";

#$jar_exec = sprintf("java -Djava.awt.headless=true -jar %s/NarcsToHighchartsHeatmap.jar %d %d", $source_dir,
#                                                                                         $start_date, $end_date);

$jar_exec = sprintf("java -jar %s/NarcsToHighchartsHeatmap.jar %d %d", $source_dir,
                                                                       $start_date, $end_date);
system $jar_exec;

unlink "settings.file" or warn "Cannot unlink the settings.file";
unlink "output.file" or warn "Cannot unlink the output.file";
unlink "input.file" or warn "Cannot unlink the input.file";





#---------------------------------------------------------
# Remove the NARCS file that was copied

#unlink $narcs_file or warn "Could not remove the temporary NARCS file";



printf("\n\nNARCS Long term plots for 20080410 to %d completed.\n",$end_date);

# end of file
