#!/bin/bash
#  $Id: patch_remove.sh 145455 2017-06-15 14:40:42Z mike.mcewen@oracle.com $
#  Copyright (c) 2014, 2019, 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 remove 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

if [ -f ${TTAMSG}/common -a -f ${TTAMSG}/patch_remove ]; then
  . ${TTAMSG}/common
  . ${TTAMSG}/patch_remove
else
  echo "Unable to open message file: ${TTAMSG}/patch_remove"
  exit 1
fi

GetPrivUsrGrp()
{
    cat ${TTAINSTDIR}/var/serverconfig/global/array.properties 2>/dev/null | awk -F= \
	'BEGIN { u="ttasys"; wu="ttaserv"; g="ttaserv" }
	$1 == "tarantella.config.array.privilegeduser" { u = $2 }
	$1 == "tarantella.config.array.webserveruser" { wu = $2 }
	$1 == "tarantella.config.array.privilegedgroup" { g = $2 }
	END { print u " " wu " " g }'
}
_privusrgrp=`GetPrivUsrGrp`
privusr=`expr "$_privusrgrp" : '\([^ ]*\) .*'`
webservusr=`expr "$_privusrgrp" : '.* \([^ ]*\) .*'`
privgrp=`expr "$_privusrgrp" : '.* \([^ ]*\)$'`

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}
}


Usage_remove() {
    echo "${info_long_usage}"
    exit 1
}

ExitIfInactive() {
    ENTRIES=$1

    # Removal of inactive patches is not supported
    IS_INACTIVE=`echo ${ENTRIES} | grep "|${INACTIVE}|"`
    if [ -n "${IS_INACTIVE}" ]; then
	echo "${err_inactive_unsupported}"
	exit 1
    fi
}

Remove_Jserver() {
    ENTRIES=$1

    # Abort if SGD is still running
    ExitIfRunning ${TTAINSTDIR}/var/info/auxserver.pid ${TTAINSTDIR} "${err_sgd_running}"
    ExitIfRunning ${TTAINSTDIR}/var/info/jserver.pid "java -XX:-UseVMInterruptibleIO" "${err_sgd_running}"

    cd ${TTAINSTDIR}/bin
    [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
    
    ExitIfInactive "${ENTRIES}"

    cd ${TTAINSTDIR}/bin/java
    JAR_EXE=`jar`
    # Remove the jar from the classpath
    JAR_LOCATION="\${JAVADIR}/${NAME}.jar"
    sed -e 's%'CLASSPATH=${JAR_LOCATION}:'%'CLASSPATH='%' ${TTAINSTDIR}/bin/scripts/jserverstart > tmp.start
    \cp tmp.start ${TTAINSTDIR}/bin/scripts/jserverstart

    echo "${ENTRIES}" | while read ENTRY; do
	# Current location of the class
	CURRENT_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`

	# Old location
	OLD_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $5}'`
	
	# Delete the current path
	rm -f "${CURRENT_PATH}"

	ENTRY_TO_UPDATE=`${AWK} -F "${DELIM}" -v class="${OLD_PATH}" '{ if ($4 == class) print $0}' ${PATCH_LIST}`
	NEW_ENTRY=`echo "${ENTRY_TO_UPDATE}" | sed -e 's:'${DELIM}${INACTIVE}${DELIM}':'${DELIM}${ACTIVE}${DELIM}':'`
	if [ -n "${ENTRY_TO_UPDATE}" ]; then
	    UpdateEntry "${ENTRY_TO_UPDATE}" "${NEW_ENTRY}"
	fi
	
        # Delete the entry from the patch file
	grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
	mv -f patch.list.tmp ${PATCH_LIST}
    done

    # Delete the patch jar
    rm -f "${BACKUP_JSERVER_DIR}/${NAME}.jar"
}

Remove_Binary() {
    ENTRIES=$1

    cd ${TTAINSTDIR}/bin
    [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
    SGD_VERSION=`GetSGDVersion`

    ExitIfInactive "${ENTRIES}"

    echo "${ENTRIES}" | while read ENTRY; do
        # Current location of the binary
	BINARY_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`

        # Extract the binary this replaced
	OLD_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $5}'`

    	# Delete the binary
	rm -f "${BINARY_PATH}"

	# Move the replaced binary back
	mv -f "${OLD_PATH}" "${BINARY_PATH}"

        # Was the replaced binary also a patch ?
	IS_ORIG=`echo "${OLD_PATH}" | grep "${SGD_VERSION}\$"`
	if [ -z "${IS_ORIG}" ]; then
	    # Binary was already patched so need to update the
	    # patch list to show that patch as active again
	    REPLACED_PATCH_NAME=`echo "${OLD_PATH}" | ${AWK} -F "." '{print $NF}'`
	    OLD_ENTRY=`grep "^${REPLACED_PATCH_NAME}${DELIM}" ${PATCH_LIST} | grep "${OLD_PATH}"`
	    grep -v "${OLD_ENTRY}" ${PATCH_LIST} > patch.list.tmp
	    mv -f patch.list.tmp ${PATCH_LIST}
	    NEW_ENTRY=`echo "${OLD_ENTRY}" | ${AWK} -F "|" -v newpath="${BINARY_PATH}" -v delim="${DELIM}" -v active="${ACTIVE}" '{print $1 delim $2 delim $3 delim newpath delim $5 delim $6 delim active delim}'`
	    echo "${NEW_ENTRY}" >> ${PATCH_LIST}
	fi

        # Delete the entry from the patch file
	grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
	mv -f patch.list.tmp ${PATCH_LIST}
    done
}

Remove_Webserver() {
    ENTRY=$1

    # Abort if SGD is still running
    ExitIfRunning ${TTAINSTDIR}/var/info/auxserver.pid ${TTAINSTDIR} "${err_sgd_running}"
    ExitIfRunning ${TTAINSTDIR}/var/info/jserver.pid "java -XX:-UseVMInterruptibleIO" "${err_sgd_running}"

    ExitIfInactive "${ENTRIES}"

    [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
    # Get name
    PATCH_NAME=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $1}'`
    # Current location of the webserver component
    FILE_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`

    # Extract the file this replaced
    OLD_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $5}'`
    OLD_VERSION=`basename ${OLD_PATH}`
    SGD_VERSION=`GetSGDVersion`

    IS_TOMCAT=`echo ${PATCH_NAME} | grep "^tomcat"`
    IS_APACHE=`echo ${PATCH_NAME} | grep "^apache"`
    if [ -n "${IS_TOMCAT}" ] ; then
      if [ -s "${TTAINSTDIR}/var/info/tomcatversion.${OLD_VERSION}" ] ; then
        mv -f ${TTAINSTDIR}/var/info/tomcatversion.${OLD_VERSION} ${TTAINSTDIR}/var/info/tomcatversion
      else
        echo "${OLD_VERSION}" > ${TTAINSTDIR}/var/info/tomcatversion
      fi
      chown ${privusr}:${privgrp} ${TTAINSTDIR}/var/info/tomcatversion
      chmod 640 ${TTAINSTDIR}/var/info/tomcatversion
      # Re-create Resources.properties link
      cd ${TTAINSTDIR}/bin/java/com/tarantella/tta/webservices/client/views
      rm -f Resources.properties 2>/dev/null
      ln -s $OLD_PATH/shared/classes/com/tarantella/tta/webservices/client/views/Resources.properties .
      # Tidy up 
      # Remove the patch directory
      rm -rf ${FILE_PATH}
    elif [ -n "${IS_APACHE}" ] ; then
      if [ -s "${TTAINSTDIR}/var/info/apacheversion.${OLD_VERSION}" ] ; then
        mv -f ${TTAINSTDIR}/var/info/apacheversion.${OLD_VERSION} ${TTAINSTDIR}/var/info/apacheversion
      else
        echo "${OLD_VERSION}" > ${TTAINSTDIR}/var/info/apacheversion
      fi
      chown ${privusr}:${privgrp} ${TTAINSTDIR}/var/info/apacheversion  
      chmod 640 ${TTAINSTDIR}/var/info/apacheversion
      # remove messed up old conf
      #rm -rf ${OLD_PATH}/conf

      # Tidy up 
      # Remove the patch directory
      rm -rf ${FILE_PATH}
    else
      echo "${ENTRY} ${err_remove_webserver}"
      return
    fi 

    # Delete the entry from the patch file
    grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
    mv -f patch.list.tmp ${PATCH_LIST}

    # Update the previous apache/tomcat patch to show as active (if applicable)
    OLD_ENTRY=`grep "${DELIM}${OLD_PATH}${DELIM}" ${PATCH_LIST}`
    if [ -n "${OLD_ENTRY}" ]; then
        NEW_ENTRY=`echo "${OLD_ENTRY}" | sed -e 's:'${DELIM}${INACTIVE}${DELIM}':'${DELIM}${ACTIVE}${DELIM}':'`
        UpdateEntry "${OLD_ENTRY}" "${NEW_ENTRY}"
    fi
}

Remove_SGDWebComponent() {
    ENTRIES=$1

    # Abort if SGD is still running
    ExitIfRunning ${TTAINSTDIR}/var/info/auxserver.pid ${TTAINSTDIR} "${err_sgd_running}"
    ExitIfRunning ${TTAINSTDIR}/var/info/jserver.pid "java -XX:-UseVMInterruptibleIO" "${err_sgd_running}"

    cd ${TTAINSTDIR}/bin
    [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
    SGD_VERSION=`GetSGDVersion`

    ExitIfInactive "${ENTRIES}"

    echo "${ENTRIES}" | while read ENTRY; do
        # Current location of the webcomponent file
        FILE_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`

        # Extract the file this replaced
        OLD_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $5}'`

        RESTORE_DIR=`dirname "${FILE_PATH}"`
        # Move the replaced file back
        if [ -z "${OLD_PATH}" ]; then
          # No back-up, must have been a new file
          
          rm -f "${FILE_PATH}"
        else
          if [ -f "${OLD_PATH}" -a -d "${RESTORE_DIR}" ] ; then
            mv -f "${OLD_PATH}" "${FILE_PATH}"
            touch "${FILE_PATH}"
          else
            # The directory this backup came from no longer exists so delete the backup
            rm -rf "${OLD_PATH}"
          fi
        
          # Was the replaced file also a patch ?
          IS_ORIG=`echo "${OLD_PATH}" | grep "${SGD_VERSION}\$"`
          if [ -z "${IS_ORIG}" ]; then
              # File was already patched so need to update the
              # patch list to show that patch as active again
              REPLACED_PATCH_NAME=`echo "${OLD_PATH}" | ${AWK} -F "." '{print $NF}'`
              OLD_ENTRY=`grep "^${REPLACED_PATCH_NAME}${DELIM}" ${PATCH_LIST} | grep "${OLD_PATH}"`
              grep -v "${OLD_ENTRY}" ${PATCH_LIST} > patch.list.tmp
              mv -f patch.list.tmp ${PATCH_LIST}
              NEW_ENTRY=`echo "${OLD_ENTRY}" | ${AWK} -F "|" -v newpath="${FILE_PATH}" -v delim="${DELIM}" -v active="${ACTIVE}" '{print $1 delim $2 delim $3 delim newpath delim $5 delim $6 delim active delim}'`
              echo "${NEW_ENTRY}" >> ${PATCH_LIST}
          fi
        fi          

        # Delete the entry from the patch file
        grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
        mv -f patch.list.tmp ${PATCH_LIST}
    done 
}

Remove_File() {
  ENTRIES=$1
 
  ExitIfInactive "${ENTRIES}"
 
  [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
  SGD_VERSION=`GetSGDVersion`
  
  echo "${ENTRIES}" | while read ENTRY; do
    # Current location of the file
    FILE_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`
    
    # If a directory continue
    if [ -d "${FILE_PATH}" ] ; then
        continue;
    fi

    # Extract the file this replaced
    OLD_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $5}'`

    RESTORE_DIR=`dirname "${FILE_PATH}"`
    # Move the replaced file back
    if [ -f "${OLD_PATH}" -a -d "${RESTORE_DIR}" ] ; then
      mv -f "${OLD_PATH}" "${FILE_PATH}"
      touch "${FILE_PATH}"
    else
      # Or delete if no old file
      rm -f ${FILE_PATH}
    fi

    # Was the replaced file also a patch ?
    IS_ORIG=`echo "${OLD_PATH}" | grep "${SGD_VERSION}\$"`
    if [ -z "${IS_ORIG}" ]; then
      # Binary was already patched so need to update the
      # patch list to show that patch as active again
      REPLACED_PATCH_NAME=`echo "${OLD_PATH}" | ${AWK} -F "." '{print $NF}'`
      OLD_ENTRY=`grep "^${REPLACED_PATCH_NAME}${DELIM}" ${PATCH_LIST} | grep "${OLD_PATH}"`
      grep -v "${OLD_ENTRY}" ${PATCH_LIST} > patch.list.tmp
      mv -f patch.list.tmp ${PATCH_LIST}
      NEW_ENTRY=`echo "${OLD_ENTRY}" | ${AWK} -F "|" -v newpath="${FILE_PATH}" -v delim="${DELIM}" -v active="${ACTIVE}" '{print $1 delim $2 delim $3 delim newpath delim $5 delim $6 delim active delim}'`
      echo "${NEW_ENTRY}" >> ${PATCH_LIST}
    fi

    # Delete the entry from the patch file
    grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
    mv -f patch.list.tmp ${PATCH_LIST}
  done

  # Remove any empty directory
  echo "${ENTRIES}" | while read ENTRY; do
    # Current location of the file
    FILE_PATH=`echo ${ENTRY} | ${AWK} -F "${DELIM}" '{print $4}'`
    
    # If a directory and empty remove the directory
    # It needs to be empty as it maybe in use by another patch
    if [ -d "${FILE_PATH}" ] ; then
      rmdir "${FILE_PATH}" 2>/dev/null
      # Delete the entry from the patch file
      grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
      mv -f patch.list.tmp ${PATCH_LIST}
    fi
  done
}

Remove_Delete() {
  ENTRIES="$1"

  ExitIfInactive "${ENTRIES}"

  [ -x "/usr/bin/nawk" ] && AWK="nawk" || AWK="awk"
  SGD_VERSION=`GetSGDVersion`

  echo "${ENTRIES}" | while read ENTRY; do

    # Current location of the file
    FILE_PATH="`echo "${ENTRY}" | ${AWK} -F "${DELIM}" '{print $4}'`"

    # Extract the file this replaced
    OLD_PATH="`echo "${ENTRY}" | ${AWK} -F "${DELIM}" '{print $5}'`"

    RESTORE_DIR="`dirname "${FILE_PATH}"`"

    # Move the replaced file back
    if [ -f "${OLD_PATH}" -o -d "${OLD_PATH}" -a -d "${RESTORE_DIR}" ];
    then
      mv -f "${OLD_PATH}" "${FILE_PATH}"
      touch "${FILE_PATH}"
    else
      # Or delete if no old file
      rm -f "${FILE_PATH}"
    fi

    # Was the replaced file also a patch ?
    IS_ORIG=`echo "${OLD_PATH}" | grep "${SGD_VERSION}\$"`
    if [ -z "${IS_ORIG}" ];
    then
      # File was already patched so need to update the
      # patch list to show that patch as active again
      REPLACED_PATCH_NAME=`echo "${OLD_PATH}" | ${AWK} -F "." '{print $NF}'`
      OLD_ENTRY=`grep "^${REPLACED_PATCH_NAME}${DELIM}" ${PATCH_LIST} | grep "${OLD_PATH}"`
      grep -v "${OLD_ENTRY}" ${PATCH_LIST} > patch.list.tmp
      mv -f patch.list.tmp ${PATCH_LIST}
      NEW_ENTRY=`echo "${OLD_ENTRY}" | ${AWK} -F "|" -v newpath="${FILE_PATH}" -v delim="${DELIM}" -v active="${ACTIVE}" '{print $1 delim $2 delim $3 delim newpath delim $5 delim $6 delim active delim}'`
      echo "${NEW_ENTRY}" >> ${PATCH_LIST}
    fi

    # Delete the entry from the patch file
    grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
    mv -f patch.list.tmp ${PATCH_LIST}
  done

  # Remove any empty directory
  echo "${ENTRIES}" | while read ENTRY; do
    # Current location of the file
    FILE_PATH="`echo "${ENTRY}" | ${AWK} -F "${DELIM}" '{print $4}'`"

    # If a directory and empty remove the directory
    # It needs to be empty as it maybe in use by another patch
    if [ -d "${FILE_PATH}" ];
    then
      rmdir "${FILE_PATH}" 2>/dev/null
      # Delete the entry from the patch file
      grep -v "${ENTRY}" ${PATCH_LIST} > patch.list.tmp
      mv -f patch.list.tmp "${PATCH_LIST}"
    fi
  done
}

TidyUpAfterUpgrade

NAME=""

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

while [ ! -z "$1" ]; do
    case "$1" in
	--help)
	    Usage_remove
	    ;;
	--name)
	    case "$2" in
		"" | --*) echo ${err_invalid_args}; Usage_remove
	    esac
	    NAME="$2"
	    ;;
	*)
	    echo ${err_invalid_args}
	    Usage_remove
	    ;;
    esac
    shift; shift
done

# Exit if no patches are installed
if [ ! -s "${PATCH_LIST}" ];then
    echo "${err_no_patch_list}"
    exit 1
fi

# Update the list of superseded patches
sed -e 's/',${NAME}'/''/g' ${PATCH_CFG} | sed -e 's/':${NAME}'/''/g' | grep ":" > cfg.tmp
mv -f cfg.tmp ${PATCH_CFG}

# Find all the entries to be removed
JSERVER_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}jserver${DELIM}"`
BINARY_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}binary${DELIM}"`
JVM_ENTRY=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}JVM${DELIM}"`
WEBSERVER_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}webserver${DELIM}"`
WEBCOMP_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}webcomp${DELIM}"`
FILE_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}file${DELIM}"`
DELETE_ENTRIES=`grep "^${NAME}${DELIM}" ${PATCH_LIST} | grep "${DELIM}delete${DELIM}"`

if [ -z "${JSERVER_ENTRIES}" -a -z "${BINARY_ENTRIES}" -a -z "${WEBSERVER_ENTRIES}" -a -z "${JVM_ENTRY}" -a -z "${WEBCOMP_ENTRIES}" -a -z "${FILE_ENTRIES}" -a -z "${DELETE_ENTRIES}" ]; then
    echo "${err_version_nomatch}"
    exit 1
fi

if [ -n "${JSERVER_ENTRIES}" ]; then
    Remove_Jserver "${JSERVER_ENTRIES}"
fi

if [ -n "${BINARY_ENTRIES}" ]; then
    Remove_Binary "${BINARY_ENTRIES}"
fi

if [ -n "${JVM_ENTRY}" ]; then
    Remove_JVM "${JVM_ENTRY}"
fi

if [ -n "${WEBSERVER_ENTRIES}" ]; then
    Remove_Webserver "${WEBSERVER_ENTRIES}"
fi

if [ -n "${WEBCOMP_ENTRIES}" ]; then
    Remove_SGDWebComponent "${WEBCOMP_ENTRIES}"
fi

if [ -n "${FILE_ENTRIES}" ]; then
    Remove_File "${FILE_ENTRIES}"
fi

if [ -n "${DELETE_ENTRIES}" ]; then
    Remove_Delete "${DELETE_ENTRIES}"
fi

echo "${info_complete} ${NAME}"

DATE=`date`
RemovedPatch "${DATE}" "${NAME}" >> "${PATCH_LOG}"

echo "${info_restart}"

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

