#!/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+1), DaysinMonth);



#---------------------------------------------------------
# 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";
$temp_output_file = "/data/data065/nprovs/source/narcs/NarcsToHighchartsHeatmap/nprovs_heatmap.js";

$settings_file = "/data/data065/nprovs/source/narcs/NarcsToHighchartsHeatmap/nprovs_heatmap_settings.xml";

symlink $input_file, "input.file" or warn "Cannot link the input file";
symlink $temp_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 -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";

#---------------------------------------------------------
# Copy the temporary output file to the output file

copy ($temp_output_file, $output_file);
unlink ($temp_output_file);


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

# end of file
