#!/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".)


$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 new release tag from the beginning of the script's name 
#  (before first '_')
@thisscriptnamefields = split (/_/, $thisscriptname);
$v200releasetag = $thisscriptnamefields[0];
$v200releasetaglc = length $v200releasetag;

# Set the terminating substring identifying the gzipped tar files 
#  containing the actual V2.00 distribution and the V1.73 distribution
$dontuntarfilestring = "dontuntar_file";
$dontuntarfilestringlc = length $dontuntarfilestring;

# Set the terminating substring identifying the gzipped tar file 
#  containing the extra files needed if this is an upgrade
$specialwrap1string = "specialwrap1";
$specialwrap1stringlc = length $specialwrap1string;

# Set up names of the two files that this script requires 
#  to be unpacked from the "specialwrap1" file if preparing for an upgrade
$v173releasetag = "jgofs-V1.73";
$v173dontuntarfilename = $v173releasetag . "_$dontuntarfilestring";
$getv173sitediffsname = "get_v173_site_diffs.pl";

# Set up names for "upgrade prep" and "fresh install" log files 
#  (only one of which will be used)
$upgrade_prep_logfile = "log_$v200releasetag" . "_1upgrade_prep_$timestamp";
$fresh_install_logfile = "log_$v200releasetag" . "_1install_$timestamp";

# Set up name of the V1.73 installation backup gzipped tar file.
$v173backupfilename = $servername . "_$v173releasetag" . 
						"_backup_$timestamp.tz";

# Set up name for temp directory for uncustomized V1.73 distribution, 
#  for use in upgrade preparation
$jgofs_v173_dist_root = "/tmp/$v173releasetag" . "_$timestamp";

# Set up name for site customization diffs file, for use in upgrade 
#  preparation
$diffsfilename = $servername . "_V1.73diffs.output";

# Set up name for root directory under which to save convenience copies 
#  of customized files from installed V1.73 data server
$customcopyrootname = "../$servername" . "_V1.73customcopyroot";


print "Begin  $thisscriptname  ...\n";
print "\n";
print "Begin checking this directory and the V2.00 distribution ...\n";

# Make a list of files in this directory
@filesindir = `/bin/ls -A`;
chomp (@filesindir);

# Find any .tar files
foreach (@filesindir) {
    @filenamefields = split (/\./);
    if ($filenamefields[$#filenamefields] eq "tar") {
	push (@dottarfilesindir, $_);
    }
}

# Check each .tar file for this script; there must be exactly one
$ndottarfileswiththisscript = 0;
foreach $dottarfile (@dottarfilesindir) {
    @dottarfilesls = `tar tf $dottarfile | cut -f 2 -d'/'`;
    chomp (@dottarfilesls);
    if ($dottarfilesls[0] eq "") {
	shift @dottarfilesls;
    }
    foreach $fileindottarfile (@dottarfilesls) {
	if ($fileindottarfile eq $thisscriptname) {
	    ++$ndottarfileswiththisscript;
	    $outertarfilename = $dottarfile;
	    last;
	}
    }
}
if ($ndottarfileswiththisscript != 1) {
    &print_usage_err_msg;
    exit -1;
}

# Check that the name of the .tar file containing this script starts with 
#  the same string ($v200releasetag) as this script's name
if (substr ($outertarfilename, 0, $v200releasetaglc) ne $v200releasetag) {
    print STDERR "\nERROR:  The tar file containing  $thisscriptname\n";
    print STDERR " is named  $outertarfilename; its name should begin \"$v200releasetag\";\n";
    print STDERR " either the tar file has the wrong content or it has been renamed;\n";
    print STDERR " you should resolve this problem and start over.\n";
    exit -1;
}

# Create list of all files that should have been unpacked from the .tar file 
#  containing this script, and add the name of that .tar file to the list
@outertarfilels = `tar tf $outertarfilename | cut -f 2 -d'/'`;
chomp (@outertarfilels);
if ($outertarfilels[0] eq "") {
    shift @outertarfilels;
}
@newdistfilenames = (@outertarfilels, $outertarfilename);

# Now check that we have: 
#  the tar file (whose name must start with $v200releasetag) 
#        that contains this script, 
#  and the files unpacked from it, 
#    exactly one of whose names ends with $dontuntarfilestring 
#        and starts with $v200releasetag, 
#    and exactly one of whose names ends with $specialwrap1string 
#        and starts with $v200releasetag (this check done only if upgrading);
#  and either:
#    - no other files [fresh install], or 
#    - additional files, including both "build" and "build-env" [upgrade]

$nnewdistfiles = 0;
$nnewdistdontuntarfiles = 0;
$nnewdistspecialwrap1s = 0;
$nnotnewdistfiles = 0;
foreach $fileindir (@filesindir) {
    $isanewdistfilename = 0;
    foreach $newdistfilename (@newdistfilenames) {
	if ($fileindir eq $newdistfilename) {
	    $isanewdistfilename = 1;
	    ++$nnewdistfiles;
	    if (substr ($newdistfilename, -$dontuntarfilestringlc, 
			$dontuntarfilestringlc) eq $dontuntarfilestring) {
	        $v200dontuntarfilename = $newdistfilename;
		++$nnewdistdontuntarfiles;
	    } elsif (substr ($newdistfilename, -$specialwrap1stringlc, 
			     $specialwrap1stringlc) eq $specialwrap1string) {
	        $v200specialwrap1name = $newdistfilename;
		++$nnewdistspecialwrap1s;
	    }
	    last;
	}
    }
    unless ($isanewdistfilename) {
	++$nnotnewdistfiles;
	@notnewdistfilenames = (@notnewdistfilenames, $_);
    }
}
if ($nnewdistfiles != $#newdistfilenames + 1) {
    # Not everything from the new install/upgrade distribution is present 
    #  in this directory
    &print_usage_err_msg;
    exit -1;
}

if ($nnewdistdontuntarfiles != 1) {
    # Either the new distribution's "dontuntar_file" is missing 
    #  or there's more than one
    &print_distrib_err_msg;
    &offer_clean_up_and_quit;
}

# Check that  $v200dontuntarfilename  starts with 
#  the same string ($v200releasetag) as this script's name
if (substr ($v200dontuntarfilename, 0, $v200releasetaglc) ne 
							$v200releasetag) {
    &print_distrib_err_msg;
    &offer_clean_up_and_quit;
}


# Determine whether upgrade or fresh install
if ($nnotnewdistfiles > 0) {

    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    # This directory contains files in addition to the V2.00 install/upgrade 
    #  .tar file and the files unpacked from it, so this should be the root 
    #  directory of an existing V1.73 installation.
    # Check that files "build" and "build-env" are here and, if they are, 
    #  prepare for the upgrade.
    # (For the case of no files in the directory except the V2.00 
    #  install/upgrade files, see below, following a comment marked off like 
    #  this one.)
    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Continue checking the consistency and integrity of the contents 
    #  of $outertarfilename, checking files needed in preparing to upgrade

    if ($nnewdistspecialwrap1s != 1) {
	# Either the new distribution's "specialwrap1" file is missing 
	#  or there's more than one
	&print_distrib_err_msg;
	&offer_clean_up_and_quit;
    }

    # Check that  $v200specialwrap1name  starts with 
    #  the same string ($v200releasetag) as this script's name
    if (substr ($v200specialwrap1name, 0, $v200releasetaglc) ne 
							$v200releasetag) {
	&print_distrib_err_msg;
	&offer_clean_up_and_quit;
    }


    # Check that this looks like the root directory of a V1.73 data server
    unless (-e "build" && -e "build-env") {
        # Not a fresh directory, but "build" and/or "build-env" not found
	&print_usage_err_msg;
	&offer_clean_up_and_quit;
    }


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

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

    # Check that the  $v200specialwrap1name  file contains files 
    #  $v173dontuntarfilename  and  $getv173sitediffsname .
    # (It should also contain  jgofs-V2.00_upgrade_script2.pl  and  
    #  jgofs-V2.00_specialwrap2, but we'll leave concern with them for the 
    #  second stage of the upgrade procedure.)

    $foundrequiredfiles = 0;
    $foundv173dontuntarfilename = 0;
    $foundgetv173sitediffsname = 0;
    foreach $fileinv200specialwrap1 (@v200specialwrap1ls) {
	unless ($foundv173dontuntarfilename) {
	    if ($fileinv200specialwrap1 eq $v173dontuntarfilename) {
		$foundv173dontuntarfilename = 1;
		next;
	    }
	}
	unless ($foundgetv173sitediffsname) {
	    if ($fileinv200specialwrap1 eq $getv173sitediffsname) {
		$foundgetv173sitediffsname = 1;
		next;
	    }
	}
    } continue {
	if ($foundv173dontuntarfilename && $foundgetv173sitediffsname) {
	    $foundrequiredfiles = 1;
	    last;
	}
    }
    unless ($foundrequiredfiles) {
	&print_distrib_err_msg;
	&offer_clean_up_and_quit;
    }

    print "... This directory looks like the root of a JGOFS V1.73 data server,\n";
    print "       and the V2.00 distribution looks OK.\n";


    # Start logging, in $upgrade_prep_logfile
    system ("echo \"Begin  $thisscriptname:\" >$upgrade_prep_logfile");
    system ("echo \"       Preparing to upgrade from $v173releasetag ...\" >>$upgrade_prep_logfile");


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


    # Back up (tar) existing installation
    print "\n";
    print "Begin backing up existing V1.73 installation ...\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Begin backing up existing V1.73 installation\" >>$upgrade_prep_logfile");
    system ("echo \"       to  ../$v173backupfilename  ...\" >>$upgrade_prep_logfile");
    system ("echo \"tar cfvz  ../$v173backupfilename  ./build  ./build-env  ./INSTALL.txt  ./README  ./bin  ./htmlbin  ./methods  ./objects  ./src  >>$upgrade_prep_logfile 2>&1\" >>$upgrade_prep_logfile");
    if (system ("tar cfvz  ../$v173backupfilename  ./build  ./build-env  ./INSTALL.txt  ./README  ./bin  ./htmlbin  ./methods  ./objects  ./src  >>$upgrade_prep_logfile 2>&1")) {
	system ("echo \"ERROR:  Creating  ../$v173backupfilename  failed.\" >>$upgrade_prep_logfile");
	print STDERR "ERROR:  Creating  ../$v173backupfilename  failed.\n";
	exit -1;
    }
    system ("echo \"... Finished backing up existing V1.73 installation\" >>$upgrade_prep_logfile");
    system ("echo \"       to  ../$v173backupfilename.\" >>$upgrade_prep_logfile");
    print "... Finished backing up existing V1.73 installation\n";
    print "       to  ../$v173backupfilename.\n";

 
    # Create temp directory to unpack original V1.73 distribution 
    #  for comparison with the existing installation
    print "\n";
    print "Creating temp directory  $jgofs_v173_dist_root\n";
    print "       to unpack original V1.73 distribution.\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Create temp directory to unpack original V1.73 distribution:\" >>$upgrade_prep_logfile");
    system ("echo \"mkdir  $jgofs_v173_dist_root\" >>$upgrade_prep_logfile");
    unless (mkdir ($jgofs_v173_dist_root, 0755)) {
	system ("echo \"ERROR:  Can't create temp directory to unpack original V1.73 distribution.\" >>$upgrade_prep_logfile");
	print STDERR "ERROR:  Can't create temp directory to unpack original V1.73 distribution.\n";
	exit -1;
    }


    # Unpack the gzipped V1.73 distribution .tar file 
    #  into the temp directory created for it
    print "Begin unpacking original V1.73 distribution into temp directory ...\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Begin unpacking original V1.73 distribution into temp directory ...\" >>$upgrade_prep_logfile");
    system ("echo \"tar xfvzp $v173dontuntarfilename -C $jgofs_v173_dist_root >>$upgrade_prep_logfile 2>&1\" >>$upgrade_prep_logfile");
    if (system ("tar xfvzp $v173dontuntarfilename -C $jgofs_v173_dist_root >>$upgrade_prep_logfile 2>&1")) {
	system ("echo \"ERROR:  Unpacking  $v173dontuntarfilename  failed.\" >>$upgrade_prep_logfile");
	system ("echo \" It should be a gzipped tar file; there was a problem untarring it.\" >>$upgrade_prep_logfile");
	print STDERR 
	    "ERROR:  Unpacking  $v173dontuntarfilename  failed.\n";
	print STDERR
	    " It should be a gzipped tar file; there was a problem untarring it.\n";
	exit -1;
    }
    system ("echo \"... Finished unpacking original V1.73 distribution into temp directory.\" >>$upgrade_prep_logfile");
    print "... Finished unpacking original V1.73 distribution into temp directory.\n";

    # Use the  $getv173sitediffsname  script to generate the diffs file 
    #  showing local customizations of the existing V1.73 installation
    print "\n";
    print "Launching script to identify V1.73 site customizations:\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Launching script to identify V1.73 site customizations\" >>$upgrade_prep_logfile");
    system ("echo \"(A line reporting that  $getv173sitediffsname  finished should follow:)\" >>$upgrade_prep_logfile");
    @ARGV = ();
    require $getv173sitediffsname;
    system ("echo \"... Finished  $getv173sitediffsname;\" >>$upgrade_prep_logfile");
    system ("echo \"       see file  $diffsfilename  for the results.\" >>$upgrade_prep_logfile");

    # Clear out the temp  $jgofs_v173_dist_root tree
    print "\n";
    print "Begin removing the unpacked unmodified V1.73 distribution ...\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Begin removing the unpacked unmodified V1.73 distribution ...\" >>$upgrade_prep_logfile");
    system ("echo \"rm -rf $jgofs_v173_dist_root\" >>$upgrade_prep_logfile");
    system ("rm -rf $jgofs_v173_dist_root");
    system ("echo \"... Finished removing the unpacked unmodified V1.73 distribution.\" >>$upgrade_prep_logfile");
    print "... Finished removing the unpacked unmodified V1.73 distribution.\n";

    ### Make the already-used  $getv173sitediffsname  script non-executable
    ##system ("echo \"chmod 644 $getv173sitediffsname\" >>$upgrade_prep_logfile");
    ##chmod (0644, $getv173sitediffsname);


    # Remove no-longer-needed files unpacked from  
    #  $outertarfilename  and  $v200specialwrap1name
    print "\n";
    print "Begin removing no-longer-needed files used for upgrade preparation ...\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Begin removing no-longer-needed files used for upgrade preparation ...\" >>$upgrade_prep_logfile");
    system ("echo \"rm $getv173sitediffsname\" >>$upgrade_prep_logfile");
    unlink ($getv173sitediffsname);
    system ("echo \"rm $v173dontuntarfilename\" >>$upgrade_prep_logfile");
    unlink ($v173dontuntarfilename);
    system ("echo \"rm $v200specialwrap1name\" >>$upgrade_prep_logfile");
    unlink ($v200specialwrap1name);
    system ("echo \"... Finished removing no-longer-needed files used for upgrade preparation.\" >>$upgrade_prep_logfile");
    print "... Finished removing no-longer-needed files used for upgrade preparation.\n";


    # Scan the diffs file and save convenience copies of any files that were 
    #  customized

    print "\n";
    print "Begin making convenience copies of site-customized V1.73 files ...\n";
    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"Begin making convenience copies of site-customized V1.73 files ...\" >>$upgrade_prep_logfile");
    unless (open (DIFFSFILE, $diffsfilename)) {
	print "Can't open file  $diffsfilename  for input.\n";
	exit -1;
    }
    $maybe_a_customfile = 0;
    while (<DIFFSFILE>) {
	if (substr ($_, 0, 9) eq "******** ") {
	    chomp;
	    @inputlinefields = split (/ /);
	    $candidatefilepath = $inputlinefields[1];
	    $maybe_a_customfile = 1;
	} elsif ($_ eq "\n") {
	    $maybe_a_customfile = 0;
	} else {
	    if ($maybe_a_customfile) {
		chomp;
		# Save a copy of the file, which shows diffs
		@customfilepathfields = split (/\//, $candidatefilepath);
		$customfilename = pop (@customfilepathfields);
		$customfiledir = join ("/", @customfilepathfields);
		unless (-e "$customcopyrootname/$customfiledir") {
		    system ("mkdir -p $customcopyrootname/$customfiledir");
		}
		print "cp -p ./$candidatefilepath $customcopyrootname/$candidatefilepath\n";
		system ("echo \"cp -p ./$candidatefilepath $customcopyrootname/$candidatefilepath\" >>$upgrade_prep_logfile");
		system ("cp -p ./$candidatefilepath $customcopyrootname/$candidatefilepath");
		$maybe_a_customfile = 0;
	    }
	}
    }
    system ("echo \"... Finished making convenience copies of site-customized V1.73 files.\" >>$upgrade_prep_logfile");
    print "... Finished making convenience copies of site-customized V1.73 files.\n";


    system ("echo \"\" >>$upgrade_prep_logfile");
    system ("echo \"...  $thisscriptname  is done.\" >>$upgrade_prep_logfile");

    # That's all for the initial installation script; 
    # now the installer has to plan site customizations
    print "\n";
    print "...  $thisscriptname  is done.\n\n";
    print "Now check  $upgrade_prep_logfile  for\n";
    print "  any problems, and note the site customizations detailed in\n";
    print "  file  $diffsfilename  (referring to the explanations\n";
    print "  in step (1) in file  README_", $v200releasetag, "_INSTALL_UPGRADE).\n";
    print "Then continue the upgrade with step (2)\n";
    print "  in file  README_", $v200releasetag, "_INSTALL_UPGRADE.\n\n";


} else {

    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    # This directory contains no files except the V2.00 install/upgrade 
    #  .tar file and the files unpacked from it, so we will do a fresh 
    #  V2.00 install here.
    # (For the case of additional files found in the directory, see above, 
    #  following a comment marked off like this one.)
    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    print "... This directory looks OK for a new JGOFS V2.00 data server,\n";
    print "       and the V2.00 distribution looks OK.\n";

    system ("echo \"Begin  $thisscriptname:\" >$fresh_install_logfile");
    system ("echo \"       Creating a new $v200releasetag data server ...\" >>$fresh_install_logfile");

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

    print "\n";
    print "Begin unpacking the JGOFS V2.00 distribution ...\n";
    system ("echo \"\" >>$fresh_install_logfile");
    system ("echo \"Begin unpacking the JGOFS V2.00 distribution ...\" >>$fresh_install_logfile");
    system ("echo \"tar xfvzp $v200dontuntarfilename >>$fresh_install_logfile 2>&1\" >>$fresh_install_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 $v200dontuntarfilename isn't a gzipped tar file
    ##if (system ("tar xfvzp $v200dontuntarfilename >>$fresh_install_logfile")) {
    if (system ("tar xfvzp $v200dontuntarfilename >>$fresh_install_logfile 2>&1")) {
	system ("echo \"ERROR:  Unpacking  $v200dontuntarfilename  failed.\" >>$fresh_install_logfile");
	system ("echo \" It should be a gzipped tar file; there was a problem untarring it.\" >>$fresh_install_logfile");
	print STDERR 
	    "ERROR:  Unpacking  $v200dontuntarfilename  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 JGOFS V2.00 distribution.\" >>$fresh_install_logfile");
    print "... Finished unpacking the JGOFS V2.00 distribution.\n";

    # Since this is a fresh directory and 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 library customizations

    print "\n";
    print "Installing  src/lib/makefile.jgofs  ...\n";
    system ("echo \"\" >>$fresh_install_logfile");
    system ("echo \"Installing  src/lib/makefile.jgofs  ...\" >>$fresh_install_logfile");
    unless (-e "src/lib/_makefile_.jgofs") {
	system ("echo \"ERROR IN DISTRIBUTION:  src/lib/_makefile_.jgofs  not found.\" >>$fresh_install_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\" >>$fresh_install_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.\" >>$fresh_install_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.\" >>$fresh_install_logfile");
    print "... Finished installing  src/lib/makefile.jgofs.\n";


    # Remove unneeded/no-longer-needed files unpacked from $outertarfilename
    print "\n";
    print "Begin removing unneeded/no-longer-needed files\n";
    print "       unpacked from  $outertarfilename  ...\n";
    system ("echo \"\" >>$fresh_install_logfile");
    system ("echo \"Begin removing unneeded/no-longer-needed files\" >>$fresh_install_logfile");
    system ("echo \"       unpacked from  $outertarfilename  ...\" >>$fresh_install_logfile");
    system ("echo \"rm $v200specialwrap1name\" >>$fresh_install_logfile");
    unlink ($v200specialwrap1name);
    system ("echo \"rm $v200dontuntarfilename\" >>$fresh_install_logfile");
    unlink ($v200dontuntarfilename);
    system ("echo \"... Finished removing unneeded/no-longer-needed files\" >>$fresh_install_logfile");
    system ("echo \"       unpacked from  $outertarfilename.\" >>$fresh_install_logfile");
    print "... Finished removing unneeded/no-longer-needed files\n";
    print "       unpacked from  $outertarfilename.\n";


    system ("echo \"\" >>$fresh_install_logfile");
    system ("echo \"...  $thisscriptname  is done.\" >>$fresh_install_logfile");

    # That's all for the initial installation script; 
    # now the installer has to do site customizations
    print "\n";
    print "...  $thisscriptname  is done.\n\n";
    print "Now check file  $fresh_install_logfile  for\n";
    print "  any problems.  Then, if all is well, continue the installation\n";
    print "  with step (2) in file README_", $v200releasetag, "_INSTALL_NEW.\n\n";
}


exit 0;



sub print_usage_err_msg
{
    print STDERR "\n";
    print STDERR "ERROR:  The $v200releasetag initial script must run in a\n";
    print STDERR " directory that contains a single copy of the main\n";
    print STDERR " $v200releasetag tar file; the files unpacked from it; and\n";
    print STDERR " EITHER\n";
    print STDERR "   no other files (for a new installation)\n";
    print STDERR " OR\n";
    print STDERR "   the files of the root of an existing V1.73 installation\n";
    print STDERR "   (to upgrade to V2.00).\n";
    print STDERR "\n";
    print STDERR "You should put the main $v200releasetag tar file into\n";
    print STDERR " either a newly-created directory\n";
    print STDERR " or the root directory of an existing V1.73 installation;\n";
    print STDERR " then unpack the $v200releasetag tar file there, and\n";
    print STDERR " follow the directions in the  README_", $v200releasetag, "_INSTALL  file.\n";
}


sub print_distrib_err_msg
{
    print STDERR "\n";
    print STDERR "ERROR IN DISTRIBUTION:\n";
    print STDERR " The main $v200releasetag tar file contents must include:\n";
    print STDERR "   - File  $thisscriptname  (this script);\n";
    print STDERR "   - File  $v200releasetag", "_$dontuntarfilestring,\n";
    print STDERR "       and no other file whose name ends in \"$dontuntarfilestring\";\n";
    print STDERR "   - File  $v200releasetag", "_$specialwrap1string,\n";
    print STDERR "       and no other file whose name ends in \"$specialwrap1string\";\n";
    print STDERR "      it must be a gzipped tar file whose contents include files\n";
    print STDERR "       $v173dontuntarfilename  and  $getv173sitediffsname.\n";
    print STDERR " The main $v200releasetag tar file here doesn't meet these conditions.\n";
}


sub offer_clean_up_and_quit
{
    print "\n";
    print "Ready to clean out files unpacked from $outertarfilename;";
    do {
	print " proceed? (y/n) ";
	$reply = substr (<STDIN>, 0, 1);
    } until ($reply eq "y" || $reply eq "Y" || $reply eq "n" || $reply eq "N");
    if ($reply eq "y" || $reply eq "Y") {
	foreach (@outertarfilels) {
	    print "    Removing $_ ...\n";
	    unlink;
	}
	print "Done.\n";
    }
    exit 1;
}
