#!/bin/sh

# Purpose:
# When installing this patch on the boot environment (i.e., ROOTDIR="/"),
# this patch will check to see if the required patches are installed
# on the system.  If a required patch is not present, the script issues
# a warning message and exits.  Otherwise, the patch is installed on the
# boot environment. 
#   
# When installing this patch on an alternate root environment, this script 
# checks the alternate root environment for the presence of required  patches.  
# If the required patches are not present, the script issues a warning message 
# and exits.  Otherwise, the patch is installed on the  alternate boot 
# environment. 


OBSOLETED_PATCH="False"
PATCH_ARCH=`/usr/bin/uname -p`

# DEFINE FUNCTIONS

cleanup()
{

rm -rf /var/tmp/hitlist* > /dev/null 2>&1

}


req_patch_check()
{

REQ_PATCH_LIST=$1

for Req_PID in $REQ_PATCH_LIST  ; do

   CURRENT_BASE_PID=`expr //$Req_PID : '.*\([1-9][0-9][0-9][0-9][0-9][0-9]\)'`
   CURRENT_REV=`expr //$Req_PID : '.*-\(.*\)'`

   patchadd -R ${ROOTDIR} -p | grep "Patch: $CURRENT_BASE_PID" > /var/tmp/hitlist.$$

   INSTALLED_PID="`grep "$CURRENT_BASE_PID" /var/tmp/hitlist.$$ | nawk -F: '{print $2}' | nawk -F' ' '{print $1}' | sort -r | head -n 1`"

   INSTALLED_REV=`expr //$INSTALLED_PID : '.*-\(.*\)'`

        patchadd -R ${ROOTDIR} -p | grep "Obsoletes:" | nawk -F: '{print $3}' | sed -e 's/Requires//g' | grep "$CURRENT_BASE_PID" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
          OBSOLETED_PATCH="True"
        fi

        if [ "$ROOTDIR" = "/" ]; then

             if  ( [ -n "$INSTALLED_PID" ]  && [ $INSTALLED_REV -lt $CURRENT_REV ] && [ "$OBSOLETED_PATCH" = "False" ] ) || ( [ -z "$INSTALLED_REV" ] && [ "$OBSOLETED_PATCH" = "False" ] )  ; then
        cat <<EOF

   Patch $Req_PID (or greater) must first be installed on the
   target system. This task needs to be performed before 
   continuing with the installation. 

EOF
             ABORT="Y"
             fi

         else   # perform alternate root check

             if  ( [ -n "$INSTALLED_PID" ]  && [ $INSTALLED_REV -lt $CURRENT_REV ] && [ "$OBSOLETED_PATCH" = "False" ] ) || ( [ -z "$INSTALLED_REV" ] && [ "$OBSOLETED_PATCH" = "False" ] )  ; then
        cat <<EOF

   Patch $Req_PID (or greater) must first be installed on the
   alternate root. This task needs to be performed before 
   continuing with the installation. 

EOF
             ABORT="Y"
             fi
         fi
done

  cleanup

if [ "$ABORT" = "Y" ]; then
   cat <<EOF

   Aborting Installation ...

EOF

exit 1
fi

}

check_version () {

if [ "$PATCH_CLIENT_VERSION" != "" ]; then
         OS_VERSION="5.${PATCH_CLIENT_VERSION}"
else
         OS_VERSION=`/usr/bin/uname -r`
fi

case $OS_VERSION in
         5.6|5.7|5.11)
                         cat <<EOF

   You are attempting to install on a Solaris $OS_VERSION system.

   This patch can only be installed on a Solaris 5.8, 5.9, or 5.10 
   system.  Exiting installation ... 

EOF
                         exit 1
                 ;;
         5.8|5.9|5.10)
                         return 0 
                 ;;
esac
}

# MAIN


check_version


if [ "$PATCH_ARCH" = "sparc" ]; then

     case $OS_VERSION in
              5.8)
                             req_patch_list="108987-15 110934-21"                     
                      ;;
              5.9)
                             req_patch_list="112951-10 113713-18 114482-04"                       
                      ;;
              5.10)
                             req_patch_list="119254-11"
                      ;;
     esac
fi

if [ "$PATCH_ARCH" = "i386" ]; then

     case $OS_VERSION in
              5.8)
                             req_patch_list="108988-15 110935-21 111307-07"
                      ;;
              5.9)
                             req_patch_list="114568-17 120465-01 114194-07 114483-04"
                      ;;
              5.10)
                             req_patch_list="117435-01 119255-11"
                      ;;
     esac
fi

# If Installing on an Alternate ROOT

   req_patch_check "$req_patch_list"

exit 0
