#!/bin/ksh

kbtrans_patch=118860-01

rmpkg=SUNWckr
addpkg=SUNWusb
kbtrans=/kernel/misc/kbtrans

################################################################################
# If the $ROOTDIR/usr/bin/cputrack exists, then we need to remove the existing
# file and re-create the link.  This is to circumvent the bug in the patchadd
# that does not properly restore the hard-links ( the backout mechanism creates
# a regular file instead of hard link ).
################################################################################

GetBrokenLinks() {
cat << EOF
SUNWcsu usr/bin/newtask ../../usr/lib/isaexec
SUNWcsu usr/bin/nohup ../../usr/lib/isaexec
SUNWcsu usr/bin/prctl ../../usr/lib/isaexec
SUNWcsu usr/bin/prstat ../../usr/lib/isaexec
SUNWcsu usr/bin/ps ../../usr/lib/isaexec
SUNWcsu usr/bin/savecore ../../usr/lib/isaexec
SUNWcsu usr/bin/setuname ../../usr/lib/isaexec
SUNWcsu usr/bin/uptime ../../usr/lib/isaexec
SUNWcsu usr/bin/w ../../usr/lib/isaexec
SUNWcsu usr/sbin/add_drv ../../usr/lib/isaexec
SUNWcsu usr/sbin/modinfo ../../usr/lib/isaexec
SUNWcsu usr/sbin/modload ../../usr/lib/isaexec
SUNWcsu usr/sbin/modunload ../../usr/lib/isaexec
SUNWcsu usr/sbin/pbind ../../usr/lib/isaexec
SUNWcsu usr/sbin/prtconf ../../usr/lib/isaexec
SUNWcsu usr/sbin/psrset ../../usr/lib/isaexec
SUNWcsu usr/sbin/rem_drv ../../usr/lib/isaexec
SUNWcsu usr/sbin/swap ../../usr/lib/isaexec
SUNWcsu usr/sbin/sysdef ../../usr/lib/isaexec
SUNWcsu usr/sbin/update_drv ../../usr/lib/isaexec
SUNWcsu usr/sbin/whodo ../../usr/lib/isaexec
SUNWdtrc usr/sbin/dtrace ../../usr/lib/isaexec
SUNWdtrc usr/sbin/intrstat ../../usr/lib/isaexec
SUNWdtrc usr/sbin/lockstat ../../usr/lib/isaexec
SUNWdtrc usr/sbin/plockstat ../../usr/lib/isaexec
SUNWdtrp kernel/dtrace/amd64/fasttrap ../../../kernel/drv/amd64/fasttrap
SUNWdtrp kernel/dtrace/amd64/fbt ../../../kernel/drv/amd64/fbt
SUNWdtrp kernel/dtrace/amd64/lockstat ../../../kernel/drv/amd64/lockstat
SUNWdtrp kernel/dtrace/amd64/profile ../../../kernel/drv/amd64/profile
SUNWdtrp kernel/dtrace/amd64/sdt ../../../kernel/drv/amd64/sdt
SUNWdtrp kernel/dtrace/amd64/systrace ../../../kernel/drv/amd64/systrace
SUNWdtrp kernel/dtrace/fasttrap ../../kernel/drv/fasttrap
SUNWdtrp kernel/dtrace/fbt ../../kernel/drv/fbt
SUNWdtrp kernel/dtrace/lockstat ../../kernel/drv/lockstat
SUNWdtrp kernel/dtrace/profile ../../kernel/drv/profile
SUNWdtrp kernel/dtrace/sdt ../../kernel/drv/sdt
SUNWdtrp kernel/dtrace/systrace ../../kernel/drv/systrace
SUNWesu usr/bin/pargs ../../usr/lib/isaexec
SUNWesu usr/bin/pcred ../../usr/lib/isaexec
SUNWesu usr/bin/pfiles ../../usr/lib/isaexec
SUNWesu usr/bin/pflags ../../usr/lib/isaexec
SUNWesu usr/bin/pldd ../../usr/lib/isaexec
SUNWesu usr/bin/pmap ../../usr/lib/isaexec
SUNWesu usr/bin/ppgsz ../../usr/lib/isaexec
SUNWesu usr/bin/ppriv ../../usr/lib/isaexec
SUNWesu usr/bin/preap ../../usr/lib/isaexec
SUNWesu usr/bin/prun ../../usr/lib/isaexec
SUNWesu usr/bin/psig ../../usr/lib/isaexec
SUNWesu usr/bin/pstack ../../usr/lib/isaexec
SUNWesu usr/bin/pstop ../../usr/lib/isaexec
SUNWesu usr/bin/ptime ../../usr/lib/isaexec
SUNWesu usr/bin/ptree ../../usr/lib/isaexec
SUNWesu usr/bin/pwait ../../usr/lib/isaexec
SUNWesu usr/bin/pwdx ../../usr/lib/isaexec
SUNWesu usr/bin/sort ../../usr/lib/isaexec
SUNWipfu usr/lib/ipf/ipftest ../../../usr/lib/isaexec
SUNWipfu usr/sbin/ipf ../../usr/lib/isaexec
SUNWipfu usr/sbin/ipfs ../../usr/lib/isaexec
SUNWipfu usr/sbin/ipfstat ../../usr/lib/isaexec
SUNWipfu usr/sbin/ipmon ../../usr/lib/isaexec
SUNWipfu usr/sbin/ipnat ../../usr/lib/isaexec
SUNWipfu usr/sbin/ippool ../../usr/lib/isaexec
SUNWkey usr/share/lib/keytables/type_6/layout_00 ../../../../../usr/share/lib/keytables/type_6/us
SUNWkey usr/share/lib/keytables/type_6/layout_06 ../../../../../usr/share/lib/keytables/type_6/denmark
SUNWkey usr/share/lib/keytables/type_6/layout_07 ../../../../../usr/share/lib/keytables/type_6/finnish
SUNWkey usr/share/lib/keytables/type_6/layout_08 ../../../../../usr/share/lib/keytables/type_6/france
SUNWkey usr/share/lib/keytables/type_6/layout_09 ../../../../../usr/share/lib/keytables/type_6/germany
SUNWkey usr/share/lib/keytables/type_6/layout_0e ../../../../../usr/share/lib/keytables/type_6/italy
SUNWkey usr/share/lib/keytables/type_6/layout_0f ../../../../../usr/share/lib/keytables/type_6/japan
SUNWkey usr/share/lib/keytables/type_6/layout_10 ../../../../../usr/share/lib/keytables/type_6/korea
SUNWkey usr/share/lib/keytables/type_6/layout_12 ../../../../../usr/share/lib/keytables/type_6/netherlands
SUNWkey usr/share/lib/keytables/type_6/layout_13 ../../../../../usr/share/lib/keytables/type_6/norway
SUNWkey usr/share/lib/keytables/type_6/layout_16 ../../../../../usr/share/lib/keytables/type_6/portugal
SUNWkey usr/share/lib/keytables/type_6/layout_19 ../../../../../usr/share/lib/keytables/type_6/spain
SUNWkey usr/share/lib/keytables/type_6/layout_1a ../../../../../usr/share/lib/keytables/type_6/sweden
SUNWkey usr/share/lib/keytables/type_6/layout_1b ../../../../../usr/share/lib/keytables/type_6/swiss_french
SUNWkey usr/share/lib/keytables/type_6/layout_1c ../../../../../usr/share/lib/keytables/type_6/swiss_german
SUNWkey usr/share/lib/keytables/type_6/layout_1e ../../../../../usr/share/lib/keytables/type_6/taiwan
SUNWkey usr/share/lib/keytables/type_6/layout_20 ../../../../../usr/share/lib/keytables/type_6/uk
SUNWkey usr/share/lib/keytables/type_6/layout_21 ../../../../../usr/share/lib/keytables/type_6/us
SUNWmdb usr/bin/adb ../../usr/lib/isaexec
SUNWmdb usr/bin/mdb ../../usr/lib/isaexec
SUNWrcapu usr/lib/rcap/rcapd ../isaexec
SUNWrmwbu usr/sadm/lib/wbem/rds ../../../lib/isaexec
SUNWscpu usr/ucb/ps ../../usr/lib/isaexec
SUNWtnfc usr/bin/prex ../lib/isaexec
SUNWtnfc usr/bin/tnfxtract ../lib/isaexec
SUNWtoo usr/bin/gcore ../../usr/lib/isaexec
SUNWtoo usr/bin/plimit ../../usr/lib/isaexec
SUNWtoo usr/bin/truss ../../usr/lib/isaexec
EOF
}

Quiet() {
	typeset -r cmd_n_args=$*
	 eval "$cmd_n_args" > /dev/null 2>&1
}

RestoreHardLinks() {

	_Finalize() {
		typeset -r cmd=$1
		shift 1
		typeset -r pkgs=$*

		typeset pkg=
		for pkg in $pkgs ; do
			Quiet $cmd -R $ROOTDIR -f $pkg
		done
	}

	typeset -r all_pkgs=$(GetBrokenLinks | awk '{print $1}' | sort -u)

	if [ "$ROOTDIR" = '/' ] ; then
		typeset -r RT_DIR=$ROOTDIR 
	else
		typeset -r RT_DIR="$ROOTDIR/"
	fi

	typeset pkg= target= src_path= targ_path=
	GetBrokenLinks | while read pkg target src_path ; do
		
		targ_path="${RT_DIR}${target}"
		Quiet removef -R $ROOTDIR $pkg $targ_path
		/usr/bin/rm -f $targ_path

		cd $(dirname $targ_path) 
		Quiet ln $src_path $(basename $targ_path)
	done

	_Finalize removef $all_pkgs

	typeset pkg= target= src_path= targ_path=
	GetBrokenLinks | while read pkg target src_path ; do
		targ_path=$ROOTDIR/$target
		Quiet installf -R $ROOTDIR $pkg $targ_path=$src_path
	done

	_Finalize installf $all_pkgs
}

# Process /usr/bin/cputrack
cd $ROOTDIR/usr/bin
[ ! -e "$ROOTDIR/usr/bin/cputrack" ] && ln ../lib/isaexec cputrack
Quiet installf -R $ROOTDIR SUNWcpcu $ROOTDIR/usr/bin/cputrack=../lib/isaexec
Quiet installf -R $ROOTDIR -f SUNWcpcu

#RestoreHardLinks

################################################################################

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=`showrev -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
}

if [ "$ROOTDIR" != "/" ]; then
    	# Handle diskless, alternate root case.
	restore_kbtrans
    	return
fi

if [ -x '/usr/bin/zonename' ] ; then
	if [ `/usr/bin/zonename` != "global" ] ; then
		# We are in a local zone
		# kbtrans is taken care of in global zone.
		return
	else
		# In a global zone
		restore_kbtrans
	fi
else
  	# we are in the global zone or on a system
   	# without zones support.
   	restore_kbtrans
fi

exit 0
