#!/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/NarcsToHighcharts";

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

$output_file = "/data/www/smcd/opdb/nprovs/js/nprovs_narcs.js";
$temp_output_file = "/data/data065/nprovs/source/narcs/NarcsToHighcharts/nprovs_narcs.js";

$settings_file = "/data/data065/nprovs/source/narcs/NarcsToHighcharts/nprovs_highchart_settings.xml";
$default_colors_file = "/data/data065/nprovs/source/narcs/NarcsToHighcharts/default_colors.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";
symlink $default_colors_file, "default_colors.file" or warn "Cannot link the default colors files";


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

unlink "default_colors.file" or warn "Cannot unlink the default_colors.file";
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 to Highcharts completed.\n");

# end of file
