#! /bin/sh
# This script reestablishes hard links that were broken after 
# the removal and reinstallation of the initd file. 
#

#
# Call installf/removef with the proper options to handle rerooted
# installations.

PKGPATH=${ROOTDIR}/var/sadm/pkg
SPOOLPATH=/save/pspool
pkg=SUNWpkgcmdsu

if [ "$ROOTDIR" != "" ]; then
        INSTALLF="installf -R ${ROOTDIR}"
        REMOVEF="removef -R ${ROOTDIR}"
else
        INSTALLF="installf"
        REMOVEF="removef"
fi

# When the source file was restored above, the inode number 
# has changes.  Therefore, we must reestablish the hard links 
# and update the contents database with this information.
CheckZones()
{
        if [ -x /usr/bin/zonename ]; then
                ZONENAME=`/usr/bin/zonename`
                if [ ${ZONENAME} = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
                # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}


GlobalZones()
{
  restore_links
  return 0
}

NonGlobalZones()
{
# Any local zone stuff here,
# typically this fuction is not used
        return 0
}

restore_links () {

pkginfo -R $ROOTDIR -q SUNWpkgcmdsu 
if [ $? -eq 0 ]
then
        ${REMOVEF} SUNWpkgcmdsu /usr/sbin/pkgask > /dev/null 2>&1
        ${REMOVEF} SUNWpkgcmdsu /usr/sbin/removef > /dev/null 2>&1
        ${REMOVEF} -f SUNWpkgcmdsu || exit 2

#     According to the installf man page, links should be specified as  path1=path2.  path1 indicates
#     the destination and path2 indicates the source file.  Can use "../../etc ..." notation to avoid 
#     having to have to specify ROOTDIR.

        ${INSTALLF} SUNWpkgcmdsu /usr/sbin/pkgask=../../usr/sbin/pkgadd l > /dev/null 2>&1
        ${INSTALLF} SUNWpkgcmdsu /usr/sbin/removef=../../usr/sbin/installf l > /dev/null 2>&1
        ${INSTALLF} -f SUNWpkgcmdsu || exit 2
        [ -d ${PKGPATH}/${pkg}/${SPOOLPATH}/${pkg} ] && {
           cd ${PKGPATH}/${pkg}/${SPOOLPATH}/${pkg}
           egrep -v "removef|pkgask" pkgmap > pkgmap.tmp
           echo "1 l none usr/sbin/removef=../../usr/sbin/installf" >> pkgmap.tmp
           echo "1 l none usr/sbin/pkgask=../../usr/sbin/pkgadd"    >> pkgmap.tmp
           mv pkgmap.tmp pkgmap
        }

fi
}

# Main

CheckZones

if [ ${GLOBAL_ZONE} = "true" ]; then
   GlobalZones
else
   NonGlobalZones
fi
 
exit 0
