#!/bin/ksh

PLATFORM=`/usr/bin/uname -m`

rem_dir() {

	dir=$1
	pkg=$2

	if [ -d $ROOTDIR/$dir ] ; then
                /usr/bin/rmdir $ROOTDIR/$dir
		if [ $? -eq 0 ] ; then
			removef -R $ROOTDIR $pkg $ROOTDIR/$dir
			removef -R $ROOTDIR -f $pkg
		fi
	fi

}

rem_dir_pkg() {

# remove the directory from the package, but don't try to actually remove it

	dir=$1
	pkg=$2

	if [ -d $ROOTDIR/$dir ] ; then
		removef -R $ROOTDIR $pkg $ROOTDIR/$dir
		removef -R $ROOTDIR -f $pkg
	fi

}

rem_link() {

# Remove symbolic links that were installed with this patch

	path=$1
	pkg=$2

	if [ -h $ROOTDIR/$path ] ; then
                /usr/bin/rm $ROOTDIR/$path
		if [ $? -eq 0 ] ; then
			removef -R $ROOTDIR $pkg $ROOTDIR/$path
			removef -R $ROOTDIR -f $pkg
		fi
	fi

}

restore_link() {

	link=$ROOTDIR/$1
	linkto=$2
	pkg=$3

	if [ ! -a $link ] ; then
		## if there is no link or directory, then restore orig link
		if [ -d $ROOTDIR/usr/platform/FJSV,GPUU ]; then
			installf -R $ROOTDIR $pkg $link=$linkto
			cd `dirname $link`
			ln -s $linkto
			installf -f -R $ROOTDIR $pkg
		fi
	fi
}

CheckZones()
{
	if [ "$ROOTDIR" = "/" -a -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
}


GlobalZones()
{

	if [ -z "${ROOTDIR}" ]; then
		echo "\n$0 Failed: ROOTDIR is not set.\n" >&2
		exit 1
	fi

    # Remove the symbolic links that were created by this patch

    rem_link usr/platform/FJSV,GPUU/lib/cfgadm FJSVvplu
    rem_link usr/platform/FJSV,GPUU/lib/fs FJSVvplu
    rem_link usr/platform/FJSV,GPUU/lib/libprtdiag_psr.so.1 FJSVvplu

    # If 119728-* was there, then keep the directory, since it was not
    # installed by this patch

    if /usr/sbin/patchadd -R $ROOTDIR -p | grep 119728 > /dev/null ; then
	   echo "Retaining platform lib directory..."
    else
	# remove directories that might have been left by 119728-01,
	# if it was added and then removed
	rem_dir usr/platform/FJSV,GPUU/lib/fm/fmd FJSVfmd
	rem_dir usr/platform/FJSV,GPUU/lib/fm FJSVfmd
	rem_dir_pkg usr/platform/FJSV,GPUU/lib FJSVfmd

	# restore the link for FJSVvplu
	rem_dir usr/platform/FJSV,GPUU/lib FJSVvplu
	restore_link usr/platform/FJSV,GPUU/lib ../sun4u/lib FJSVvplu
    fi
}


NonGlobalZones()
{
	return 0
}


# Main

CheckZones

if [ "${GLOBAL_ZONE}" = "true" ]; then
	GlobalZones
else
	NonGlobalZones
fi

exit 0
