#! /bin/csh -f

#
#  This utility is the fifth system 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 system directories by un-taring
#      the system5.files
#
#	    (tries running one of the two commands in the root
#	     KHOROS_HOME directory)
#
#      % zcat patch_directory/system5.files.Z | tar -xvfo -
#
#  2) fix a problem with repos/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 fifth fix for the Khoros 1.0 software.  In order for"
echo "this program to work it must have write access to the khoros system"
echo "directoy.  And 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 fifth fix."
   echo ""
   exit 1
endif

cd $KHOROS_HOME

#
#
if (! -e $KHOROS_HOME/repos/PatchLevel) then
   echo ""
   echo "Error!  We were un-able to find the \$KHOROS_HOME/repos/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/repos/PatchLevel
grep "Khoros 1.0 System Patch 4" $KHOROS_HOME/repos/PatchLevel > /dev/null
if ($status != 0) then
   echo ""
   echo "Error! It appears that you are NOT patching Khoros 1.0 Patch 4"
   echo "This shell script is for applying Patch 5, but you must be applying"
   echo "it to Khoros 1.0 with Patch 4.  Your PatchLevel file indicates the"
   echo "following:"
   more $KHOROS_HOME/repos/PatchLevel
   echo ""
   exit 1
endif

#
#  Warn the user about listing the output in the system5.listing
#
echo ""
echo "Ready to extract the patch files from the system tarfiles.  This"
echo "listing will be redirected to 'system5.list' and will contain"
echo "the name of every changed file between patch 4 and patch 5."
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 delete file exists.  If so then delete the files
#  from the Khoros 1.0 Patch 4 dirstribution
#
echo ""
echo -n "deleting obsolete files as of Khoros 1.0 Patch 5 files..... "
if (! -e $patch_dir/system5.delete) then
   echo ""
   echo "Error!  Unable to find the system delete file which contains the"
   echo "list of files that have been deleted between Patch 4 and Patch 5."
   echo ""
   exit 1
else
   \rm -rf `cat $patch_dir/system5.delete`
endif

echo "done."
echo ""

#
#  Second 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/system5.files.Z || -e $patch_dir/system5.files) then
   if (-e $patch_dir/system5.files) then
      tar -xvfo $patch_dir/system5.files >& $patch_dir/system5.list
      set return_stat = $status
   else
      zcat $patch_dir/system5.files.Z | tar xvfo - >& $patch_dir/system5.list
      set return_stat = $status
   endif
else
   echo ""
   echo "Error!  Unable to find the system tar file in which to extract the"
   echo "Khoros patch 5 files"
   echo ""
   exit 1
endif

echo "done."
echo ""

#
#  installit needs to be executable
\chmod u+x src/installit

if ($return_stat != 0) then
   echo "possible error."
   echo ""
   echo "The return status from the patch 5 file extraction indicates that a"
   echo "possible error has occurred.  Please check the 'system5.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 "applying the source patch or re-get the Khoros binaries from the"
   echo "appropriate ftp site."
   exit 1
else
   echo "extraction complete."
   echo ""
   echo "The return status from the patch 5 file extraction indicates that a"
   echo "the files were succesfully extracted.  Please continue with the next"
   echo "step of the patch.  Which is to either apply the source patch or to"
   echo "re-get the Khoros binaries from the appropriate ftp site."
   echo ""
endif
