#!/bin/ksh

kbtrans_patch=118859-01

rmpkg=SUNWckr
addpkg=SUNWusb
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()
{

 restore_kbtrans
 restore_kbtrans_pkgmap
 remove_ba_start_method

}


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

}

last_patch() {

	## returns the number of patches installed at or above this rev.

	root_dir=${ROOTDIR:-/}

	## parse id and rev
	pid=`echo $1 | cut -d\- -f1`
	prev=`echo $1 | cut -d\- -f2`
	patch_cnt=0

	## get all installed refernces to the installed patch base id	
	installed_patches=`patchadd -p -R $root_dir -p | sed -n -e 's/Req.*//' -e 's/[a-zA-Z]*://g' -e 's/,//g' -e "/$pid/p"`
	
	for x in $installed_patches ; do
		base=`echo $x  | cut -d\- -f1`
		rev=`echo $x | cut -d\- -f2`
		if [ $pid -eq $base ] && [ $rev -ge $prev ] ; then 
			## count all installed patches includeing this patch
			patch_cnt=$(($patch_cnt + 1))
		fi
	done
	
	return $patch_cnt
}


restore_kbtrans() {


	## This patch will add kbtrans to SUNWckr.  Manually remove it from SUNWusb
	## if it exists
	##

	if [ ! -f $ROOTDIR/$kbtrans ] ; then
		return
	fi
	last_patch $kbtrans_patch
	if [ "$?" = "0" ] ; then 
		pkg=`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 1>/dev/null 2>&1
			installf -R $ROOTDIR -f $addpkg
		fi
	fi
}

##
## restore_kbtrans_pkgmap
##
## Address 6343544 by restoring the zones saved pkgmap entry to SUNWusb which
## was removed by the prepatch script thanks to workaround for 6312956.

restore_kbtrans_pkgmap () {

mypatchid=${PatchNum}
savedpkgmap=$ROOTDIR/var/sadm/pkg/$addpkg/save/pspool/$addpkg/pkgmap
backoutpkgmap=$ROOTDIR/var/sadm/patch/$mypatchid.savekbtrans.$addpkg.undo.pkgmap
kbtrans_entry=kernel/misc/sparcv9/kbtrans

        if test -s $backoutpkgmap
        then

        ## We did save something.  However, installf might have been fixed
        ## in the meantime, we check whether we still need the workaround.

                if grep "$kbtrans_entry" $savedpkgmap >/dev/null; then
                :
                else
                cat $backoutpkgmap >> $savedpkgmap
		rm -f $backoutpkgmap
                fi
        fi

}

# Main

ZONENAME=global


##
## Address 6348423 by removing boot-archive's start method from repository
## Address 6359117 by executing this call in certain zones conditions:
## is_whole_root_nonglobal_zones, is_global_zone, is_alternative_root
##

remove_ba_start_method () {
typeset -r ku_w_ba=118822-21
if last_patch $ku_w_ba ; then
	echo "exit 0" > $ROOTDIR/lib/svc/method/boot-archive
	chmod +x $ROOTDIR/lib/svc/method/boot-archive
fi
}

[ -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

