#!/usr/bin/perl -W

#
# PROJECT
#          Hinode/EIS
# PURPOSE
#          Perl script which runs on EISCO. It creates a temporary
#          archive directory, and copies to it the various components
#          of the databases and CP files. This is then tarballed.
# CATEGORY
#          EIS Timeline Planning/Remote Planning
# WRITTEN
#          John Rainnie & Matthew Whillock
# HISTORY
#          v0.1 JAR 9-Sep-2008
#               Added header
#          v1.1 JAR 23-Sep-2008
#               Also get obstbl and obstbl_map from EIS_CPT_output
#          v1.2 JAR 20-Oct-2008
#               Commented out code to scp the tarball to ssh2
#          v1.3 JAR 21-Oct-2008
#               Print progress to a file
#          v1.4 JAR/DHB/DRW 30-Oct-2008
#               Copy timeline_db from user directory at /home/eisco
#

#use Date;
use Env qw(HOME SSW CMDPLN);

$a_dir = "$HOME/eis_rp_archive";
#########################################################################
#
# Refresh archive directory
#
#########################################################################
# Check whether $HOME/eis_rp_archive directory exists - remove it if so!
if(-e $a_dir) {
    print "Removing $a_dir\n";
    system("/bin/rm -rf $a_dir");
}

# Re-create the eis_rp_archive directory
system("/bin/mkdir $a_dir");


#########################################################################
#
# Create a new log file in the new archive directory
#
#########################################################################
print "Creating log file: eis_rp_archive/eis_rp_archive_log.txt\n";

open (LOGFILE, ">$a_dir/eis_rp_archive_log.txt");
print LOGFILE "Archiving eisco databases/files...\n";


#########################################################################
#
# OBSTBL & OBSTBL_MAP files
#
#########################################################################
# Create directory
system("/bin/mkdir $a_dir/EIS_CPT_output");
print LOGFILE "Creating folder: EIS_CPT_output\n";
# Copy $HOME/EIS_CPT_output/obstbl/EIS_OBSTBL_internal.hex into eis_rp_archive
print LOGFILE "    Fetching file: obstbl/EIS_OBSTBL_internal.hex\n";
$obstbl = "$HOME/EIS_CPT_output/obstbl/EIS_OBSTBL_internal.hex";
system("/bin/cp -p $obstbl $a_dir/EIS_CPT_output");

# Copy $HOME/EIS_CPT_output/obstbl_map/EIS_OBSTBL_internal.hex into eis_rp_archive
print LOGFILE "    Fetching file: obstbl_map/OBSTBL_internal.map\n";
$obstbl_map = "$HOME/EIS_CPT_output/obstbl_map/OBSTBL_internal.map";
system("/bin/cp -p $obstbl_map $a_dir/EIS_CPT_output");


#########################################################################
#
# Planning Databases
#
#########################################################################
# Copy all of $SSW/../planing_db into eis_rp_archive
print LOGFILE "\n";
print LOGFILE "Creating folder: planning_db\n";
system("/bin/cp -rp $SSW/hinode/eis/database/planning_db/ $a_dir");

# Remove the SSW timeline_db
system("/bin/rm -rf $a_dir/timeline_db");

# Transplant eisco's local timeline_db to the archive directory
#system("/bin/cp -rp $HOME/planning_db/timeline_db $a_dir/planning_db");
system("/bin/cp -rp /home/eisco/planning_db/timeline_db $a_dir/planning_db");


#########################################################################
#
# CP CMDPLN
#
#########################################################################
system("/bin/mkdir $a_dir/cmdpln");

print LOGFILE "\n";
print "Getting previous days CP (cmdpln) input files;\n";
print LOGFILE "Getting previous days CP (cmdpln) input files;\n";

print LOGFILE "    $CMDPLN/latest\n";
system("/bin/cp -rL $CMDPLN/latest   $a_dir/cmdpln");
print "    $CMDPLN/latest\n";

print LOGFILE "    $CMDPLN/today\n";
system("/bin/cp -rL $CMDPLN/today    $a_dir/cmdpln");
print "    $CMDPLN/today\n";

print LOGFILE "    $CMDPLN/tomorrow\n";
system("/bin/cp -rL $CMDPLN/tomorrow $a_dir/cmdpln");
print "    $CMDPLN/tomorrow\n";

# Copy next and previous days directories
foreach $day (-5 .. 7) {
    $new_time = time - ($day * (24 * ( 60 * 60)));
    @foo = localtime($new_time);
    $time_string = sprintf "%4u%02u%02u", $foo[5] + 1900, $foo[4] + 1, $foo[3];
    $cmdpln_dir = "$CMDPLN/" . "$time_string";
    if(-e $cmdpln_dir) {
    print "    $cmdpln_dir\n";
    print LOGFILE "    $cmdpln_dir\n";
    system("/bin/cp -rL $cmdpln_dir $a_dir/cmdpln/");
    }
}

print LOGFILE "\n";
print LOGFILE "End of log.\n";
close LOGFILE;

#########################################################################
#
# Tar up the directory here!
#
print "Archiving the necessary files from EISCO needed for remote planning\n";
system("cd $HOME && tar cfz $HOME/eis_rp_archive.tar.gz eis_rp_archive/");
#########################################################################

#
# JAR 20-Oct-2008
# Commented out code to scp the tarball to ssh2
#
# print "Copying archive tar file to ssh2\n";
# $scp_str = "scp $HOME/eis_rp_archive.tar.gz dedlok" . "@" . "ssh2:~";
# $success=system($scp_str);
# #$success=system("scp $HOME/eis_rp_archive.tar.gz dedlok\@ssh2:~");
# #print "success = $success\n";
# if($success == 0) {
#     print "Copying marker file to ssh2\n";
#     $scp_str = "scp $HOME/scp_marker,txt dedlok" . "@" . "ssh2:~";
#     $success=system($scp_str);
# #    system("scp $HOME/scp_marker.txt dedlok\@ssh2:~");
# }
# else {
#     print "Failed to copy archive file to ssh2\n";
# }


