#!/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 configure script template for use with all patches.           #
#------------------------------------------------------------------#
# This file is optional for all patches.  The purpose of this      #
# script is to perform patch installation actions that cannot be   #
# accomplished by simple unconditional file extraction from the    #
# software source media.                                           #
#------------------------------------------------------------------#

_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

# Check for /var/sam/lock
if [ ! -d /var/sam/lock ]; then
    mkdir /var/sam/lock
fi
# Force the chmog in case this directory was
# previously created 555 instead of 755
chmog 755 bin bin /var/sam/lock

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 install.

    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.

cp_uncond /etc/sam/reg_da.db
cp_uncond /etc/sam/reg_files.db
cp_uncond /etc/sam/reg_tm.db

# If the dirs database was saved, attempt to restore the databases to the
# state before the fileset was loaded, by invoking "samreg -r" on
# partner directories.  Unregister first, since there are conditions
# (multiple SAM patches being installed from the same depot, for
# example) in which the same directory may be registered twice, which samreg
# complains about.

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
