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


$baseday_minus_one = $ARGV[0];
$baseday           = $ARGV[1];
$baseday_plus_one  = $ARGV[2];

printf("Date to be processed:  %d   %d   %d\n\n", $baseday_minus_one, $baseday, $baseday_plus_one);


$source_dir = "/data/data599/orbital_archive/unified.radiosonde";
$temp_dir   = "/data/data065/nprovs/scripts/data_input/reprocess/special_capture/data";
$target_dir = "/net/www/aftp/pub/smcd/opdb/mpettey";


# Copy the files to the temp directory

$from_file = sprintf("%s/unified.radiosonde.reports.%s.gz", $source_dir, $baseday_minus_one);
$to_file   = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday_minus_one);
copy($from_file, $to_file);

$from_file = sprintf("%s/unified.radiosonde.reports.%s.gz", $source_dir, $baseday);
$to_file   = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday);
copy($from_file, $to_file);

$from_file = sprintf("%s/unified.radiosonde.reports.%s.gz", $source_dir, $baseday_plus_one);
$to_file   = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday_plus_one);
copy($from_file, $to_file);


# Tar the file

chdir $temp_dir;

system "tar -cvf sondes.tar *.gz";


# Copy the tar file to the target directory

$from_file = sprintf("%s/sondes.tar", $temp_dir);
$to_file   = sprintf("%s/sondes_%d.tar", $target_dir, $baseday);
copy($from_file, $to_file);


# Remove the files from the temp directory

$file = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday_minus_one);
unlink $file;

$file = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday);
unlink $file;

$file = sprintf("%s/unified.radiosonde.reports.%s.gz", $temp_dir, $baseday_plus_one);
unlink $file;

$file = sprintf("%s/sondes.tar", $temp_dir);
unlink $file;


# end of file
