#! /bin/csh -f

#
#  This utility is the second source patch for Khoros 1.0
#
#  Although this script is rather long what it is attempting to do is
#  the following two things:
#
#  1)  apply the fixes to the Khoros source directories by un-taring
#      the source2.files
#
#	    (tries running one of the two commands in the root
#	     KHOROS_HOME directory)
#
#      % zcat patch_directory/source2.files.Z | tar -xvfo -
#
#  2) fix a problem with src/PatchLevel since this file was released with
#     read-only permissions.
#

#
#  Get the current path so that we know where the patch files and programs
#  are.
#
set patch_dir = `pwd`
set path = ($patch_dir $path)
set program = `basename $0`

if (! -e "$program") then
echo ""
echo "Error!  Please run $program in the directory in which it resides.  In"
echo "order for this utility to work it needs access to the other files in"
echo "that directory"
echo ""
exit 1
endif

echo ""
echo "$program is the second fix for the Khoros 1.0 software.  In order for"
echo "this program to work it must have write access to the khoros source"
echo "directoy.  Also you must have your KHOROS_HOME variable defined."
echo ""
echo -n "Do you wish to continue? (Y/N).... "

set ans = $<
if ("$ans" != "Y" && "$ans" != "y") then
   exit 1
endif

#
# Check environment variable
#----------------------------------------------------
if (! $?KHOROS_HOME) then
   echo ""
   echo "Error! It appears that you currently don't have your KHOROS_HOME"
   echo "environment variable set.  Please set this and re-run this program"
   echo ""
   exit 1
endif

if (! -w "$KHOROS_HOME") then
   echo ""
   echo "Error! It appears that you have your KHOROS_HOME environment variable"
   echo "set, but you don't appear to have write permission to access the"
   echo "khoros directory ($KHOROS_HOME).  Please make sure you have write"
   echo "permission so that we can install the second fix."
   echo ""
   exit 1
endif

cd $KHOROS_HOME

#
#
if (! -e $KHOROS_HOME/src/PatchLevel) then
   echo ""
   echo "Error!  We were un-able to find the \$KHOROS_HOME/src/PatchLevel"
   echo "file.  This file is used to indicate the current level of patch for"
   echo "Khoros 1.0.  Is this Khoros 1.0 without any previous fixes?  Please"
   echo "fix this and re-run this script"
   exit 1
endif

#
#  fix the problem that the PatchLevel maynot be writable
#
chmod u+w $KHOROS_HOME/src/PatchLevel

grep "Khoros 1.0 Source Patch 1" $KHOROS_HOME/src/PatchLevel > /dev/null
if ($status != 0) then
   echo ""
   echo "Error! It appears that you are NOT patching Khoros 1.0 Patch 1"
   echo "This shell script is for applying Patch 2, but you must be applying"
   echo "it to Khoros 1.0 with Patch 1.  Your PatchLevel file indicates the"
   echo "following:"
   more $KHOROS_HOME/src/PatchLevel
   echo ""
   exit 1
endif

#
#  Warn the user about listing the output in the source2.listing
#
echo ""
echo "Ready to extract the patch files from the source tarfiles.  This"
echo "listing will be redirected to 'source2.list' and will contain"
echo "the name of every changed file between patch 1 and patch 2."
echo ""
echo "	Warning!  This will take a several minutes so please be patient."
echo ""
echo -n "Do you want to continue? (Y/N)...."

set ans = $<
if ("$ans" != "Y" && "$ans" != "y") then
   exit 1
endif

#
#  First check to see if the tar file exists.  If so then un-tar the patched
#  files.  Otherwise error out....
#
echo ""
echo -n "extracting files..... "
if (-e $patch_dir/source2.files.Z || -e $patch_dir/source2.files.Z) then
   if (-e $patch_dir/source2.files) then
      tar -xvfo $patch_dir/source2.files >& $patch_dir/source2.list
      set return_stat = $status
   else
      zcat $patch_dir/source2.files.Z | tar xvfo - >& $patch_dir/source2.list
      set return_stat = $status
   endif
else
   echo ""
   echo "Error!  Unable to find the source tar file in which to extract the"
   echo "Khoros patch 2 files"
   echo ""
   exit 1
endif

echo "done."
echo ""
if ($return_stat != 0) then
   echo "possible error."
   echo ""
   echo "The return status from the patch 2 file extraction indicates that a"
   echo "possible error has occurred.  Please check the 'source2.list' file"
   echo "and fix the problem and re-run this program.  If this is not an error"
   echo "but simply a bogus return value from tar 'then', please continue with"
   echo "re-compiling Khoros.  This can be done by changing directory to"
   echo "$KHOROS_HOME/src and running the 'installit' program."
   echo ""
   exit 1
else
   echo "extraction complete."
   echo ""
   echo "The return status from the patch 2 file extraction indicates that a"
   echo "the files were succesfully extracted.  Please continue with the next"
   echo "step of the patch.  Which is to re-compile Khoros; this can be done"
   echo "by changing directory to $KHOROS_HOME/src and running the 'installit'"
   echo "program."
   echo "
endif
