#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 12.4 $ generated on Mon Sep 18 21:21:50 PDT 2000
#
# (c)Copyright 1983-2000 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=PHNE_22397

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

# We need to verify that the removal of this transport patch
# won't leave us with IPSec installed without the transport patch
# upon which it is dependent.

function xport_ar_modules
{
  # This function returns the number of modules in an archive
  # library, or -1 if the file isn't an archive.  If the file
  # doesn't exist, a value of 0 is returned.
  typeset library=$1
  if [[ -f $library ]]; then
    if file $library | grep -e ' archive file ' >/dev/null 2>&1 ; then
      # The file has been butchered, don't know what to do.
      print -u2 "ERROR: /usr/conf/lib/$library is NOT an archive file."
      print "-1"
    else
      # Determine if the library contains stubs or not
      ar -t $library 2>/dev/null | wc -l
    fi  
  else
    print "0" 
  fi
}

typeset pwd=$(pwd)
typeset i_modules size ip_file
typeset error=0
cd /usr/conf/lib

let i_modules=`xport_ar_modules libipsec.a`
if (( i_modules < 0 )); then # Invalid file
  exitval=$FAILURE
else
  if (( i_modules > 1 )); then # We have IPSEC installed
    typeset ipsec_hp_o=/var/adm/sw/save/$_PATCHID/NET2-KRN/usr/conf/lib/libipsectp.a/ipsec_hp.o
    typeset libipsectp_a=/var/adm/sw/save/$_PATCHID/NET2-KRN/usr/conf/lib/libipsectp.a
    # Determine if the previous patch contains stubs to support IPSec.
    if [[ -f $ipsec_hp_o || -f $libipsectp_a ]]; then
      if [[ -f $libipsectp_a ]]; then
	ipsec_file=$libipsectp_a
      else
	ipsec_file=$ipsec_hp_o
      fi
      let size=$(ls -l $ipsec_file | awk '{printf("%s\n",$5); exit 0}')
      if (( size == 0 )); then
        error=1
      fi
    else
      error=1 # The file doesn't exist
    fi
  fi
fi

if (( error != 0 )); then
  print -u2 "ERROR: Cannot remove $_PATCHID until IPSec/9000 has been removed"
  exitval=$FAILURE
fi

cd $pwd
unset -f xport_ar_modules
exit $exitval
