#!/bin/ksh

#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

pkg=SUNWopenssl-libraries
rmpkg=SUNWusb
addpkg=SUNWckr
kbtrans=/kernel/misc/sparcv9/kbtrans
PKGCOND=/usr/bin/pkgcond

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
}

ExecuteALLCmds()
{
 relocate_kbtrans
 save_kbtrans_pkgmap
}


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.
      ExecuteALLCmds
      return 0
  fi

  if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
      # Execute commands specific to the mini-root
      ExecuteALLCmds
      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

}

relocate_kbtrans() {


        if [ ! -f $ROOTDIR/$kbtrans ] ; then
                return
        fi

	## This patch will add kbtrans to SUNWckr.  Manually remove it from SUNWusb
	## if it exists
	##
	Pgk=`pkgchk -R $ROOTDIR -l -p $kbtrans | grep $rmpkg`
	if [ "$?" =  "0" ] ; then
		removef -R $ROOTDIR $rmpkg $ROOTDIR/$kbtrans 1>/dev/null 2>&1
		removef -R $ROOTDIR -f $rmpkg
		installf -R $ROOTDIR $addpkg $ROOTDIR/$kbtrans
		installf -R $ROOTDIR -f $addpkg
	fi
}


##
## Save kbtrans entry
##
## Address 6343544 by using workaround 6312956 - removef doesn't update zones
## saved pkgmap files. We'll use cp -p to preserve permissions on the pkgmap
## file. Even though kbtrans may have moved from SUNWusb to SUNWckr, the
## pkgmap still needs to be cleaned up after the package switch is complete.
##

save_kbtrans_pkgmap () {

        rmpkg=SUNWusb
	kbtrans_entry=kernel/misc/sparcv9/kbtrans
        mypatchid=${PatchNum}

        cd $ROOTDIR/var/sadm/pkg/$rmpkg/save/pspool/$rmpkg
        ## Until removef/installf is fixed, we'll check if the entry is
        ## still in the origin package's pkgmap

        if grep "$kbtrans_entry" pkgmap >/dev/null
        then
            ## Save kbtrans entry in backoutpkgmap for postbackout restore
            backoutpkgmap=$ROOTDIR/var/sadm/patch/$mypatchid.savekbtrans.$rmpkg.undo.pkgmap
            cp -p pkgmap $backoutpkgmap
            grep "$kbtrans_entry" pkgmap > $backoutpkgmap

            ## Remove kbtrans entry in present SUNWusb pkgmap to clean up
            cp -p pkgmap pkgmap.tmp
            grep -v "$kbtrans_entry" pkgmap > pkgmap.tmp
            mv pkgmap.tmp pkgmap

        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

if pkgparam -R $ROOTDIR $pkg > /dev/null 2>&1 ; then
	exit 0
else
	exit 1
fi
