#!/bin/sh

PATH=/usr/sadm/bin:$PATH

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

#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 we are not in alternate root, we want to backup certain libs for overlay
# mount later in the process and if they have been already overlayed, unmount
# or fail.

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\$"
}

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

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
}

Umount()	# $mount1 [ $mount2 ... ]
# Umount will umount all objects from its parameters
# it will exit 1 if an object can not be unmounted
{
	for LIB_NAME in "$@"; do
		if [ -n "`GetMount $PKG_INSTALL_ROOT/$LIB_NAME`" ]; then
			$UMOUNT "$PKG_INSTALL_ROOT/$LIB_NAME" \
			|| {
				echo "ERROR: can not umount $PKG_INSTALL_ROOT/$LIB_NAME, please reboot and try $SP_ACTION again."
				exit 1
			}
		fi
	done
}

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
	LD_LIBRARY_PATH="`GetLibStroreLibPath $PATCH_LIBS_STORE`"
	export LD_LIBRARY_PATH
	Umount $LIBS_TO_OVERLAY_SYMMETRIC $LIBS_TO_OVERLAY_LATEST
	# Store only all libraries in patchrm
	for LIB_NAME in $LIBS_TO_OVERLAY_SYMMETRIC $LIBS_TO_OVERLAY_LATEST; do
		if [ ! -f $PATCH_LIBS_STORE/*/*/$LIB_NAME ]; then
			CP_OBJ "$PKG_INSTALL_ROOT/$LIB_NAME" "$LIB_STORE_2/$LIB_PATCHID"
		fi
	done
fi

exit 0
