#!/bin/sh
# This script deletes the used backout data for a patch package
# and removes the deletes file entries.
#
# directory format options.
#
#       @(#)patch_postinstall 1.3 98/07/29 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

#
#JS we need this for protecting postinstall from library mount/umount 
#JS more changes done to this script, search for #JS
#
##
## variables which are set by checkinstall for us
##
## SP_ACTION
## LIBS_TO_OVERLAY_SYMMETRIC
## LIBS_TO_OVERLAY_LATEST
## PATCH_LIBS_STORE
## SP_LD_LIBRARY_PATH
##

NAWK="/bin/nawk"
FIND="/bin/find"
SORT="/bin/sort"
DIRNAME="/bin/dirname"

GetLibStroreLibPath()	# $libstore
{
	if [ -n "$1" -a -d "$1" ]; then
		echo `$FIND "$1" -type f -exec $DIRNAME {} \;|$SORT -u`|$NAWK '{ gsub(/ /,":"); print }'
	fi
}

# lets set LD_LIBRARY_PATH the way how checkinstall found it safe
LD_LIBRARY_PATH="${SP_LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

# now we can add libs which has been saved by preinstall
LD_LIBRARY_PATH="`GetLibStroreLibPath $PATCH_LIBS_STORE`"
export LD_LIBRARY_PATH

#JS end

PATH=/usr/sadm/bin:$PATH
THIS_DIR=`dirname $0`

Our_Deletes=$THIS_DIR/deletes

#
# Delete the used backout data
#
if [ -f $Our_Deletes ]; then
	cat $Our_Deletes | while read path; do
		if valpath -l $path; then
			Client_Path=`echo "$CLIENT_BASEDIR/$path" | sed "s|//|/|"`
		else	# It's an absolute path
			Client_Path=$path
		fi
		rm `removef $PKGINST $Client_Path`
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Remove the deletes file, checkinstall and the postinstall
#
rm -r $PKGSAV/$ACTIVE_PATCH
rm -f $THIS_DIR/checkinstall $THIS_DIR/postinstall


#XXXSpecial_CommandsXXX#

##
## variables which are set by checkinstall for us
##
## SP_ACTION
## LIBS_TO_OVERLAY_SYMMETRIC
## LIBS_TO_OVERLAY_LATEST
## PATCH_LIBS_STORE
## SP_LD_LIBRARY_PATH
##

# If not in alterante root overlay certain libs with previously stored ones.

LAST="/bin/last"
NAWK="/bin/nawk"
MKDIR="/bin/mkdir"
DIRNAME="/bin/dirname"
CP="/bin/cp"
MOUNT="/sbin/mount"
UMOUNT="/sbin/umount"
FIND="/bin/find"
SORT="/bin/sort"

RebootID()
# because I do not know how to get on solaris kernel incarnation number (if there is one)
# I am using last system reboot time
#
# prints last reboot time in form Thu_Aug_3_09_47
{
	TZ=C $LAST 'system boot'|$NAWK 'NR==1 && $7 !~ /^$/ { gsub(/[.:-]/,"_",$7); print $4"_"$5"_"$6"_"$7; }'
}

CP_OBJ()		# $object $pathto
# copy $object into $pathto preserving $object pathname
# and creating all needed subdirectories if needed
# CP_OBJ /lib/libc.so.1 /tmp/.aa will copy /lib/libc.so.1 into /tmp/.aa/lib/libc.so.1
# Do not do anything if target already exists.
{
	if [ -n "$2" -a ! -f "$2/$1" ]; then
		$MKDIR -p "`$DIRNAME \"$2/$1\"`" \
		&& $CP -rp "$1" "$2/$1"
	fi
}

GetMount()	# $mountpoint
# prints how mount command should look like to get it mounted back
# if there is more overlay mounts it prints last one
{
	[ -n "$1" ] \
	&& $MOUNT -p \
	   | $NAWK '{ gsub(/\/+/,"/",XM);}; $3 ~ XM {X="-O -F "$4" -o "$7" "$1" "$3; }; END{ print X; }' XM="^$1\$"
}

GetDeviceMount()	# $device
# prints how mount should look like to get it mounted back
# if there is more overlay mounts it prints last one
{
	[ -n "$1" ] \
	&& $MOUNT -p \
	   | $NAWK '{ gsub(/\/+/,"/",XM);}; $1 ~ XM {X="-O -F "$4" -o "$7" "$1" "$3; }; END{ print X; }' XM="^$1\$"
}

AlternateRoot()
# returns 0 if $PKG_INSTALL_ROOT -
{
	if [ -n "$PKG_INSTALL_ROOT" ]; then
		if  [ "/" = "`echo $PKG_INSTALL_ROOT|$NAWK '{gsub(/\/+/,"/"); print}'`" ]; then
			return 1
		else
			return 0
		fi
	else
		return 1
	fi
}

PATCHADD_LIBS="$PATCH_LIBS_STORE/patchadd"
PATCHRM_LIBS="$PATCH_LIBS_STORE/patchrm"

if [ "$SP_ACTION" = "patchadd" ]; then
	LIB_STORE_1="$PATCHRM_LIBS"
	LIB_STORE_2="$PATCHADD_LIBS"
	LIB_PATCHID="$SUNW_PATCHID"
else
	LIB_STORE_2="$PATCHRM_LIBS"
	LIB_STORE_1="$PATCHADD_LIBS"
	LIB_PATCHID="$ACTIVE_PATCH"
fi

if AlternateRoot; then
	:
else
	for LIB_NAME in $LIBS_TO_OVERLAY_SYMMETRIC $LIBS_TO_OVERLAY_LATEST ; do
		if [ ! -f "$LIB_STORE_1/$LIB_PATCHID/$LIB_NAME" ]; then
			# todo: sanity check when $PATCH_LIBS/*/*/$LIB_NAME reolves into more then single entry
			if [ -f $PATCH_LIBS_STORE/*/*/$LIB_NAME ]; then
				$MOUNT -F lofs -O $PATCH_LIBS_STORE/*/*/$LIB_NAME "${PKG_INSTALL_ROOT}/${LIB_NAME}"
			fi
		fi
	done
fi

#!!
#JS modification done in this i.none script, please search for #JS comments
#

exit 0

