#! /sbin/sh
########
# Product: Ignite-UX
# Fileset: BOOT-KERNEL-PA
# preinstall
# @(#) $Revision: 10.7 $
########
#
# (c) Copyright Hewlett-Packard Company 2002
#
########################################################################

##########################################################################
# This script is used to preserve any user-configurable information that
# may have been stored inside the install RAM filesystem.  This information
# will be put back inside the new filesystem by the configure script.
#
UTILS=/usr/lbin/sw/control_utils
if [ -f $UTILS ]
then
    . $UTILS
else
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

exitval=$SUCCESS


INST_DIR=/opt/ignite
BOOT_DIR=${INST_DIR}/boot
TMP_CFG=${BOOT_DIR}/fs_cfg.old
DEF_CFG=${BOOT_DIR}/fs_cfg.def
INSTALLFS=${BOOT_DIR}/INSTALLFS

PATH=${INST_DIR}/bin:$PATH

#
# Make sure the older BOOT-KERNEL(s) are removed.
#
p=Ignite-UX
fs=Ignite-UX.BOOT-KERNEL
swlist -l fileset $fs >/dev/null 2>&1
if [ $? -eq 0 ]
then
    fs_specs=`swlist -a software_spec -l fileset $fs | grep $fs | \
        awk ' { print $2 }' | sed "s%$fs,%%" `
    no_boot_kernels=`echo $fs_specs | wc -w`
    if [ $no_boot_kernels -gt 0 ]
    then
        # Something needs to be removed.
        for spec in $fs_specs
        do
                p_arch=`echo $spec | sed "s%,fr=.*%%" `
                no_fs=`swlist -l fileset $p,$p_arch | grep "$p\." \
                         | wc -l`
                if [ $no_fs -eq 1 ]
                then
                    swmodify -u $fs,$spec $p,$p_arch >/dev/null 2>&1
                else
                    swmodify -u $fs,$spec >/dev/null 2>&1
                fi
        done
        swlist -l fileset $fs >/dev/null 2>&1
        if [ $? -eq 0 ]
        then
            echo "WARNING: Unable to remove $fs"
            echo "         Do it manually after the install"
            exitval=$WARNING
        fi
    fi
fi
#
# First check to make sure that BOOT-KERNEL[-PA] has been previously
# loaded, if not, then there is nothing to do in this script.
#

if [ ! -f $INSTALLFS ]
then
    exit $exitval
fi

#
# Check to see if INST_DIR is on a NFS mount.  This is the case for
# diskless clients (at least).
#
if [ "$(find $INST_DIR -fstype nfs -type d -print)" = $INST_DIR ]
then
    echo "\
NOTE:    The Ignite-UX product will not be configured on diskless clients."
    exit $SUCCESS
fi

#
# determine the architecture we are running on and set the location of
# native binaries to link to /opt/ignite/bin and /opt/ignite/lbin
############
case $(uname -m) in
    ia64*) bindir=/opt/ignite/binia
           lbindir=/opt/ignite/lbinia
           ;;
        *) bindir=/opt/ignite/binpa
           lbindir=/opt/ignite/lbinpa
           ;;
esac
binlink=/opt/ignite/bin
lbinlink=/opt/ignite/lbin

#
# do we have the instl_adm command? look in linked and native bin
# directories
# if not exit
#
have_instl_adm=0
if [[ -x ${binlink}/instl_adm ]];then
  instladm=${binlink}/instl_adm
  have_instl_adm=1
elif [[ -x ${bindir}/instl_adm ]];then
  instladm=${bindir}/instl_adm
  have_instl_adm=1
fi

#
# if no instl_adm exit
if (( !  have_instl_adm ));then
  exit ${SUCCESS}
fi

#
# Now that we know that instl_adm and the INSTALLFS exist, check
# to see if any config info is in the INSTALLFS.
#

${instladm} -F $INSTALLFS -d > $TMP_CFG
if [ "$?" != 0 ]
then
    echo "\
WARNING: instl_adm(1M) failed, any user-modified configuration info from the
         cold-install RAM filesystem will not be saved."
    exit $WARNING
fi

# If there was no info in the INSTALLFS, or if the info is the same
# as when it was first installed, then remove it and let the
# configure script set the defaults.
if [ ! -s $TMP_CFG ]
then
    # remove if empty.
    rm -f $TMP_CFG
else
    if [ -f $DEF_CFG ] && cmp -s $DEF_CFG $TMP_CFG
    then
        # remove if the same as old defaults.
        rm -f $TMP_CFG
    fi
fi

exit $exitval
