#! /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
PKGCOND=/usr/bin/pkgcond

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
}

LocalZones()
{
# 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
}

ExecuteALLCmds()
{
  restore_links
  return 0
}

determine_pkgcond () {

  if $PKGCOND  is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
      # Execute non-global whole root zone commands.
      # Should be same action as the default action.
      return 0
  fi

  if $PKGCOND  is_nonglobal_zone > /dev/null 2>&1 ; then 
      # Execute non-global zone commands. Should be no action here
      return 0
  fi

  if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
      # Execute commands applicable to patching the mini-root.
      # There are usually no actions to take here since your patching
      # the mini-root on an install server.
      return 0
  fi

  if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
      # Execute commands specific to the mini-root
      return 0
  fi

  if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
      # Execute commands specific to diskless client
      return 0
  fi

  if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
      # Execute commands specific to an alternate root
      ExecuteALLCmds
      return 0
  fi

  if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
      # In a global zone and system is mounted on /.
      # Execute all commands.
      ExecuteALLCmds
      return 0
  fi

}


# Main

ZONENAME=global

[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

if [ -x $PKGCOND ] ; then
  determine_pkgcond && exit 0 || exit 1
else
  CheckZones
  if [ "${GLOBAL_ZONE}" = "true" ]; then
       ExecuteALLCmds
  else
   LocalZones
  fi
fi
exit 0
