#!/usr/bin/perl

# (If "which perl" indicates a different path to perl, we recommend 
#  that you (or your system administrator) create a symbolic link to it 
#  at  /usr/bin/perl, so that you don't have to change the above path 
#  in this script and the other scripts in this distribution; 
#  this is the syntax:
#	ln  -s  <wherever perl is>  /usr/bin/perl
#  where "<wherever perl is>" is replaced by the path shown by "which perl".)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Variables within this block must be set correctly for the specific 
#  distribution

# Set the names of the new release and the previous release
$newrelname = "V2.2";
$oldrelname = "V2.00";
$oldrelnamebrev = "v200";

# For $oldrelname before "V2.00", $old_build_script_name = "build" and 
#  $old_build_env_script_name = "build-env";
# for $oldrelname of "V2.00" or later, $old_build_script_name = "build.pl" 
#  and $old_build_env_script_name = "build-env.pl"
$build_script_name_base = "build";
$build_env_script_name_base = "build-env";
$old_build_script_name = "${build_script_name_base}.pl";
$old_build_env_script_name = "${build_env_script_name_base}.pl";
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$timestamp = `date +"%Y%m%d%H%M%S"`;
chomp ($timestamp);

# Take the name of the current working directory as the server name
#  (On fleetlink, data server root directories are under /data1/.)
#  (On globec, the data server root directory is under /data5/.)
$cwdpath = `pwd`;
chomp $cwdpath;
@cwdfields = split (/\//, $cwdpath);
$servername = $cwdfields[$#cwdfields];

# Get the name of this script from the last element of its path 
#  (i.e., following last '/')
@thisscriptpathfields = split (/\//, $0);
$thisscriptname = $thisscriptpathfields[$#thisscriptpathfields];

# Get the release tag from the beginning of the script's name 
#  (before first '_')
@thisscriptnamefields = split (/_/, $thisscriptname);
$newreleasetag = $thisscriptnamefields[0];
$newreleasetaglc = length $newreleasetag;

# Set the name of the gzipped tar file containing the actual ${newrelname} 
#  distribution
$dontuntarfilestring = "dontuntar_file";
$newreldontuntarfilename = "${newreleasetag}_${dontuntarfilestring}";

# Set the name of the gzipped tar file 
#  containing the extra file needed to finish preparing for the upgrade
$specialwrap2string = "specialwrap2";
$newrelspecialwrap2name = "${newreleasetag}_${specialwrap2string}";

# Set the names of the two files that this script requires 
#  to be unpacked from the "specialwrap2" file
$finish_oldrelclean_name = "${newreleasetag}_finish_${oldrelnamebrev}_clean.pl";
$remove_newrel_restore_oldrel_name = 
    "XXX_remove_bad_${newrelname}_restore_${oldrelname}.pl";

# Set up base of name of the ${oldrelname} installation backup gzipped tar file
$oldreleasetag = "jgofs-${oldrelname}";
$oldrelbackupfilenamebase = "${servername}_${oldreleasetag}_backup_";

# Set up name for "finish upgrade" log file
$finish_upgrade_logfile = "log_${newreleasetag}_2finish_upgrade_${timestamp}";


print "Begin  ${thisscriptname}  ...\n";
print "\n";
print "Begin checking this directory, the ${oldrelname} backup, and\n";
print "       the ${newrelname} distribution ...\n";

unless (-e $newreldontuntarfilename) {
    # The new distribution's "dontuntar_file" is missing 
    &print_usage_err_msg;
    exit -1;
}

unless (-e $newrelspecialwrap2name) {
    # The new distribution's "specialwrap2" file is missing 
    &print_usage_err_msg;
    exit -1;
}


# Check that this looks like the root directory of a ${oldrelname} data server
unless (-e $old_build_script_name && -e $old_build_env_script_name) {
    &print_usage_err_msg;
    exit -1;
}


# Check that the  $newrelspecialwrap2name  file is a gzipped tar file
if (system ("tar tfz ${newrelspecialwrap2name} >/dev/null 2>&1")) {
    &print_distrib_err_msg;
    exit -1;
}

# Make a list of the contents of the  $newrelspecialwrap2name  file
@newrelspecialwrap2ls = `tar tfz $newrelspecialwrap2name | cut -f 2 -d'/'`;
chomp (@newrelspecialwrap2ls);
if ($newrelspecialwrap2ls[0] eq "") {
    shift @newrelspecialwrap2ls;
}

# Check that the  $newrelspecialwrap2name  file contains files 
#  $finish_oldrelclean_name  and  $remove_newrel_restore_oldrel_name

$foundrequiredfiles = 0;
$found_finish_oldrelclean_name = 0;
$found_remove_newrel_restore_oldrel_name = 0;
foreach $fileinnewrelspecialwrap2 (@newrelspecialwrap2ls) {
    unless ($found_finish_oldrelclean_name) {
	if ($fileinnewrelspecialwrap2 eq $finish_oldrelclean_name) {
	    $found_finish_oldrelclean_name = 1;
	    next;
	}
    }
    unless ($found_remove_newrel_restore_oldrel_name) {
	if ($fileinnewrelspecialwrap2 eq $remove_newrel_restore_oldrel_name) {
	    $found_remove_newrel_restore_oldrel_name = 1;
	    next;
	}
    }
} continue {
    if ($found_finish_oldrelclean_name && $found_remove_newrel_restore_oldrel_name) {
	$foundrequiredfiles = 1;
	last;
    }
}
unless ($foundrequiredfiles) {
    &print_distrib_err_msg;
    exit -1;
}


# Check that the ${oldrelname} installation backup that  
#  jgofs-${newrelname}_1install_script.pl  should have made is there
unless (defined <./$oldrelbackupfilenamebase*.tz>) {
    print STDERR 
	"ERROR:  Expecting a backup file of the existing ${oldrelname} data server,\n";
    print STDERR 
	" with a name of the form \"./${oldrelbackupfilenamebase}*.tz\".\n";
    print STDERR 
	" It should have been created by running  ${newreleasetag}_1install_script.pl.\n";
    exit -1;
}


print "... This directory looks like the root of a JGOFS ${oldrelname} data server,\n";
print "       and the ${oldrelname} backup and ${newrelname} distribution look OK.\n";

# Start logging, in $finish_upgrade_logfile
system ("echo \"Begin  ${thisscriptname}:\" >${finish_upgrade_logfile}");
system ("echo \"       Continuing upgrade from ${oldreleasetag} ...\" >>${finish_upgrade_logfile}");


# Unpack  $newrelspecialwrap2name  into the current working directory
print "\n";
print "Begin unpacking  ${newrelspecialwrap2name}  ...\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Begin unpacking  ${newrelspecialwrap2name}  ...\" >>${finish_upgrade_logfile}");
system ("echo \"tar xfvzp ${newrelspecialwrap2name} >>${finish_upgrade_logfile} 2>&1\" >>${finish_upgrade_logfile}");
if (system ("tar xfvzp ${newrelspecialwrap2name} >>${finish_upgrade_logfile} 2>&1")) {
    system ("echo \"ERROR:  Unpacking  ${newrelspecialwrap2name}  failed.\" >>${finish_upgrade_logfile}");
    system ("echo \" It is a gzipped tar file; there was a problem untarring it.\" >>${finish_upgrade_logfile}");
    print STDERR 
	"ERROR:  Unpacking  ${newrelspecialwrap2name}  failed.\n";
    print STDERR
	" It is a gzipped tar file; there was a problem untarring it.\n";
    exit -1;
}
system ("echo \"... Finished unpacking  ${newrelspecialwrap2name}.\" >>${finish_upgrade_logfile}");
print "... Finished unpacking  ${newrelspecialwrap2name}.\n";


# Run "./${old_build_script_name} -cleanonly" to clean out generated files 
#  from the ${oldrelname} installation
print "\n";
print "Begin cleaning out generated files from the ${oldrelname} installation ...\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Begin cleaning out generated files from the ${oldrelname} installation ...\" >>${finish_upgrade_logfile}");
system ("echo \"./${old_build_script_name} -cleanonly >>${finish_upgrade_logfile} 2>&1\" >>${finish_upgrade_logfile}");
if (system ("./${old_build_script_name} -cleanonly >>${finish_upgrade_logfile} 2>&1")) {
    system ("echo \"ERROR:  \\\"${old_build_script_name} -cleanonly\\\" failed.\" >>${finish_upgrade_logfile}");
    print STDERR "ERROR:  \"${old_build_script_name} -cleanonly\" failed.\n";
    exit -1;
}
system ("echo \"... Finished cleaning out generated files from the ${oldrelname} installation.\" >>${finish_upgrade_logfile}");
print "... Finished cleaning out generated files from the ${oldrelname} installation.\n";

# Run $finish_oldrelclean_name to clean out other files that are obsolete or 
#  moved or renamed in ${newrelname}
print "\n";
print "Launching script to clean out files gone, moved, or renamed in ${newrelname}:\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Launching script to clean out files gone, moved, or renamed in ${newrelname}\" >>${finish_upgrade_logfile}");
system ("echo \"(A line reporting that  ${finish_oldrelclean_name}  finished\" >>${finish_upgrade_logfile}");
system ("echo \"       should follow:)\" >>${finish_upgrade_logfile}");
require $finish_oldrelclean_name;
system ("echo \"... Finished  ${finish_oldrelclean_name}.\" >>${finish_upgrade_logfile}");


# Ready to unpack the new distribution

# The $newreldontuntarfilename file should be a gzipped tar file; 
#  unpack it into this directory

print "\n";
print "Begin unpacking the new JGOFS ${newrelname} distribution ...\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Begin unpacking the new JGOFS ${newrelname} distribution ...\" >>${finish_upgrade_logfile}");
system ("echo \"tar xfvzp ${newreldontuntarfilename} >>${finish_upgrade_logfile} 2>&1\" >>${finish_upgrade_logfile}");
# [THE FOLLOWING COMMENT AND COMMENTED-OUT "tar" COMMAND ARE FROM 
#  oo2008_initial_script.pl; BUT NOW I DON'T FIND WHAT IT SAYS TO BE 
#  TRUE, SO I AM REDIRECTING stderr HERE.]
### I don't redirect stderr in the following system call, because it doesn't 
###  work, and it causes the value returned by the system call to be 0 
###  instead of 512 when $newreldontuntarfilename isn't a gzipped tar file
##if (system ("tar xfvzp ${newreldontuntarfilename} >>${finish_upgrade_logfile}")) {
if (system ("tar xfvzp ${newreldontuntarfilename} >>${finish_upgrade_logfile} 2>&1")) {
    system ("echo \"ERROR:  Unpacking  ${newreldontuntarfilename}  failed.\" >>${finish_upgrade_logfile}");
    system ("echo \" It should be a gzipped tar file; there was a problem untarring it.\" >>${finish_upgrade_logfile}");
    print STDERR 
	"ERROR:  Unpacking  ${newreldontuntarfilename}  failed.\n";
    print STDERR
	" It should be a gzipped tar file; there was a problem untarring it.\n";
    exit -1;
}
system ("echo \"... Finished unpacking the new JGOFS ${newrelname} distribution.\" >>${finish_upgrade_logfile}");
    print "... Finished unpacking the new JGOFS ${newrelname} distribution.\n";

# Since the installer will need to merge library customizations (if any) 
#  after this script finishes, we'll copy src/lib/_makefile_.jgofs to 
#  src/lib/makefile.jgofs now, freeing the user from the need 
#  to do anything with it if there are no customizations
print "\n";
print "Installing  src/lib/makefile.jgofs  ...\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Install  src/lib/makefile.jgofs:\" >>${finish_upgrade_logfile}");
unless (-e "src/lib/_makefile_.jgofs") {
    system ("echo \"ERROR IN DISTRIBUTION:  src/lib/_makefile_.jgofs  not found.\" >>${finish_upgrade_logfile}");
    print STDERR 
	"ERROR IN DISTRIBUTION:  src/lib/_makefile_.jgofs  not found.\n";
    exit -1;
}
system ("echo \"cp -p  src/lib/_makefile_.jgofs  src/lib/makefile.jgofs\" >>${finish_upgrade_logfile}");
if (system ("cp -p  src/lib/_makefile_.jgofs  src/lib/makefile.jgofs")) {
    system ("echo \"ERROR:  Copying  src/lib/_makefile_.jgofs  to  src/lib/makefile.jgofs  failed.\" >>${finish_upgrade_logfile}");
    print STDERR "ERROR:  Copying  src/lib/_makefile_.jgofs\n";
    print STDERR "             to  src/lib/makefile.jgofs  failed.\n";
    exit -1;
}
system ("echo \"Copied  src/lib/_makefile_.jgofs  to  src/lib/makefile.jgofs\" >>${finish_upgrade_logfile}");
print "... Finished installing  src/lib/makefile.jgofs.\n";

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
## THIS SECTION WAS WRITTEN BEFORE WE ADDED AN UNCONFIGURED  build-env.pl  
##  TO THE RELEASE (WITH DEFAULTS SET DIFFERENTLY THAN IN  _build-env_.pl); 
##  IN THE PROCEDURE PROVIDED WITH THE RELEASE, THERE IS NO DANGER OF 
##  CLOBBERING THE OLD  build-env.pl, SO THERE'S NO NEED TO USE THE 
##  PROTECTIVE NAME.
## COMMENT OUT THE FOLLOWING SECTION IF $newrelname IS LESS THAN "V2.1":
## Since the installer will need to merge  build-env.pl  site customizations 
##  after this script finishes, and we've already protected the user from 
##  losing the old site customizations (by saving the old build-env.pl, both 
##  in the full system backup and in a convenience copy), we'll copy 
##  _build-env_.pl  to  build-env.pl  now, freeing the user from the need 
##  to rename it after doing the needed customizations
##print "\n";
##print "Installing  build-env.pl  ...\n";
##system ("echo \"\" >>${finish_upgrade_logfile}");
##system ("echo \"Install  build-env.pl:\" >>${finish_upgrade_logfile}");
##unless (-e "_build-env_.pl") {
##    system ("echo \"ERROR IN DISTRIBUTION:  _build-env_.pl  not found.\" >>${finish_upgrade_logfile}");
##    print STDERR 
##	"ERROR IN DISTRIBUTION:  _build-env_.pl  not found.\n";
##    exit -1;
##}
##system ("echo \"cp -p  _build-env_.pl  build-env.pl\" >>${finish_upgrade_logfile}");
##if (system ("cp -p  _build-env_.pl  build-env.pl")) {
##    system ("echo \"ERROR:  Copying  _build-env_.pl  to  build-env.pl  failed.\" >>${finish_upgrade_logfile}");
##    print STDERR "ERROR:  Copying  _build-env_.pl\n";
##    print STDERR "             to  build-env.pl  failed.\n";
##    exit -1;
##}
##system ("echo \"Copied  _build-env_.pl  to  build-env.pl\" >>${finish_upgrade_logfile}");
##print "... Finished installing  build-env.pl.\n";
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


# Remove no-longer-needed files unpacked from  
#  $outertarfilename, $newrelspecialwrap1name, and  $newrelspecialwrap2name
print "\n";
print "Begin removing no-longer-needed files used for upgrading ...\n";
system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"Begin removing no-longer-needed files used for upgrading ...\" >>${finish_upgrade_logfile}");
system ("echo \"rm ${finish_oldrelclean_name}\" >>${finish_upgrade_logfile}");
unlink ($finish_oldrelclean_name);
system ("echo \"rm ${newrelspecialwrap2name}\" >>${finish_upgrade_logfile}");
unlink (${newrelspecialwrap2name});
system ("echo \"rm ${newreldontuntarfilename}\" >>${finish_upgrade_logfile}");
unlink ($newreldontuntarfilename);
system ("echo \"... Finished removing no-longer-needed files used for upgrading.\" >>${finish_upgrade_logfile}");
print "... Finished removing no-longer-needed files used for upgrading.\n";


system ("echo \"\" >>${finish_upgrade_logfile}");
system ("echo \"...  ${thisscriptname}  is done.\" >>${finish_upgrade_logfile}");

# That's all for upgrade script 2; 
# now the installer has to do site customizations
print "\n";
print "...  ${thisscriptname}  is done.\n\n";
print "Now check  ${finish_upgrade_logfile}  for\n";
print "  any problems, referring to the explanations in step (2) in\n";
print "  file  README_${newreleasetag}_INSTALL_UPGRADE.\n";
print "Then continue the upgrade with step (3) (customization)\n";
print "  in file  README_${newreleasetag}_INSTALL_UPGRADE.\n\n";

exit 0;



sub print_usage_err_msg
{
    print STDERR "\n";
    print STDERR "ERROR:  The ${releasetag} upgrade script 2 must run in a\n";
    print STDERR " directory that contains the files of the root of an\n";
    print STDERR " existing jgofs-${oldrelname} installation, and\n";
    print STDERR " files unpacked by running the ${newreleasetag} initial script\n";
    print STDERR " in the same directory.\n";
}


sub print_distrib_err_msg
{
    print STDERR "\n";
    print STDERR "ERROR IN DISTRIBUTION:\n";
    print STDERR " File  ${newrelspecialwrap2name}  must be a gzipped tar file\n";
    print STDERR " which contains files  ${finish_oldrelclean_name}\n";
    print STDERR " and  ${remove_newrel_restore_oldrel_name}; it isn't.\n";
}
