#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 18.3 $ generated on Thu Jun  7 19:16:35 EDT 2001
#
# (c)Copyright 1983-2001 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD preinstall script template for 11.X patches.                  #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  The purpose of this #
# script is to prepare the system for installation of the patch.   #
#------------------------------------------------------------------#

_PATCHID=PHCO_23876

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

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

. $UTILS
exitval=$SUCCESS

# PHCO_19047 was an anomaly in not using the newconfig utility for
# installing registration database files.  So when removing the
# current patch, the newconfig copies that we restore are not from
# PHCO_19047, but from the previous patch or original SAM
# installation.  This will break SAM when the database files don't
# match the *.tm and *.da files they refer to.  So if PHCO_19047 has
# been applied, let's abandon newconfig and instead restore the
# copy that we've now started saving during preinstall.  The saved
# copies are not unsullied by partner registrations like the
# newconfig copies are, but in postremove we'll unregister partner
# apps before re-registering them.  Since partner apps might have
# been unregistered after the current patch was installed, the
# saved database files may contain entries that we no longer know
# how to unregister.  This is unavoidable but is limited to the
# case of PHCO_19047 preceding this patch.

# The case we care about is when PHCO_19047 is present and applied 
# (not committed or superseded).

STATE=$(swlist -l patch PHCO_19047.SAM 2>/dev/null)
PRESENT=$?
APPLIED=$(echo "$STATE" | grep applied)
if [[ $PRESENT -eq 0 && -n "$APPLIED" ]]
then
    SAVEDIR=${SW_ROOT_DIRECTORY%/}/etc/sam/save.$_PATCHID
    mkdir -p $SAVEDIR
    if [[ $? -ne 0 ]]
    then
	echo "ERROR:   Cannot create directory $SAVEDIR".

#       Note: non-zero exit of preinstall will not abort the install
#       process unless a kernel fileset is selected.

	exit $FAILURE
    fi

    for f in reg_da.db reg_tm.db reg_files.db
    do
	if [[ ! -f ${SW_ROOT_DIRECTORY%/}/etc/sam/$f ]]
	then
	    echo "ERROR:   SAM registry file ${SW_ROOT_DIRECTORY%/}/etc/sam/$f is missing"
	    exit $FAILURE
	fi
    done

    for f in reg_da.db reg_tm.db reg_files.db
    do
	cp ${SW_ROOT_DIRECTORY%/}/etc/sam/$f $SAVEDIR/$f
    done

fi

exit $exitval
