#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 9.5 $ generated on Tue Jun 22 09:40:01 PDT 1999
#
# (c)Copyright 1983-1996 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD checkremove script template for 11.X patches.                 #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  The purpose of this #
# script is to ensure that there is no patch specific restriction  #
# to removing the selected software from the target system.        #
#------------------------------------------------------------------#

_PATCHID=PHKL_18543

UTILS="/usr/lbin/sw/control_utils"

if [ ! -f $UTILS ]; then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

. $UTILS
exitval=$SUCCESS

#####
##### Insert any code you may require at this point in the script.
##### Pay attention to the exitval variable and set appropriately
##### in your code.  If you are unsure as to what types of operations
##### are legal in this file and what types aren't, consult the 
##### "Guidelines for SD Control Scripts" document.
#####
##### START_CUSTOMIZATION_HERE

# START Check for 11.X split patch and dependencies                #
#------------------------------------------------------------------#
Filesets="
AdvJournalFS.VXFS-ADV-KRN
JournalFS.VXFS-BASE-KRN
JournalFS.VXFS-PRG
OS-Core.CORE-KRN
OS-Core.CORE2-KRN
OS-Core.KERN2-RUN
ProgSupport.C-INC
"


#########################################################################
# This script part is to replace the whole custom part of the script with
# another patch if needed.  At initial patch release time there is no 
# basepatch_utils file
#

BASEPATCH_UTILS="$SW_ROOT_DIRECTORY/usr/lbin/patch/basepatch_utils"

if [ -f $BASEPATCH_UTILS ] 
then
    # replace with a fixed version if needed
    print -u2 "NOTE:    $BASEPATCH_UTILS installed."
    print -u2 "         Using control script functions from this file."
    . $BASEPATCH_UTILS
    checkremove_cntl || exitval=$? # retain a non-zero return value
    exit $exitval
fi # [ -f  ]

PFsetList=""

for Fileset in $Filesets
do
  Fset=${Fileset#*.}
  PFset=$_PATCHID.$Fset
  pstate=$(get_install_state $PFset)
  is_software_selected $PFset; pselected=$?
  if [ $pselected -ne 0 ] 
  then
    if [ "$pstate" = configured -o "$pstate" = installed ]
    then
      PFsetList="${PFset}\n$PFsetList"
      exitval=$FAILURE
    fi
  fi
done

if [ -n "$PFsetList" ]
then
  print -u2 "ERROR:   The following filesets are currently installed or"
  print -u2 "         configured, but not selected for removal:"
  for eachPFset in $PFsetList
  do
    print -u2 "           $eachPFset"
  done
  print -u2 "         All installed and configured filesets for patch"
  print -u2 "         $_PATCHID must be removed at the same time."
fi

# Now check for patches with dependencies on this patch

anyCommitted=no

dependencies=$(
  swlist -a prerequisite -a patch_state -l fileset \*.\*,c=patch |
  awk -v _PATCHID=${_PATCHID} '
    { if (match($0, _PATCHID) == 0) { continue } ;# did not find patch name
       sub("\..*", "", $1)
       if ($1 == _PATCHID) { continue }           # skip patch itself
       if ($NF == "committed")
           printf "%s (committed)\n", $1      
       else
           print $1
    }' | sort -nut "_" -k2) # yields \n delimited string

if [ -n "$dependencies" ]
then
  print -u2 "ERROR:   The following patches depend on $_PATCHID:"

  echo "$dependencies" |
  while read dependency
  do
    if echo $dependency | grep -qF "committed" ; then
      anyCommitted=yes
    fi
    print -u2 "           $dependency"
  done

  if [ "$anyCommitted" = "yes" ]
  then
    print -u2 "NOTE:    One or more patches in the above list have been"
    print -u2 "         \"committed\", and thus can not be removed from the"
    print -u2 "         system.  Therefore, patch $_PATCHID can not be removed"
    print -u2 "         from this system via swremove(1M)."

  else
    print -u2 "         Each must be removed before removing $_PATCHID."
    print -u2 "         To reduce the downtime while removing these patches,"
    print -u2 "         you can duplicate the following swmodify(1M)"
    print -u2 "         command for each patch to prevent a system reboot when"
    print -u2 "         each patch is removed:"
    print -u2 "                swmodify -a is_reboot=false $dependency.\*"
  fi

  print -u2 "NOTE:    Setting the \"enforce_dependencies\" option to \"off\""
  print -u2 "         is not supported when installing or removing \"PHKL\""
  print -u2 "         prefixed patches."

  exitval=$FAILURE
fi

#------------------------------------------------------------------#
# DONE with Check for 11.X split patch and dependencies            # 


exit $exitval
