#!/usr/bin/sh

if [ "`whoami`" != "root" ] ; then
        echo "Must be root to run this patch script."
        exit 1
fi

Scripts="do_config_vhost.tcl do_config_report.tcl config_location.tcl"
Suffix=".OSIS5x-patched"
for Script in $Scripts; do
   Source="$Script$Suffix"
   if [ ! -f $Source ] ; then
      echo "Can't find $Source"
      exit 1
   fi
done

ValidSubsets=IAFWAPCH5[01]0
if [ ! -f /usr/.smdb./$ValidSubsets.lk ] ; then
   echo "This patch can only be applied to the Open Source Internet Solutions V5.0"
   echo "or V5.1 Apache subset (IAFWAPCH500 or IAFWAPCH510) which do not appear"
   echo "to be installed."
   exit 1
fi

TargetDir=/usr/internet/apache/port-8089/cgi-bin
PatchScripts=
for Script in $Scripts; do
   Target=$TargetDir/$Script
   Source="$Script$Suffix"
   if [ -f $Target ]; then
      if [ "`cksum $Target | cut -f-2 -d\ `" = "`cksum $Source | cut -f-2 -d\ `" ]; then
         continue
      fi
   fi
   PatchScripts="$PatchScripts $Script"
done
if [ "$PatchScripts" = "" ]; then
   echo "This patch has already been applied."
   exit 1
fi

V=`ls /usr/.smdb./$ValidSubsets.lk | tail -1 | sed -e "s/^[^0-9]*\([0-9]*\).lk/\1/" -e "s/0$//"`
Version=`echo $V | sed -e "s/^\(.\)/\1\./"`
echo "Applying Open Source Internet Solutions V$Version Apache Administration patch"

for Script in $PatchScripts; do
   Target=$TargetDir/$Script
   Source="$Script$Suffix"
   mv $Target $Target.OSIS$V-orig
   cp $Source $Target
   chmod 700 $Target
done

echo "Open Source Internet Solutions V$Version Apache Administration patch applied successfully"
