#!/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 postremove script template for 11.X patches.                  #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  The purpose of this #
# script is to perform any necessary cleanup actions after the     #
# patch's files have been removed.                                 #
#------------------------------------------------------------------#

_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

# libipsectp.a contains the current version of the ARPA
# transport stubs. Removing a transport patch may change the
# contents of these stubs, so we must ensure that libipsec.a
# gets updated appropriately.
#
# If libipsec.a exists and contains more than one module, then
# this system  has IPSEC installed, and we copy the current stubs
# into /var/adm/ipsec/libipsec.a where the IPSEC removal scripts
# can find it.
#
# If libipsec.a contains only a single module, we replace it
# with the contents of libipsectp.a  which always contains the
# current version of the ARPA Transport stubs. Otherwise we
# do nothing.
#
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 t_modules
cd /usr/conf/lib

let t_modules=`xport_ar_modules libipsectp.a`
let i_modules=`xport_ar_modules libipsec.a`
if (( i_modules < 0 || t_modules < 0 )); then # Invalid archive(s)
  exitval=$FAILURE
else
  if (( t_modules > 0 )); then # libipsectp.a exits
    if (( i_modules <= 1 )); then # IPSEC isn't installed
      cp libipsectp.a libipsec.a # restore stubs from previous patch
    else # IPSEC is installed 
      if [[ -f /var/adm/ipsec ]]; then
        rm -f /var/adm/ipsec
      fi
      if [[ ! -d /var/adm/ipsec ]]; then
        mkdir -p /var/adm/ipsec
        chmod 755 /var/adm/ipsec
      fi
      cp libipsectp.a /var/adm/ipsec/libipsec.a
    fi
  else # No IPSEC transport patch, that means transport doesn't know
       # about IPSEC, so libipsec.a and libipsectp.a are unecessary.
    rm -f libipsec.a
    if [[ -f libipsectp.a ]]; then
      rm -f libipsectp.a
      # swmodify -ux files=/usr/conf/lib/libipsectp.a Networking.NET2-KRN
    fi
  fi
fi
cd $pwd
unset -f xport_ar_modules

exit $exitval
