#!/usr/bin/perl

#
# PROJECT
#          Hinode/EIS
# PURPOSE
#          Perl script to unpack archive tarball from EISCO
# 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 put obstbl and obstbl_map into EIS_CPT_output
#          v1.2 JAR 14-Oct-2008
#               Check $HOME/remote_planning and subdirectories exist.
#               Check $HOME/EIS_CPT_output  and subdirectories exist.
#               If not, then create them (mkdir)
#          v1.3 JAR/DHB 28-Oct-2008
#               Check that we have planning_db and EIS_CPT_output
#               directories in our tarball archive. If not, don't
#               try to unpack them. This is for updating CP cmdpln
#               info. only.
#

use Env qw(HOME);

# Define some variables
$archive_tarball         = "$HOME/eis_rp_archive.tar.gz";
$archive_dir             = "$HOME/eis_rp_archive";
$archive_cpt_output_dir  = "$archive_dir/EIS_CPT_output";
$archive_cmdpln_dir      = "$archive_dir/cmdpln";
$archive_planning_db_dir = "$archive_dir/planning_db";

print "Checking if obstbl map exists in eis_rp_archive.tar.gz tarball...\n";
$command="tar tvfz $archive_tarball | grep map";
$status = system ("$command");
if ($status == 0) {

# Unpack tarball
print "+++++++++++++++++++++++++++++++++++++++++++++++\n";
# Check whether $HOME/eis_rp_archive directory exists and remove it if it does
if(-d $archive_dir) {
    print "Removing $archive_dir\n";
    system("/bin/rm -rf $archive_dir");
}

print "Unpacking eis_rp_archive.tar.gz tarball...\n";
system("cd $HOME && tar xzf $archive_tarball eis_rp_archive/");

#
# EIS_CPT_output obstbl and obstbl_map files
#
print "\n";
print "Checking EIS_CPT_output directory and sub-directories exist...\n";
unless (-d "$HOME/EIS_CPT_output"){
    print "    Creating $HOME/EIS_CPT_output              directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output");
}

unless (-d "$HOME/EIS_CPT_output/data_rate"){
    print "    Creating $HOME/EIS_CPT_output/data_rate    directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/data_rate");
}
unless (-d "$HOME/EIS_CPT_output/dr"){
    print "    Creating $HOME/EIS_CPT_output/dr           directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/dr");
}
unless (-d "$HOME/EIS_CPT_output/eis_plan"){
    print "    Creating $HOME/EIS_CPT_output/eis_plan     directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/eis_plan");
}
unless (-d "$HOME/EIS_CPT_output/intel_tables"){
    print "    Creating $HOME/EIS_CPT_output/intel_tables directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/intel_tables");
}
unless (-d "$HOME/EIS_CPT_output/mssl_tables"){
    print "    Creating $HOME/EIS_CPT_output/mssl_tables  directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/mssl_tables");
}
unless (-d "$HOME/EIS_CPT_output/obstbl"){
    print "    Creating $HOME/EIS_CPT_output/obstbl       directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/obstbl");
}
unless (-d "$HOME/EIS_CPT_output/obstbl_map"){
    print "    Creating $HOME/EIS_CPT_output/obstbl_map   directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/obstbl_map");
}
unless (-d "$HOME/EIS_CPT_output/orl"){
    print "    Creating $HOME/EIS_CPT_output/orl          directory\n";
    system("/bin/mkdir  $HOME/EIS_CPT_output/orl");
}


print "Updating EIS_CPT_output obstbl and obstbl_map files...\n";

$urlocal_obstbl_file = "$HOME/EIS_CPT_output/obstbl/EIS_OBSTBL_internal.hex";
$archive_obstbl_file = "$archive_cpt_output_dir/EIS_OBSTBL_internal.hex";
system("/bin/rm -f $urlocal_obstbl_file");
system("/bin/cp $archive_obstbl_file $urlocal_obstbl_file");

$urlocal_obstbl_map_file = "$HOME/EIS_CPT_output/obstbl_map/OBSTBL_internal.map";
$archive_obstbl_map_file = "$archive_cpt_output_dir/OBSTBL_internal.map";
system("/bin/rm -f $urlocal_obstbl_map_file");
system("/bin/cp $archive_obstbl_map_file $urlocal_obstbl_map_file");

#
# planning_db
#
print "\n";
print "Refreshing \$HOME/planning_db directory...\n";
system("/bin/rm -rf $HOME/planning_db");
print "    Copying $archive_planning_db_dir to $HOME\n";
system("/bin/cp -r $archive_planning_db_dir $HOME");

}

#
# CMDPLN
#
# Get a directory listing for eis_rp_archive/cmdpln (excluding
# directories . and ..). Loop through each subdirectory and copy
# (delete existing directory if it already exists) them into
# $HOME/remote_planning/cp/cmdpln
#
print "\n";
print "Checking cmdpln directories exist...\n";

unless (-d "$HOME/remote_planning"){
    print "    Creating directory $HOME/remote_planning\n";
    system("/bin/mkdir            $HOME/remote_planning");
}
unless (-d "$HOME/remote_planning/cp"){
    print "    Creating directory $HOME/remote_planning/cp\n";
    system("/bin/mkdir            $HOME/remote_planning/cp");
}
unless (-d "$HOME/remote_planning/cp/cmdpln"){
    print "    Creating directory $HOME/remote_planning/cp/cmdpln\n";
    system("/bin/mkdir            $HOME/remote_planning/cp/cmdpln");
}
unless (-d "$HOME/remote_planning/eisco"){
    print "    Creating directory $HOME/remote_planning/eisco\n";
    system("/bin/mkdir            $HOME/remote_planning/eisco");
}
unless (-d "$HOME/remote_planning/eisco/cmdpln"){
    print "    Creating directory $HOME/remote_planning/eisco/cmdpln\n";
    system("/bin/mkdir            $HOME/remote_planning/eisco/cmdpln");
}
unless (-d "$HOME/remote_planning/eisco/cmdpln/latest"){
    print "    Creating directory $HOME/remote_planning/eisco/cmdpln/latest\n";
    system("/bin/mkdir            $HOME/remote_planning/eisco/cmdpln/latest");
}


print "Updating CP CMDPLN files...\n";
opendir(dirhandle, $archive_cmdpln_dir);
@cmdpln_files = grep !/^\.\.?$/,readdir(dirhandle);
closedir(dirhandle);
foreach $dir (@cmdpln_files) {
   if(-e "$HOME/remote_planning/cp/cmdpln/$dir") {
    #print "Deleting folder: $HOME/remote_planning/cp/cmdpln/$dir\n";
    system("/bin/rm -rf $HOME/remote_planning/cp/cmdpln/$dir");
    }
    print "    Copying folder : $HOME/remote_planning/cp/cmdpln/$dir\n";
    system("/bin/cp -r $archive_cmdpln_dir/$dir $HOME/remote_planning/cp/cmdpln/");
}


print "\n";
print "Script finished!\n";
print "    Now start sswidl and invoke eis Planning Tools\n";
print "    in Remote Planning mode with;\n";
print "        SSWIDL> eis_mk_plan , /rp\n";
print "+++++++++++++++++++++++++++++++++++++++++++++++\n";

