#!/bin/ksh

PLATFORM=$(/usr/bin/uname -m)
REV05=116561-05
REV09=116561-09

## Returns the number of patches installed at or above this rev.
LastPatch() {
	typeset -r this_patch=$1
	typeset -r root_dir=${ROOTDIR:-/}

	## Parse patch base & rev
	typeset -r patchbase=${this_patch%%-*}
	typeset -r patch_rev=${this_patch##*-}

	## Get all installed references to the installed patch base id
	typeset -r installed_patches=$(showrev -R $root_dir -p | \
					sed -n -e 's/Req.*//' \
					-e 's/[a-zA-Z]*://g'  \
					-e 's/,//g' -e "/$patchbase/p")

	integer patch_cnt=0

	typeset instbase= inst_rev= inst_patch=
	for inst_patch in $installed_patches ; do
		instbase=${inst_patch%%-*}
		inst_rev=${inst_patch##*-}

		if [ $patchbase -eq $instbase ] && \
				[ $inst_rev -ge $patch_rev ] ; then
			## count all installed patches including this patch
			patch_cnt=patch_cnt+1
		fi
	done

	return $patch_cnt
}

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

RemDir() {
	typeset -r dir=$1
	typeset -r pkg=$2

	[ -z "$dir" ] && return 1

	Quiet removef -R $ROOTDIR $pkg ${ROOTDIR}/$dir/*
	Quiet removef -R $ROOTDIR $pkg ${ROOTDIR}/$dir
	Quiet removef -R $ROOTDIR -f $pkg

	return 0
}

RestoreLink() {
	typeset -r link=${ROOTDIR}/$1
	typeset -r linkto=$2
	typeset -r pkg=$3

	[ -a $link ] && return 0

	## if there is no link or directory, then restore orig link
	if [ $PLATFORM = "sun4u" ]; then
		Quiet installf -R $ROOTDIR $pkg $link=$linkto s
		cd $(dirname $link)
		ln -s $linkto
		Quiet installf -f -R $ROOTDIR $pkg
	fi

	return 0
}

################################################################################
# If system has patches below rev 5, then remove db entries and clean it up.  
# Remove the directories and reinstall symlinks.
################################################################################

_PkgInstalled() {
	typeset -r pkg=$1
	Quiet pkginfo -R $ROOTDIR $pkg 2>/dev/null
	return $?
}

typeset -r NETRA_DIR=usr/platform/SUNW,Netra-CP2300
typeset -r sbin_dir=$NETRA_DIR/sbin
typeset -r incl_dir=$NETRA_DIR/include

typeset -r sbin_link="../sun4u/sbin"
typeset -r incl_link="../sun4u/include"
typeset -r fruadm_link="../$sbin_link/fruadm"

typeset -r kvm_pkg=SUNWkvm
typeset -r fru_pkg=SUNWfruip

if LastPatch $REV05 ; then
	# After backout, if the system has the same patchbase with revs < 05
	RemDir $incl_dir SUNWhea
	[ -d $ROOTDIR/$incl_dir ] && /usr/bin/rm -rf $ROOTDIR/$incl_dir

	RemDir $sbin_dir $kvm_pkg

	# Remove SUNWfruip only if it is installed on the target system
	_PkgInstalled $fru_pkg && RemDir $sbin_dir $fru_pkg

	[ -d $ROOTDIR/$sbin_dir ] && /usr/bin/rm -rf $ROOTDIR/$sbin_dir

	RestoreLink $incl_dir $incl_link SUNWhea
	RestoreLink $sbin_dir $sbin_link $kvm_pkg
elif ! LastPatch $REV09 ; then
	# After backout, if the system has the same patchbase with revs >= 09
	RestoreLink $sbin_dir/fruadm $fruadm_link $fru_pkg
else
	# After backout, if the system has the same patchbase b/w revs 05 and 08
	RestoreLink $sbin_dir/fruadm $fruadm_link $kvm_pkg

	# Remove SUNWfruip only if it is installed on the target system
	_PkgInstalled $fru_pkg && RemDir $sbin_dir $fru_pkg
fi

exit 0
