#!/bin/bash
#  $Id: gateway_patch_add.sh 145510 2017-07-03 08:55:25Z steven.durrans@oracle.com $
#  Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential

# Need to be root in order to add patches
u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi


TTAINSTDIR=${INSTDIR}

msgfile="${INSTDIR}/bin/scripts/en-us_patch"
[ -s "${msgfile}" ] && . ${msgfile}

PATCH_CFG=${TTAINSTDIR}/var/info/patch.cfg
PATCH_VERSION="PATCHED_VERSION"

GetSGDVersion() {
    TTAVERSION="${TTAINSTDIR}/etc/pkg/ttaversion"
    
    if [ ! -s "${TTAVERSION}" ]; then
	return
    fi
    
    SGD_VERSION=`tail -1 ${TTAVERSION}`
    echo "${SGD_VERSION}"
}


PATCH_LIST=${TTAINSTDIR}/var/info/patch.list
PATCH_LOG=${TTAINSTDIR}/var/log/patch.log
BACKUP_BIN_DIR=${TTAINSTDIR}/bin/bin/backups
BACKUP_JSERVER_DIR=${TTAINSTDIR}/bin/java/backups
BACKUP_WEBSERVER_DIR=${TTAINSTDIR}/webserver/backups
BACKUP_FILE_DIR=${TTAINSTDIR}/var/patchbackups
DELIM="|"
ACTIVE="active"
INACTIVE="inactive"

AddedPatch() {
   echo "$1 - Added patch : $2 $3" >> ${PATCH_LOG}
}

RemovedPatch() {
   echo "$1 - Removed patch : $2 $3" >> ${PATCH_LOG}
}

Check_sgd_running() {
    # ps output only gives limited characters in the command so truncate
    # the install directory if it's too long
    match=`echo $2 | cut -c1-25`

  # Count the number of lines in the file
  entries=`wc -l "$1" 2> /dev/null | awk '{print $1}'`
  if [ ! -s "$1" ]; then
      # Delete an empty file
      rm -f "$1" 2> /dev/null
      return 2;
  elif [ $entries -eq 1 ]; then
      ThePid=`tail -1 "$1" 2>/dev/null | sed 's/ //g'`
      if [ ! -n "${ThePid}" ]; then
          # No pid in the file
	  rm -f "$1" 2> /dev/null
	  return 1
      elif [ -n "`ps -fp${ThePid} 2>/dev/null | egrep \"$match\" | egrep -v egrep`" ] ; then
          # Single pid and the process is still running
	  return 0
      fi

      # The file exists but the process isn't one we expect
      rm -f "$1" 2> /dev/null
      return 1
  else
      # Multiple entries in the file - write all the pids for our processes
      # into a "running" file
      while read Pid ; do
         [ -n "`ps -fp${Pid} 2>/dev/null | egrep \"$match\" | egrep -v egrep`" ] && echo ${Pid} >> running
      done < $1

      if [ -s running ]; then
          # Move the "running" file back to the correct location
          mv -f running $1
          running_entries=`wc -l "$1" 2> /dev/null | awk '{print $1}'`
          ThePid=`tail -1 "$1" 2>/dev/null | sed 's/ //g'`
          if [ $running_entries -eq 1 ]; then
              # Single running process
	      return 0
          else
              # Multiple running processes
              return 3
          fi
      else
          rm -f "$1" 2> /dev/null
          return 1
      fi
  fi
}

ExitIfRunning() {
    DIR=$4

    Check_sgd_running $1 $2
    result=$?
    if [ ${result} -eq 0 -o ${result} -eq 3 ]; then
	echo "$3"
	cd ${TTAINSTDIR}/bin
	rm -rf ${DIR} 2> /dev/null
	exit 1
    fi
}

# Delete all the patches of the given type from the patch list
DeleteRedundantPatches () {
    [ -f "${PATCH_LIST}" ] || {
        return
    }

    TYPE=$1

    # Find and delete the redundant patches from the old version
    [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
    LIST=`grep "${DELIM}${TYPE}${DELIM}" ${PATCH_LIST} |
    ${AWK} -F "${DELIM}" -v version=${FILE_VERSION} '{
        if ($6 == version)
          print $1
    }' | tr ' ' '|'`

    if [ -n "${LIST}" ]; then
	egrep -v "${LIST}" ${PATCH_LIST} > ${TTAINSTDIR}/var/info/patch.list.tmp
	mv -f ${TTAINSTDIR}/var/info/patch.list.tmp ${PATCH_LIST}
    fi
}


# If the SGD server has been upgraded, delete any patches which have
# been superseded by the new version.
TidyUpAfterUpgrade() {
    # If patch config file doesn't exist there is nothing to do here.
    [ -f "${PATCH_CFG}" ] || {
        return;
    }
    
    FILE_VERSION=`grep "${PATCH_VERSION}" ${PATCH_CFG} 2> /dev/null | awk '{print $3}'`
    CURRENT_VERSION=`GetSGDVersion`

    case "${CURRENT_VERSION}" in
	${FILE_VERSION})
            return
	    ;;
	*)
	    DeleteRedundantPatches "binary" 
	    DeleteRedundantPatches "jserver"
	    DeleteRedundantPatches "webserver"
	    DeleteRedundantPatches "webcomp"
	    DeleteRedundantPatches "file"
	    ;;
    esac
}

# Modifies a entry in the patch list
# By default, it changes the status from active -> inactive.
# If a second parameter is supplied, it swaps entry for the new 
# entry without any other changes
UpdateEntry() {
    FROM_ENTRY=$1
    TO_ENTRY=$2

    if [ -z "${TO_ENTRY}" ]; then
	TO_ENTRY=`echo ${FROM_ENTRY} | sed -e 's/'${DELIM}${ACTIVE}${DELIM}'/'${DELIM}${INACTIVE}${DELIM}'/g'`
    fi

    grep -v "${FROM_ENTRY}" ${PATCH_LIST} > patch.list.tmp
    echo ${TO_ENTRY} >> patch.list.tmp
    mv -f patch.list.tmp ${PATCH_LIST}	
}

# Update the patch name for a patch command
UpdateRef() {
    COMMAND=$1
    NAME=$2
    PATCH_CFG=$3

    grep -v "^${COMMAND} :" ${PATCH_CFG} > patch.cfg.tmp
    mv -f patch.cfg.tmp ${PATCH_CFG}
    echo "${COMMAND} : ${NAME}" >> ${PATCH_CFG}
}

machinearch ()
{
#!/bin/sh
#  $Id: ixiarch.sh 23561 2012-05-15 08:22:43Z ww161634 $
#  Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Print canonical syscode for platform

# There are three valid types of command line:
#
# 1.     visionarch
# 2.     visionarch <available codes>...
# 3.	 visionarch [-c|-o]
#
# 1. The architecture is echoed
# 2. The best architecture from the list is returned
# 3. The -c and -o options return just the system Code or the OS type.

# 0) INITIALIZE VARIABLES

# Some of these are historical, but dont cause any harm
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$XDTHOME/bin:$PMWMHOME/bin:$IXIHOME/bin:$PATH


# 1) Arguments
numcodes=0
for i
do
  case $i in
    -c )
      codeonly=1
    ;;
  
    -o )
      osonly=1
    ;;

    -n )
      if [ -z "$codeonly" -a -z "$osonly" ]
      then
	  displayname=1	
      fi
    ;;
 
    * )
      checkcodes="$checkcodes $i"
      numcodes=`expr $numcodes + 1`
    ;;

   esac
done

# fast exit
if [ $numcodes = 1 ]
then
  echo $checkcodes
  exit 0
fi


# 2) MESSAGES
myname=`basename $0`

#  First define some messages.
#  They get overwritten by sourcing from a file, scripts.ll_TT,
#  so there's no need to translate them here.  

#  Messages for the visionarch script.
#  These follow text like "visionarch: !4! ", and may extend to several lines.
msg_xdtarch_no_uname=\
'No uname command found.'
msg_xdtarch_os_cpu=\
'Cannot recognize the Operating System or processor type.'
msg_xdtarch_ver_rel=\
'Cannot calculate version or release.'
msg_xdtarch_no_match=\
'Cannot find matching installed system.'

#  End of messages
# Try to find a language script
posslangfiles=""
for lang in C $IXILANG $PANLANG $LANG
do
  for pathname in $XDTHOME/bin/scripts. $PMWMHOME/bin/scripts. $IXIHOME/bin/scripts. ./scripts.
  do
    posslangfiles="$posslangfiles $pathname.$lang"
  done
done

# see if we can find one of those language files
for f in $posslangfiles
do
  if [ -f $f ]
  then
    . $scriptslangfile			# source it
    break
  fi						
done
  
# 3) TRY UNAME AND SET UP VARIABLES

unamev=`uname -v 2>/dev/null`
if [ $? -ne 0 ]
then
  # This isn't really a UNIX system is it?
  echo >&2  "$myname: !2! $msg_xdtarch_no_uname"
  exit 2
fi

U_SYSTEM=`uname -s 2>/dev/null`  || U_SYSTEM=UNKNOWN
U_RELEASE=`uname -r 2>/dev/null` || U_RELEASE=UNKNOWN
U_VERSION=`uname -v 2>/dev/null` || U_VERSION=UNKNOWN

# Some machines use -m arg to return 'model' rather
# than architecture.  For example, NCR SVR4 returns
# 3433,3434.  On such machines need to use -p which
# returns processor.

for MACHARG in m p
do
    U_MACHINE=`uname -"$MACHARG" 2>/dev/null` || U_MACHINE=UNKNOWN

    # 4)  FIND THE MACHINE
    # Note that we only look for cpu/os combinations our products currently
    # support - if you want better matching, most GNU software comes with
    # a script called 'config.guess' which does a more thorough job (and
    # has totally different output!)

    CPU= 			# Characters 1-2 of syscode
    OS= 			# Characters 3-4 of syscode
    VER=			# Digits 5-6 of syscode
    REL= 			# Digits 7-8 of syscode


    case ${U_MACHINE}::${U_SYSTEM}::${U_RELEASE}::${U_VERSION} in
      # Intel FreeBSD
      i*::FreeBSD::*:* )
        CPU=i3
        OS=fb
        NAME="FreeBSD (Intel)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # MacOS X
      powerpc::Darwin::*::*)
        CPU=pp
        OS=dw
        NAME="Darwin (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # MacOS X (Intel)
      i386::Darwin::*::*)
        CPU=i3
        OS=dw
        NAME="Darwin (Intel)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # PowerPC Linux
      ppc*::Linux::*::*)
        CPU=pp
        OS=li
        NAME="Linux (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # Intel Linux
      i*::Linux::*::* )
	CPU=i3
	OS=li
	NAME="Linux (Intel)"
	VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
	REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
      ;;
      # Athlon 64 Linux
      x86_64::Linux::*::* )
	CPU=i3
	OS=li
	NAME="Linux (AMD 64)"
	VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
	REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
      ;;
      # PowerPC Linux
      ppc*::Linux::*::* )
        CPU=pp
        OS=li
        NAME="Linux (PowerPC)"
        VER=`expr $U_RELEASE : '[^0-9]*\([0-9][0-9]*\)[^0-9]'`
        REL=`expr $U_RELEASE : '[^0-9]*[0-9][0-9]*[^0-9]\([0-9][0-9]*\)'`
       ;;
      # Sequent (Intel)
      i*::DYNIX*::*::* )
	CPU=i3
	OS=dy
	NAME="Sequent DYNIX/ptx (Intel)"
	VER=`expr $U_VERSION : 'V\([0-9]\).*'`
	REL=`expr $U_VERSION : 'V[0-9].\([0-9].[0-9]\)' | sed 's/\.//g'`
      ;;

      # The Sun family
      *::SunOS::4.*::* )
	CPU=sp
	OS=su
	VER=4
	NAME="Sun SunOS (Sparc)"
	REL=`expr $U_RELEASE : '4.\([^_]*\)' | sed 's/[^0-9]//g'`
      ;;
      sun*::SunOS::5.*::* )
	CPU=sp
	OS=so
	VER=5
	NAME="Sun Solaris (Sparc)"
	REL=`expr $U_RELEASE : '5.\([0-9][0-9]*\).*' | sed 's/[^0-9]//g'`
      ;;
      *86*::SunOS::5.*::* )
	CPU=i3
	OS=so
	VER=5
	NAME="Sun Solaris (Intel)"
	REL=`expr $U_RELEASE : '5.\(.*\)' | sed 's/[^0-9]//g'`
      ;;
      *::SunOS::5.*::* )
	CPU=sp
	OS=so
	VER=5
	NAME="Sun Solaris (Sparc)"
	REL=`expr $U_RELEASE : '5.\([0-9][0-9]*\).*' | sed 's/[^0-9]//g'`
      ;;

      # HP
      *::HP-UX::*::* )
	CPU=pa
	OS=hp
	NAME="Hewlett-Packard HP-UX (PA-RISC)"
	VER=`expr $U_RELEASE : '[^.]*.\([0-9]*\).*'`
	REL=`expr $U_RELEASE : '[^.]*.[^.]*.\([0-9]*\)'`
      ;;

      # Data General
      *::dgux::5.4*::* )
	CPU=m8
	OS=dg
	VER=54
	NAME="Data General DG/UX (Motorola)"
	REL=`expr $U_RELEASE : '5.4\(.*\)' | sed 's/[^0-9]//g' | sed 's/\(.\).*\(.\)$/\1\2/'`
      ;;

      # Data General on Intel
      *::dgux::R4*::* )
	case `uname -p`  in
	  m* )
	    CPU=m8 ;;
	  * )
	    CPU=i3 ;;
	esac
	OS=dg
	VER=04
	NAME="Data General DG/UX (Intel)"
	REL=`expr $U_RELEASE : 'R4.\(.*\)' | sed 's/[^0-9]//g'`
        REL=`echo $REL | sed 's/\(..\).*/\1/'`
      ;;

      # OSF/1
      alpha::OSF1::*::* )
	CPU=ax
	OS=o1
	NAME="DEC OSF/1 (Alpha)"
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # ULTRIX
      *::ULTRIX::*::* )
	CPU=mp
	OS=ul
	NAME="DEC Ultrix (MIPS)"
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # IRIX
      *::IRIX::4.*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;
      
      *::IRIX::5.1::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*.[^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Note
      # If we ever get minor release numbers of 5.2 we'll have to do
      # something different
      *::IRIX::5.2::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=20
      ;;
      # Note
      # This system does not exist yet
      *::IRIX::5.2.*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # catchall for other irix versions
      # 6.4 and later change from IRIX to IRIX64
      *::IRIX*::*::* )
	CPU=mp
	OS=ir
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # AIX
      # note that we can't figure out the patch level of a 3.2.x (x<4)
      # system (even IBM can't tell you immediately!)
      *::AIX::*::* )
	CPU=rs
	OS=ai
	NAME="IBM AIX (Risc)"
	VER=$U_VERSION
	REL=$U_RELEASE
      ;;

      # RISC OS (the mips variety, not acorn)
      # version 5 like output
      mips::*::*::RISCos )
	CPU=mp
	OS=ro
	VER=`echo $U_RELEASE | sed 's/\([^.]*\).*/\1/' | sed 's/[^0-9]//g'`
	REL=`echo $U_RELEASE | sed 's/[^.]*.\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;
      # version 4 like output
      mips::*::*::UMIPS )
	CPU=mp
	OS=ro
	VER=`echo $U_RELEASE | sed 's/\([0-9]*\).*/\1/'`
	REL=`echo $U_RELEASE | sed 's/[0-9]*\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Siemens Nixdorf mips R4000 (at least)
      R4000::*::*::* )
	CPU=mp
	OS=sx
	NAME="Siemens Nixdorf SINIX (MIPS)"
	VER=`echo $U_RELEASE | sed 's/\([0-9]*\).*/\1/'`
	REL=`echo $U_RELEASE | sed 's/[0-9]*\(.*\)/\1/' | sed 's/[^0-9]//g'`
      ;;

      # Motorola SVR4
      *88*::*::4*::* )
	CPU=m8
	OS=mo
	VER=`expr $U_VERSION : '[^V]*.\([^.]*\).*' | sed 's/[^0-9]//g'`
	REL=`expr $U_VERSION : '[^V]*.[^.]*.\(.*\)' | sed 's/[^0-9]//g'`
      ;;

      # windows nt with nutcracker
      *::Windows_NT::*::* )
	CPU=i3
	OS=wi
	VER="$U_RELEASE"
	REL="$U_VERSION"
      ;;

      DRS*::*::*::* )
	  PROC=`uname -p 2>/dev/null` || PROC=UNKNOWN
	  case "$PROC" in
	    sparc | SPARC )
	      CPU=sp
	      OS=nx
	      NAME="ICL DRS/NX (Sparc)"
	      ;;
	    i386 )
	      CPU=i386
	      OS=nx
	      NAME="ICL DRS/NX (Intel)"
	      ;;
	  esac

	  VER=`expr $U_VERSION : '\([0-9]\).*'`
	  REL=`expr $U_VERSION : '[0-9]..*\([0-9].[0-9]\)' | sed 's/\.//g'`
      ;;

      
      *::UnixWare::*::* )
	CPU=i3
	OS=un
	NAME="SCO UnixWare (Intel)"
	VER=`expr $U_VERSION : '\([0-9]\)'`
	REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
	if [ -z "$REL" ]; then
		REL=00
        elif [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] ; then
		REL="$REL"0
	fi
	;;

      *::OpenUNIX::*::* )
        CPU=i3
        OS=un
        NAME="SCO UnixWare (Intel)"
        VER=`expr $U_VERSION : '\([0-9]\)'`
        REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
        if [ -z "$REL" ]; then
                REL=00
        elif [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] ; then
                REL="$REL"0
        fi
        ;;

      # Various Intel based Unixen.  These seem particularly reluctant
      # to divulge their OS name, so we have to grep through
      # various system files to find the OS string

      *86*::*::*::* )
	if grep -s -i UnixWare /stand/boot 2>/dev/null 1>&2 || strings /stand/1/unix_mp 2>/dev/null | grep -s -i Unixware 2>/dev/null 1>&2
	then
          # It's some version of Unixware obviously
	  CPU=i3
	  OS=un
	  NAME="SCO UnixWare (Intel)"
          if expr $U_VERSION : 'D\([0-9]*.*\)' 2>/dev/null 1>&2
	  then
            # It's a date stamped version ; we dont yet know how to figure out
            # which so just pretend that it is unixware 212
            VER=02
            REL=12
          else
	    VER=`expr $U_VERSION : '\([0-9]\)'`
	    REL=`expr $U_VERSION : '[0-9].\(.*\)' | sed -e 's/\.//'`
	    if [ "x`expr $REL : '[0-9]\([0-9]\)'`" = "x" ] 
	    then
		REL="$REL"0
	    fi
          fi
	else
	  if [ "$U_SYSTEM" = "SCO_SV" ]
	  then
	    CPU=i3
	    OS=sc
	    VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	    NAME="SCO OpenServer (Intel)"
	    # just the release string ie: 3.2v5.0.0
	    relstr=`uname -X | grep Release | awk '{ print $3 }'`
	    # just the major SCO version after the SVR? (3.2v) bit
	    U_VERSION=`expr ${relstr} : '[0-9.]*v\([0-9]\)[0-9.]*'`
	    REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	  else
	    if grep -s 'The Santa Cruz Operation' /etc/gettydefs 2>/dev/null 1>&2
	    then
	      CPU=i3
	      OS=sc
	      NAME="SCO Unix (Intel)"
	      VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	      REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	    else
	      # DYNIX/ptx V2 doesn't return 'DYNIX' or 'ptx' in uname.
	      # This kludge may work, may not, it's either this
	      # or flip a coin!   
	      if grep -s 'ptx' /etc/versionlog 2>/dev/null 1>&2
	      then
		CPU=i3
		OS=dy
		NAME="Sequent DYNIX/ptx (Intel)"
		VER=`expr $U_VERSION : 'V\([0-9]\).*'`
		REL=`expr $U_VERSION : 'V[0-9].\([0-9].[0-9]\)' | sed 's/\.//g'`
	      else
	        # Assume OS must be System V Release 4.
	        CPU=i3
	        OS=sv
		NAME="System V Release 4 (Intel)"
	        VER=`expr $U_RELEASE : '^\([0-9]*\)'`
	        REL=`expr ${U_RELEASE}::${U_VERSION} : '[0-9]*\(.*\)' | sed 's/[^0-9]//g'`
	      fi
	    fi
	  fi
	fi
	;;	
    esac

    if [ -n "$CPU" -a -n "$OS" -a -n "$VER" -a -n "$REL" ]
    then
	break
    fi
done

# 5) Do sanity checking and cleanup
VER=`echo $VER | sed 's/[^0-9]//g'`
REL=`echo $REL | sed 's/[^0-9]//g'`

if [ -z "$CPU" -o -z "$OS" ]
then
  echo "$myname: !10! $msg_xdtarch_os_cpu"
  exit 10
fi

if [ -z "$VER" -o -z "$REL" ]
then
  echo "$myname: !11! $msg_xdtarch_ver_rel"
  exit 11
fi

# Add leading zeroes
REL=`echo 0$REL | sed 's/.*\(..\)/\1/'`
VER=`echo 0$VER | sed 's/.*\(..\)/\1/'`

# 6) So we now know it all

syscode=$CPU$OS$VER$REL

if  test -n "$codeonly"
then
    echo $CPU$OS
    exit 0
fi

if  test -n "$osonly"
then
    echo $VER$REL
    exit 0
fi

# 7) Check for codes given on the command line

if [ ! -z "$checkcodes" ]
then
    for i in $checkcodes
    do
	# Filter the input $checkcodes by matching CPU and OS, and strip
	# them off leaving just the VER and REL.  The tr command replaces
	# spaces by line-feeds, so that the following sort can do its stuff.
	posscodes=`echo $checkcodes | tr ' ' '\012' \
				| sed -ne "s/$CPU$OS//p" | sort -n`
	if [ -z "$posscodes" ]
	then				# None supplied for correct CPU+OS
	    echo >&2 "$myname: !5! $msg_xdtarch_no_match"
	    exit 5
	fi 

	# Some of them are the right platform.
	# The following algorithm returns the greatest possible code which
	# is older than this syscode, or, failing that, the least possible
	# code which is more recent than this one.

	for ver_rel in $posscodes
	do
	    if [ $VER$REL -eq $ver_rel ]
	    then 			# bingo we have this exact release
		echo $syscode
		exit 0
	    elif [ $VER$REL -gt $ver_rel ]
	    then 			# store this previous version
		priorcode=$CPU$OS$ver_rel
	    else 			# $VER$REL is < $ver_rel
		if [ -n "$priorcode" ]
		then
		    echo $priorcode
		    exit 0
		else
		    # Oh dear, we have no previous version so we should use
		    # the oldest newer version which is this current one ...
		    echo $CPU$OS$ver_rel
		    exit 0
		fi
	    fi
	done
    done
    if [ -n "$priorcode" ]
    then
        echo $priorcode
        exit 0
    fi
else
    if [ -n "$displayname" ]
    then
	echo "$NAME"
    else
	echo $syscode
    fi
fi

}
MACHINEARCH=`machinearch`

privusr="sgdgsys"
privgrp="sgdgserv"

PATCH_LOG=${TTAINSTDIR}/proxy/var/log/patch.log

# get version numbers
[ -s "${INSTDIR}/etc/version" ] && . ${INSTDIR}/etc/version
SGD_VERSION=${VERSION_SUNWsgdg}
VERSION_AS_NUMBER=`echo "${SGD_VERSION}" | tr -d .`
if [ "${VERSION_AS_NUMBER}" -lt 520000 ]; then
  privusr=daemon
  privgrp=daemon
fi

apachepath="${INSTDIR}/httpd/httpd-${VERSION_APACHE}"
apachebin=$apachepath/bin/apachectl
apacheconf=$apachepath/conf/httpd.conf
fApachePid="${apachepath}/logs/httpd.pid"
fProxyPid="${INSTDIR}/proxy/var/run/proxy.pid"
GetApachePid() {
    _pid=""
    [ -s "${fApachePid}" ] && _pid=`tr -cd '0123456789' <"${fApachePid}"`
    [ -n "${_pid}" ] && [ -n "`ps -p${_pid} | grep \"${_pid}\"`" ] && echo "${_pid}"
    return 0
}

GetProxyPid() {
    _pid=""
    [ -s "${fProxyPid}" ] && _pid=`tr -cd '0123456789' <"${fProxyPid}"`
    [ -n "${_pid}" ] && [ -n "`ps -p${_pid} | grep \"${_pid}.*java\"`" ] && echo "${_pid}"
    return 0
}


PATCH_TXT=patch.txt
TMP_PATCH=tmp_patch
TOPLEVEL_PATCH_DIR=${TTAINSTDIR}/bin/${TMP_PATCH}

nextfreeport() {
    [ -z "$1" ] && return
    [ "$1" -lt 1 ] && return
    port=$1
    probe="$INSTDIR/bin/bin/ttahostprobe localhost:"
    while [ "`${probe}${port}`" = "y" ]; do
        port=`expr $port + 1`
    done
    echo $port
}

Usage_add() {
  echo "$info_long_usage_add"
  exit 1
}

ExitIfGwRunning() {
  DIR=$1
  ApachePid=`GetApachePid`
  ProxyPid=`GetProxyPid`

  if [ -n "${ApachePid}" -o -n "${ProxyPid}" ]; then
    echo "${err_sgd_running_add}"
    cd ${TTAINSTDIR}/bin
    rm -rf ${DIR} 2> /dev/null
    exit 1
  fi
}

Unpack() {
  PATCHFILE=$1
  PATCH_DIR=$2

  # Unpack the patch file
  echo "${info_unpacking} ${PATCHFILE}"
  mkdir ${PATCH_DIR}
  cd ${PATCH_DIR}

  case "${PATCHFILE}" in
    *.tar.gz)
      gunzip -c ${PATCHFILE} > unzipped.tar
      if [ $? -ne 0 ] ; then
        echo ${err_unzip}
        cd ${TTAINSTDIR}/bin
        rm -rf ${PATCH_DIR}
        exit 1
      fi
      PATCHFILE=unzipped.tar
      ;;
    *.tar)
      # Will be unpacked below
      ;;
    *)
      echo "${err_file_format}"
      cd ${TTAINSTDIR}/bin
      rm -rf ${PATCH_DIR}
      exit 1
      ;;
  esac

  tar -xf ${PATCHFILE}
  if [ $? -ne 0 ]; then
    echo ${err_untar}
    cd ${TTAINSTDIR}/bin
    rm -rf ${PATCH_DIR}
    exit 1
  fi
}

# Get the value of the parameter from patch.txt
Get_Value () {
  VALUE=`grep $1 ${PATCH_TXT} | tr '=' ' ' | awk '{for (i=2; i<=NF; i++) print $i}'`
  echo "${VALUE}"
}

# Check for any conflicts with the installed patches
CheckConflicts() {
  PATCH_DIR=$1

  # Extract conflicts data and check existing patches
  echo "${info_checking_conflicts}"
  CONFLICTS=`Get_Value CONFLICTS`
  if [ -z "${CONFLICTS}" ]; then
    # No conflicts defined
    return 0
  fi

  echo "${CONFLICTS}" | tr ' ' '\012' |
  while read data; do
    CheckExists "${data}"
    if [ "$?" -eq 1 ]; then
      cd ..
      rm -rf ${PATCH_DIR}
      echo "${err_patch_conflict}" ${data}
      echo "${err_patch_failed_requirements}"
      exit 1 # exits the sub-shell
    fi
  done

  # If conflicts exist, the patch directory no longer exists
  if [ -d ${PATCH_DIR} ]; then
    return 0
  else
    return 1
  fi
}

# Check that dependencies are resolved
CheckDependencies() {
  PATCH_DIR=$1

  # Extract dependencies data and check existing patches
  echo "${info_checking_dependencies}"
  DEPENDS=`Get_Value DEPENDENCIES`
  if [ -z "${DEPENDS}" ]; then
    # No dependencies defined
    return 0
  fi

  echo "${DEPENDS}" | tr ' ' '\012' |
  while read data; do
    # Check installed patches
    CheckExists "${data}"
    if [ "$?" -eq 0 ]; then
      cd ..
      rm -rf ${PATCH_DIR}
      echo "${err_patch_depends} ${data}"
      echo "${err_patch_failed_requirements}"
      exit 1 # exits the sub-shell
    fi
  done

  # If dependencies exist, the patch directory no longer exists
  if [ -d ${PATCH_DIR} ]; then
    echo "${DEPENDS}"
    return 0
  else
    return 1
  fi
}

# Check for compatibility with the current patch mechanism
CheckPatchMech()
{
    CURRENT_PATCH_MECH=`grep "^patch_mechanism_version" ${PATCH_CFG} | awk '{print $3}'`
    if [ -z "${CURRENT_PATCH_MECH}" ]; then
        # Mechanism has not been patched
        CURRENT_PATCH_MECH="2.0"
    fi

    REQUIRED_PATCH_MECH=`Get_Value PATCH_MECH`
    if [ -n "${REQUIRED_PATCH_MECH}" ]; then
        VALID=`echo "${CURRENT_PATCH_MECH} ${REQUIRED_PATCH_MECH}" | tr '.' ' ' |
               awk '{if ($1>$3 || ($1==$3 && ($2>$4 || $2==$4))) print "valid"}'`
        if [ -z "${VALID}" ]; then
        echo "${err_mech_invalid_1} ${REQUIRED_PATCH_MECH} ${err_mech_invalid_2}"
        cd ..
        return 1
        fi
    fi

    return 0
}

AddPatchEntry() {
  NAME=$1
  TYPE=$2
  DATE=$3
  CPATH=$4
  OPATH=$5
  SGDVERSION=$6

  echo "${NAME}${DELIM}${TYPE}${DELIM}${DATE}${DELIM}${CPATH}${DELIM}${OPATH}${DELIM}${SGD_VERSION}${DELIM}${ACTIVE}${DELIM}" >> ${PATCH_LIST}
}

# Check whether a patch with the same name is already installed
CheckExists() {
  NAME=$1

  if [ ! -s "${PATCH_LIST}" ]; then
    return 0
  fi

  # Check whether the patch is already installed
  INSTALLED=`grep "^${NAME}${DELIM}" ${PATCH_LIST}`
  if [ -n "${INSTALLED}" ]; then
    return 1
  else
    return 0
  fi
}

AddSuperseded() {
  TO_INSTALL=$1
  SUPERSEDES=$2

  LINE=`grep "^${SUPERSEDES}" ${PATCH_CFG} 2> /dev/null`
  if [ -z "${LINE}" ]; then
    echo "${SUPERSEDES}:${TO_INSTALL}" >> ${PATCH_CFG}
  else
    FOUND=`echo ${LINE} | egrep ".*:.*,${TO_INSTALL},.*|.*:${TO_INSTALL},|.*:.*,${TO_INSTALL}\$"`
    if [ -z "${FOUND}" ]; then
      NEW_LINE="${LINE},${TO_INSTALL}"
      sed -e 's/'${LINE}'/'${NEW_LINE}'/' ${PATCH_CFG} > cfg.tmp
      mv -f cfg.tmp ${PATCH_CFG}
    fi
  fi
}

PostPatchScript() {
  POST_SCRIPT=`Get_Value POST_PATCH_SCRIPT`

  if [ -n "${POST_SCRIPT}" ] ; then
    echo ${info_post_install}
    export TTAINSTDIR
    ./${POST_SCRIPT} 2>&1 | tee -a ${PATCH_LOG}
  fi
}

# Execute any pre-install script
# The exit code of the script should be 0 (success) or none zero (failure)
# On failure we tidy up and exit.
PrePatchScript() {
    patch_dir="$1"
    pre_script="`Get_Value PRE_PATCH_SCRIPT`"

    if [ -n "${pre_script}" ]
    then
        export TTAINSTDIR
        # Need admin to see output of script so no re-direction to log file
        # And check return code so no 'tee'
        ./"${pre_script}"
        if [ "$?" -ne 0 ]
        then
            cd "${patch_dir}"/..
            rm -rf "${patch_dir}" 2>&1 | tee -a "${PATCH_LOG}"
            exit 1
        fi
    fi
    return 0
}

Update_Patch_Commands() {
    FILELIST=$1
    LOG_MSG=$2
    PATCH_NAME=$3

    # Pass the other files off to gateway_patch_update
    exec ${TTAINSTDIR}/bin/scripts/gateway_patch_update ${PATCH_DIR} "${FILELIST}" "${LOG_MSG}" "${PATCH_LOG}" "${PATCH_NAME}" "${PATCH_CFG}" "${PATCH_ADD_CALLBACK}"
}

Add_Commands() {
  NAME=$1
  PATCH_DIR=$2

  # Handle the patch_update first, as it can't update itself
  IS_UPDATE=`ls gateway_patch_update 2> /dev/null`
  if [ -n "${IS_UPDATE}" ]; then
    \cp gateway_patch_update ${TTAINSTDIR}/bin/scripts
    chown ${privusr}:${privgrp} ${TTAINSTDIR}/bin/scripts/gateway_patch_update
    #UpdateRef "patch_update" "${NAME}" "${PATCH_CFG}"
  fi

  # Update any other available patch commands
  #TO_UPDATE=`ls patch_* en-us/patch_* 2> /dev/null | grep -v update`
  TO_UPDATE=`ls gateway_patch_* en-us_patch 2> /dev/null | grep -v update`
  if [ -z "${TO_UPDATE}" ]; then
    # Only gateway_patch_update included
    cd ..
    rm -rf ${PATCH_DIR} 2> /dev/null
  else
    echo ${info_update_commands}
    DATE=`date`
    LOG_MSG="${DATE} - ${info_updated_commands} : ${NAME}"
    Update_Patch_Commands "${TO_UPDATE}" "${LOG_MSG}" "${NAME}"
  fi
}

Add_Bundle() {
  # NAME gets changed a lot, use BUNDLE_NAME
  BUNDLE_NAME=$1
  TTA_PATCH_DIR=$2

  # Get the list of components
  COMPONENTS=`Get_Value COMPONENT`
  echo ${info_read_bundle}

  # Special case. If the bundle contains a new set of patch commands.
  # Handle the commands patch first.
  # Patch update will then callback patch_add to install the rest of the bundle
  COMMANDS="`echo "${COMPONENTS}" | grep "^commands_"`"
  # If contains new patch commands
  if [ -n "${COMMANDS}" ]
  then
      Unpack "${TTA_PATCH_DIR}/${COMMANDS}" "${TTA_PATCH_DIR}/${TMP_PATCH}"
      rm -f "${TTA_PATCH_DIR}/${COMMANDS}" unzipped.tar 2>/dev/null
      PATCH_ADD_CALLBACK="true"
      NAME="`Get_Value NAME`"
      # Remove from bundle's patch.txt as already handled
      grep -v "^COMPONENT=${COMMANDS}" "../${PATCH_TXT}" > "../${PATCH_TXT}.tmp"
      mv "../${PATCH_TXT}.tmp" "../${PATCH_TXT}"
      Add_Commands ${NAME} "${TTA_PATCH_DIR}/${TMP_PATCH}"
  fi

  # Run any pre installation script at bundle level
  PrePatchScript "${TTA_PATCH_DIR}"

  echo "${COMPONENTS}" | while read file; do
    # Unpack the tar file
    Unpack "${TTA_PATCH_DIR}/${file}" "${TTA_PATCH_DIR}/${TMP_PATCH}"
    rm -f "${TTA_PATCH_DIR}/${file}" unzipped.tar 2>/dev/null

    # Verify that we have the patch mech required for this patch
    CheckPatchMech
    if [ "$?" -eq 1 ]
    then
        rm -rf "${TTA_PATCH_DIR}/${TMP_PATCH}"
        continue
    fi

    # Run any pre installation script at component level
    PrePatchScript "${TTA_PATCH_DIR}${TMP_PATCH}"

    # Get the type
    TYPE=`Get_Value TYPE`
    case "${TYPE}" in
      file)
        Add_File ${BUNDLE_NAME} "${TTA_PATCH_DIR}/${TMP_PATCH}"
      ;;
      webserver)
        RESTART=true
        Add_Webserver "${TTA_PATCH_DIR}/${TMP_PATCH}"
      ;;
    esac
  done

  # Execute any post patch script
  cd ${TTA_PATCH_DIR}
  PostPatchScript

  # Tidy up
  if [ "${TTA_PATCH_DIR}" ]; then
    cd ${TTAINSTDIR}/bin
    rm -rf ${TTA_PATCH_DIR}
  fi
}

Copy_File() {
  DETAILS=$1
  PATCH_DIR=$2
  DEST=$3

  # If destination a link delete it - Solaris
  if [ -h "${DEST}" ] ; then
    rm -f ${DEST}
  fi

  FILENAME=`echo "${DETAILS}" | ${AWK} -F ":" '{print $1}'`
  \cp -rP ${PATCH_DIR}/${FILENAME} ${DEST}
  # Split the details to get the user/group and permissions
  USER=`echo "${DETAILS}" | ${AWK} -F ":" '{print $3}'`
  if [ -n "${USER}" ] ; then
    case "${USER}" in
      "PRIVUSR")
        USER=${privusr}
      ;;
      "WEBUSR")
        USER=${webservusr}
      ;;
    esac
    chown -h ${USER} ${DEST}
  fi

  GROUP=`echo "${DETAILS}" | ${AWK} -F ":" '{print $4}'`
  if [ -n "${GROUP}" ] ; then
    case "${GROUP}" in
      "PRIVGRP")
        GROUP=${privgrp}
      ;;
    esac
    chgrp -h ${GROUP} ${DEST}
  fi

  PERMISSIONS=`echo "${DETAILS}" | ${AWK} -F ":" '{print $5}'`
  if [ -n "${PERMISSIONS}" ] ; then
    chmod ${PERMISSIONS} ${DEST}
  fi

}

Add_File() {
  NAME=$1
  PATCH_DIR=$2

  CheckDependencies "${PATCH_DIR}"
  if [ "$?" -eq 1 ]; then
    return
  fi

  echo "${info_file_install} ${NAME}"
  [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"

  DATE=`date`
  # Superceded patches
  SUPERSEDED=`Get_Value SUPERSEDED`
  if [ -n "${SUPERSEDED}" ]; then
    echo "${SUPERSEDED}" | tr ' ' '\012' |
    while read data; do
      if [ -z "${data}" ]; then
        continue
      fi

      AddSuperseded ${NAME} ${data}
      # For each superseded patch, check whether it is currently installed
      grep "^${data}${DELIM}" ${PATCH_LIST} |
      {
        while read ENTRY; do
          # If the patch is already inactive, skip it
          IS_INACTIVE=`echo "${ENTRY}" | ${AWK} -F "${DELIM}" '{print $7}' | grep ${INACTIVE}`
          if [ -n "${IS_INACTIVE}" ]; then
            continue
          fi

          # Handle each file in the superseded patch
          PATCH_PATH=`echo "${ENTRY}" | ${AWK} -F "${DELIM}" '{print $4}'`
          FILE_NAME=`basename "${PATCH_PATH}"`
          BACKUP_PATCH_FILE="${PATCH_PATH}.${data}"

          # Back up the superseded file
          \cp -rpP "${PATCH_PATH}" "${BACKUP_PATCH_FILE}"

          RELATIVE_PATH=`echo "${PATCH_PATH}" | sed 's:'${TTAINSTDIR}/'::'`
          # Copy new file
          FILE_DETAILS=`Get_Value FILE | grep "^${RELATIVE_PATH}:"`
          if [ -z "${FILE_DETAILS}" ] ; then
            FILE_DETAILS=`Get_Value FILE | grep "^${FILE_NAME}:"`
          fi
          if [ -z "${FILE_DETAILS}" ] ; then
            FILE_DETAILS=`Get_Value FILE | grep "^${FILE_NAME}$"`
          fi
          echo "${info_binary} ${PATCH_PATH}"
          Copy_File ${FILE_DETAILS} ${PATCH_DIR} "${PATCH_PATH}"

          # Add file to list of files handled by superseding to be removed
          # before normal patch handling
          FILE_TO_REMOVE=`echo "${FILE_DETAILS}" | ${AWK} -F ":" '{print $1}'`
          HANDLED="${HANDLED} ${FILE_TO_REMOVE}"

          # Modify the patch list for the superseded file
          NEW_ENTRY=`echo "${ENTRY}" | sed -e 's/'${DELIM}${ACTIVE}${DELIM}'/'${DELIM}${INACTIVE}${DELIM}'/g' | sed -e 's:'${DELIM}${PATCH_PATH}${DELIM}':'${DELIM}${BACKUP_PATCH_FILE}${DELIM}':'`
          UpdateEntry "${ENTRY}" "${NEW_ENTRY}"
          # Create a new entry for the patch binary
          AddPatchEntry "${NAME}" "file" "${DATE}" "${PATCH_PATH}" "${BACKUP_PATCH_FILE}" "${SGD_VERSION}"
        done
        # Remove form tmp_patch so not handled by code below.
        rm -f ${HANDLED} 2>/dev/null
      }
    done
  fi

  # Handle patch
  FILES=`Get_Value FILE`
  echo "${FILES}" | while read file; do
    cd ${PATCH_DIR}
    FILENAME=`echo "${file}" | ${AWK} -F ":" '{print $1}'`
    # File valid and not handled in superseded above
    if [ -f "${FILENAME}" ]; then
      LOCATION=`echo "${file}" | ${AWK} -F ":" '{print $2}'`
      #EXTRA_INFO=`echo ${DETAILS} | grep ':'`
      if [ -z "${LOCATION}" ] ; then
        # No location specified find all occurrences of file omit tmp_patch directory
        cd ${TTAINSTDIR}
        find . -name ${file} -print 2>/dev/null | grep -v "${TMP_PATCH}" | sed -e 's:'./'::' | while read location; do
          # Make backup of file
          BACKUP_FILE="${TTAINSTDIR}/${location}.${SGD_VERSION}"
          \cp -rpP ${TTAINSTDIR}/${location} ${BACKUP_FILE}
          # Copy in new file
          echo "${info_binary} ${TTAINSTDIR}/${location}"
          Copy_File ${file} ${PATCH_DIR} ${TTAINSTDIR}/${location}
          # Add patch entry
          AddPatchEntry "${NAME}" "file" "${DATE}" "${TTAINSTDIR}/${location}" "${BACKUP_FILE}" "${SGD_VERSION}"
        done
      else
        # Location specified
        # Backup any existing file
        FULLFILE=${TTAINSTDIR}/${FILENAME}
        BACKUP_FILE=${FULLFILE}.${SGD_VERSION}
        if [ -f "${FULLFILE}" ] ; then
          \cp -rpP "${FULLFILE}" "${BACKUP_FILE}"
        fi
        echo "${info_binary} ${FULLFILE}"
        Copy_File ${file} ${PATCH_DIR} ${FULLFILE}
        AddPatchEntry "${NAME}" "file" "${DATE}" "${FULLFILE}" "${BACKUP_FILE}" "${SGD_VERSION}"
      fi
    fi
  done

  # Execute any post patch script
  cd ${PATCH_DIR}
  PostPatchScript
  # Reuse message
  echo "${info_webcomp_complete}"
  # Tidy up
  cd ..
  rm -rf ${PATCH_DIR} 2> /dev/null

  # Update log
  AddedPatch "${DATE}" "file" "${NAME}"
}

Add_Webserver() {

    PATCH_DIR=$1
    PATCH_NAME=`Get_Value APACHE_PATCH`
    CheckDependencies "${PATCH_DIR}"
    if [ "$?" -eq 1 ]; then
      return
    fi

    OLD_APACHE_VERSION="${VERSION_APACHE}"
    # Read patch data
    NEW_APACHE_VERSION=`Get_Value APACHE_VERSION`
    if [ -z "${NEW_APACHE_VERSION}" ]; then
      echo "${err_no_apache_version}"
      return 1;
    fi

    if [ "${NEW_APACHE_VERSION}" = "${OLD_APACHE_VERSION}" ]; then
      echo "${err_already_installed1}${PATCH_NAME}${err_already_installed2}"
      return 1
    fi

    APACHE_DIR=${TTAINSTDIR}/httpd
    OLD_APACHE_DIR="${APACHE_DIR}/httpd-${OLD_APACHE_VERSION}"
    NEW_APACHE_DIR="${APACHE_DIR}/httpd-${NEW_APACHE_VERSION}"

    # The directory might already be present if upgrading a previously patched
    # gateway.
    # The upgrade will have reverted to it's default apache version, but will have
    # left the directories of the newer version in place.
    # We still need to apply the patch.
    # Rename the existing directory and carry on. Bug 21033510
    if [ -d "${NEW_APACHE_DIR}" ] ; then
      mv -f ${NEW_APACHE_DIR} ${NEW_APACHE_DIR}.pre_${SGD_VERSION}
    fi

    echo ${info_webserver_apache}

    # Move the new apache into the correct location
    mv -f ${NEW_APACHE_VERSION} ${NEW_APACHE_DIR}
    # Set the correct file ownership
    chown -R -h ${privusr}:${privgrp} ${NEW_APACHE_DIR}
    chmod -R o-rwx ${NEW_APACHE_DIR}
    chmod -R g-w ${NEW_APACHE_DIR}
    # Keep old conf
    \cp -rpP  ${OLD_APACHE_DIR}/conf ${NEW_APACHE_DIR}
    cd ${NEW_APACHE_DIR}/conf
    find . -type f -exec grep -l "${OLD_APACHE_DIR}" {} \; -print 2>/dev/null | while read file
    do
      sed "s|${OLD_APACHE_DIR}|${NEW_APACHE_DIR}|g" $file > $file.tmp
      cp $file.tmp $file
      rm $file.tmp
    done

    echo ${info_webserver_config}
    # detect any settings from previous installs and use them as the default
    if [ -s $INSTDIR/etc/gatewayconfig.xml ]; then
      reverse_proxy_addr=`sed -n "s/.*<reverseproxyaddr>\(.*\)<\/reverseproxyaddr>.*/\1/p" $INSTDIR/etc/gatewayconfig.xml`
    else
      # Shouldn't happen but set to default just incase
      REVERSEPROXYREDIRECTPORT=`nextfreeport 8080`
      REVERSEPROXYPORT=`expr "${REVERSEPROXYREDIRECTPORT}" + 1`
      reverse_proxy_addr="localhost:${REVERSEPROXYPORT}"
    fi

    # configure the web server
    # .sh for pre 5.2
    CONFIG_INSTALL=${INSTDIR}/bin/scripts/config_install
    if [ -f ${INSTDIR}/bin/scripts/config_install.sh ]; then
      CONFIG_INSTALL=${INSTDIR}/bin/scripts/config_install.sh
    fi

    /bin/sh ${CONFIG_INSTALL} \
    --apachelocation="${INSTDIR}/httpd/httpd-${NEW_APACHE_VERSION}" \
    --apacheport="${reverse_proxy_addr}" \
    --machinename="`${INSTDIR}/bin/bin/ttahostname`" \
    --ttauser="${privusr}" \
    --ttagroup="${privgrp}" \
    --instdir=${INSTDIR}"

    # Update apacheversion in etc/version
    sed -e 's/'${VERSION_APACHE}'/'${NEW_APACHE_VERSION}'/g' ${TTAINSTDIR}/etc/version > tmp-version
    mv -f tmp-version ${TTAINSTDIR}/etc/version
    chown ${privusr}:${privgrp} ${TTAINSTDIR}/etc/version
    # Update version in var/info/apacheversion
    echo ${NEW_APACHE_VERSION} > ${TTAINSTDIR}/var/info/apacheversion

    # Update any previous patches to be inactive
    # Update the patch log
    if [ -s "${PATCH_LIST}" ]; then
      # Look for an old version which is superceded by this update
      ENTRY=`grep "${DELIM}${TYPE}${DELIM}" ${PATCH_LIST} | grep "${OLD_APACHE_VERSION}"`

      if [ -n "${ENTRY}" ]; then
        # Update the entry to show that version as now inactive
        UpdateEntry "${ENTRY}"
      fi
    fi

    # Add new patch entry
    DATE=`date`
    AddPatchEntry "${PATCH_NAME}" "${TYPE}" "${DATE}" "${NEW_APACHE_DIR}" "${OLD_APACHE_DIR}" "${SGD_VERSION}"

    echo "${info_webcomp_complete}"

    return 0

}

##############################################################################

if [ "$#" -eq 0 ]; then
    Usage_add
fi

# Parse the arguments
FILE=""
UNPACK="true"
while [ ! -z "$1" ]; do
    case $1 in
    --help)
        Usage_add
        ;;
    --file)
        case "$2" in
        "" | --*)
            echo ${err_invalid_arg}
            Usage_add
            ;;
        /*)
            # correct format
            ;;
        *)
            echo ${err_file_not_absolute}
            exit 1
            ;;
        esac
        FILE="$2"
        ;;
     --cont)
        # Just updated patch commands, continue with rest of bundle - don't unpack
        UNPACK="";
        ;;
    *)
        echo ${err_unknown_arg} "$1"
        Usage_add
        ;;
    esac
    shift; shift
done

# Check file was supplied
if [ -z "${FILE}" -a "${UNPACK}" ]
then
    echo ${err_missingfile}
    Usage_add
fi

# Check the file exists
if [ ! -f "${FILE}" -a "${UNPACK}" ]
then
    echo ${err_file_does_not_exist} ${FILE}
    exit 1
fi

# Check required/available disk space
[ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
if [ "${UNPACK}" ]
then
    FILE_SIZE=`ls -la "${FILE}" | $AWK '{ print $5 }'`
fi
if [ -n "${FILE_SIZE}" ] ; then
  REQUIRED_DISK_SPACE=`expr ${FILE_SIZE} \* 6`
  # Convert to kilobytes
  KILOBYTES=`expr ${REQUIRED_DISK_SPACE} / 1000 + 1`
  MEGABYTES=`expr ${KILOBYTES} / 1000 + 1`
  AVAIL=`df -k "${TTAINSTDIR}" | $AWK '/[0-9]%/{print $(NF-2)}'`
  # Bail out if not enough
  if [ "${AVAIL}" -lt "${KILOBYTES}" ] ; then
    echo "${err_disk_space}${MEGABYTES} ${err_disk_space_unit}"
    exit 1
  fi
fi

# Unpack the patch file
if [ "${UNPACK}" ]
then
    Unpack ${FILE} ${TOPLEVEL_PATCH_DIR}
else
    # Continue with unfinished bundle (but only if the patch directory exists)
    if [ -d "${TOPLEVEL_PATCH_DIR}" ]
    then
        cd "${TOPLEVEL_PATCH_DIR}"
    else
        exit 1
    fi
fi

# Check patch is for this product
PRODUCT="`Get_Value PRODUCT`"
if [ -n "${PRODUCT}" ] ; then
  if [ "${PRODUCT}" != "Gateway" ] ; then
    cd ..
    rm -rf ${TOPLEVEL_PATCH_DIR}
    echo "${err_wrong_product} ${PRODUCT}"
    echo "${err_this_product}"
    exit 1
  fi
fi

# Check architecture
ARCH=`Get_Value ARCH`
if [ -n "${ARCH}" ] ; then
  SHORTARCH=`machinearch -c`
  if [ "${SHORTARCH}" != "${ARCH}" ] ; then
    cd ..
      rm -rf ${TOPLEVEL_PATCH_DIR}
    echo ${err_wrong_arch} ${ARCH}
    echo ${err_local_arch} ${SHORTARCH}
    exit 1
  fi
fi

# Check patch is for this version of SGD
PATCH_SGD_VERSION=`Get_Value SGD_VERSION`
if [ -n "${PATCH_SGD_VERSION}" ] ; then
  SGD_SHORT_VERSION=`expr "${SGD_VERSION}" : '\(....\).*'`
  MATCH=`echo ${PATCH_SGD_VERSION} | grep ${SGD_SHORT_VERSION}`
  if [ -z "${MATCH}" ] ; then
    cd ..
    rm -rf ${TOPLEVEL_PATCH_DIR}
    echo ${err_wrong_sgd} ${PATCH_SGD_VERSION}
    echo ${err_local_sgd} ${SGD_SHORT_VERSION}
    exit 1
  fi
fi

# If the name exists, check it is not already installed
NAME=`Get_Value NAME`
if [ -n "${NAME}" ]; then
    CheckExists "${NAME}"
    if [ "$?" -eq 1 ]; then
    cd ..
    rm -rf ${TOPLEVEL_PATCH_DIR}
    echo "${err_already_installed1}${NAME}${err_already_installed2}"
    exit 1
    fi
fi

# Check the patch is not superseded by another which is already installed
IS_SUPERSEDED=`grep "^${NAME}:" ${PATCH_CFG} | tr ':' ' ' | awk '{print $2}' | tr ',' ' '`
if [ -n "${IS_SUPERSEDED}" ]; then

    cd ..
    rm -rf ${TOPLEVEL_PATCH_DIR}
    echo "${err_superseded} ${IS_SUPERSEDED}"
    exit 1
fi

CheckPatchMech
if [ "$?" -eq 1 ]
then
    rm -rf ${TOPLEVEL_PATCH_DIR}
    exit 1
fi

# Check for restart action
ACTION=`Get_Value ACTION`
case "${ACTION}" in
    stop)
    ExitIfGwRunning ${TOPLEVEL_PATCH_DIR}
    ;;
    restart)
    RESTART=true
    ;;
    array)
    ARRAY=true
    # Query whether the installation should proceed
    echo "${info_array}"
    while [ 1 -eq 1 ]; do
        echo "${query_array}"
        read continue
        case "${continue}" in
        y|Y|yes)
            break;
            ;;
        n|N|no)
            echo "${info_abort}"
            cd ..
            rm -rf ${TOPLEVEL_PATCH_DIR}
            exit 1
            ;;
        esac
    done
    ;;
esac

# Get the type from the patch
TYPE=`Get_Value TYPE`

# Run any pre-install script
# Note bundle is a special case - we update the patch commands before running any
# pre-install script
if [ "${TYPE}" != "bundle" ]
then
    PrePatchScript "${TOPLEVEL_PATCH_DIR}"
fi

# Add the patch based on the type
case "${TYPE}" in
    bundle)
      Add_Bundle ${NAME} ${TOPLEVEL_PATCH_DIR}
    ;;
    commands)
      Add_Commands ${NAME} ${TOPLEVEL_PATCH_DIR}
    ;;
    file)
      Add_File ${NAME} ${TOPLEVEL_PATCH_DIR}
    ;;
    webserver)
      RESTART=true
      Add_Webserver ${TOPLEVEL_PATCH_DIR}
    ;;
    *)
      echo ${err_invalid_type} "${TYPE}"
      rm -rf ${TOPLEVEL_PATCH_DIR}
      exit 1
    ;;
esac

 # Tidy up
if [ "${TOPLEVEL_PATCH_DIR}" ]; then
  cd ${TTAINSTDIR}/bin
  rm -rf ${TOPLEVEL_PATCH_DIR}
fi

# Prompt for a restart, if required
if [ -n "${RESTART}" ]; then
    echo "${info_restart}"
fi

# Warn other array nodes must be patched
if [ -n "${ARRAY}" ]; then
    echo "${info_array}"
fi

# Set correct ownership of patch.cfg and patch.list
chown ${privusr}:${privgrp} ${PATCH_LIST} ${PATCH_CFG} 2>/dev/null
