#!/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 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=PHCO_23876

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

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

. $UTILS
exitval=$SUCCESS

########
# cp_uncond
#       $1:     File that has to be copied.
#
# Unconditionally copy a file from newconfig to its destination.
# Issue default messages to the log file containing the name of a file put
# on the system.
#
cp_uncond ()
{
    USRDIR=$SW_LOCATION
    [[ $SW_LOCATION = "/" ]] && USRDIR=/usr
    NEWCONFIG=${SW_ROOT_DIRECTORY%/}${USRDIR}/newconfig

    if [[ -e ${NEWCONFIG}${1} ]]
    then
        cp_retain ${NEWCONFIG}${1} ${1}
        echo "NOTE:    A new version of \"${1}\""
        echo "         has been placed on the system."
    else
        echo "ERROR:   An updated version of \"${1}\" was expected to"
        echo "         be delivered in /usr/newconfig but is missing."
    fi

    return 0
}

########
# my_cp_retain --
#       same function as cp_retain, but without adding $2 to the IPD
#       $1:     copy fromfile
#       $2:     copy destination path
#
#    unconditionally copy $1 to $2 and set attributes
#
my_cp_retain ()
{
    if [[ $# -ne 2 ]]
    then
        echo "Usage: my_cp_retain source target" >&2
        return 1
    fi

    _dirname=${2%/*}
    [[ -z $_dirname ]] && _dirname=/
    r_mkdir 755 bin bin $_dirname
    cp -p $1 $2 || return 1
    _ownership=`get_owner_group $1` || return 1
    chmog 0 $_ownership "$2" || return 1
    return 0
}


exitval=$SUCCESS

rm -f ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db.old

if [ -f ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db ]
then

#   Save the dirs database if it exists.  It's never delivered, so it
#   can't be overwritten during removal.

    my_cp_retain ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db \
                 ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db.old
fi

# Overwrite the database files; we will restore any partner
# information by calling "samreg -r" later.  These files are
# necessarily delivered in /usr/newconfig for diskless clients
# but do not make use of the "newconfig" change control mechanism.

# 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, we need
# unregister applications that were registered when the current patch
# was installed, then re-register those in the more recent reg_dirs.db.

SAVEDIR=${SW_ROOT_DIRECTORY%/}/etc/sam/save.$_PATCHID
if [[ -d $SAVEDIR ]]
then

    for f in reg_da.db reg_tm.db reg_files.db
    do
	if [[ ! -f $SAVEDIR/$f ]]
	then
	    echo "ERROR:   Saved SAM registry file $SAVEDIR/$f is missing"
	    exit $FAILURE
	fi
    done

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

#   In case a partner application has been removed since this patch was
#   installed, the information samreg needs (the contents of the actual
#   application files referenced in the database) to unregister it in 
#   the saved database is no longer available.  Instead, search 
#   reg_files.db for non-existent files and remove any corresponding
#   entries from reg_*.db.

    for f in reg_da.db reg_tm.db reg_files.db
    do
	cp -p ${SW_ROOT_DIRECTORY%/}/etc/sam/$f /var/tmp/$f.$$
    done

    awk '/^[ 	]*FILE/ {printf "%s ", $2}
	 /^[ 	]*NUMBER/ {printf "%s\n", $2}' /var/tmp/reg_files.db.$$ |
    while read fname fnum
    do
	[[ -f $fname ]] && continue

#       File doesn't exist, so remove all entries from reg_tm.db and
#       reg_db.da that have a matching file number in field 2.

	/usr/sam/lbin/samlog NOTE "Postremove for $_PATCHID found reg_files.db entry for non-existent file."

	awk -vfname=$fname '
	    /FILE/ {if ($2 == fname) strip = 1
		    else             strip = 0}
		   {if (strip == 0) print}' \
	    < /var/tmp/reg_files.db.$$ \
	    > /var/tmp/reg_files.db.$$.tmp
	mv /var/tmp/reg_files.db.$$.tmp /var/tmp/reg_files.db.$$

	awk -vfnum=$fnum '{if ($2 != fnum) print}' \
	    < /var/tmp/reg_da.db.$$ \
	    > /var/tmp/reg_da.db.$$.tmp
	mv /var/tmp/reg_da.db.$$.tmp /var/tmp/reg_da.db.$$

	awk -vfnum=$fnum '{if ($2 != fnum) print}' \
	    < /var/tmp/reg_tm.db.$$ \
	    > /var/tmp/reg_tm.db.$$.tmp
	mv /var/tmp/reg_tm.db.$$.tmp /var/tmp/reg_tm.db.$$
	
    done

    for f in reg_da.db reg_tm.db reg_files.db
    do
	mv /var/tmp/$f.$$ ${SW_ROOT_DIRECTORY%/}/etc/sam/$f
	rm -f /var/tmp/$f.$$
    done


    rm -rf $SAVEDIR

else

    for f in reg_da.db reg_tm.db reg_files.db
    do
#       Configure uses cp_uncond() here, but that function
#       implicitly calls IPD_addfile(), which leaves
#       /var/adm/sw/swmodify_rtmp/<PRODUCT>/<FILESET>/addlist, which
#       doesn't get cleaned up by postremove due to swremove defect, which
#       causes errors during the next swinstall.

	my_cp_retain ${SW_ROOT_DIRECTORY%/}/usr/newconfig/etc/sam/$f \
		     ${SW_ROOT_DIRECTORY%/}/etc/sam/$f
    done

fi


# If the dirs database was saved, attempt to restore the databases to the
# state before the fileset was removed, by invoking "samreg -r" on
# partner directories.  Unregister first to avoid duplication errors
# from samreg.

if [ -f ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db.old ]
then
    for DIR in `cat ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db.old`
    do
        ${SW_ROOT_DIRECTORY%/}/usr/sam/bin/samreg -u $DIR >/dev/null 2>&1
        ${SW_ROOT_DIRECTORY%/}/usr/sam/bin/samreg -r $DIR || {
	    REGFAIL=$FAILURE
	    exitval=$FAILURE
	}
    done

#   Rely on samreg for logging of any errors.  If samreg fails, keep
#   reg_dirs.db.old around for triage.

    [[ "$REGFAIL" != "$FAILURE" ]] &&
    rm -f ${SW_ROOT_DIRECTORY%/}/etc/sam/reg_dirs.db.old

fi

# Create rmuser.excl.n.  Rename it to rmuser.excl if rmuser.excl
# doesn't exist.

cut -d: -f1 /usr/newconfig/etc/passwd > /etc/sam/rmuser.excl.n 2> /dev/null

if [[ ! -f /etc/sam/rmuser.excl ]]
then
    mv /etc/sam/rmuser.excl.n /etc/sam/rmuser.excl > /dev/null 2>&1
    if [[ $? -ne 0 ]]
    then
        exitval=$WARNING
        echo "WARNING: A problem occurred while moving \"/etc/sam/rmuser.excl.n\" to"
        echo "         \"/etc/sam/rmuser.excl\".  Correct the problem and move the"
        echo "         file manually."
    else
        echo "NOTE:    \"/etc/sam/rmuser.excl\" has been created."
    fi
else
    echo "NOTE:    \"/etc/sam/rmuser.excl\" exists and will not be replaced."
    echo "         The potential replacement rmuser.excl has been left in"
    echo "         \"/etc/sam/rmuser.excl.n\"."
fi

# Same as above for rmgroup.excl

cut -d: -f1 /usr/newconfig/etc/group > /etc/sam/rmgroup.excl.n 2> /dev/null

if [[ ! -f /etc/sam/rmgroup.excl ]]
then
    mv /etc/sam/rmgroup.excl.n /etc/sam/rmgroup.excl > /dev/null 2>&1 
    if [[ $? -ne 0 ]]
    then
        exitval=$WARNING
        echo "WARNING: A problem occurred while moving \"/etc/sam/rmgroup.excl.n\" to"
        echo "         \"/etc/sam/rmgroup.excl\".  Correct the problem and move the"
        echo "         file manually."
    fi
else
    echo "NOTE:    \"/etc/sam/rmgroup.excl\" exists and will not be replaced."
    echo "         The potential replacement rmgroup.excl has been left in"
    echo "         \"/etc/sam/rmgroup.excl.n\"."
fi

# Remove the kernel config table; SAM will re-create it with current
# settings when Kernel Parameters is next run.

if [[ -f /var/sam/boot.config ]]
then
    rm -f /var/sam/boot.config > /dev/null 2>&1
    if [[ $? -ne 0 ]]
    then
        exitval=$WARNING
        echo "WARNING: Unable to remove the file \"/var/sam/boot.config\".  Ensure"
        echo "         that the filesystem containing \"/var/sam/boot.config\" is not"
        echo "         mounted read-only and remove the file manually.  The file will"
        echo "         be re-created when SAM's Kernel Configuration Parameters"
        echo "         section is next run."
    fi
fi

exit $exitval
