#!/bin/sh
# checkinstall script to validate backing out a patch.
# directory format option.
#
#       @(#)patch_checkinstall 1.3 01/01/11 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

PATH=/usr/sadm/bin:$PATH

LATER_MSG="PaTcH_MsG 6 ERROR: A later version of this patch is applied."
NOPATCH_MSG="PaTcH_MsG 2 ERROR: Patch number $ACTIVE_PATCH is not installed"
NEW_LIST=""

# Get OLDLIST
. $1

#
# Confirm that the patch that got us here is the latest one installed on
# the system and remove it from PATCHLIST.
#
Is_Inst=0
Skip=0
active_base=`echo $ACTIVE_PATCH | nawk '
	{ print substr($0, 1, match($0, "-")-1) } '`
active_inst=`echo $ACTIVE_PATCH | nawk '
	{ print substr($0, match($0, "-")+1) } '`
for patchappl in ${OLDLIST}; do
	appl_base=`echo $patchappl | nawk '
		{ print substr($0, 1, match($0, "-")-1) } '`
	if [ $appl_base = $active_base ]; then
		appl_inst=`echo $patchappl | nawk '
			{ print substr($0, match($0, "-")+1) } '`
		result=`expr $appl_inst \> $active_inst`
		if [ $result -eq 1 ]; then
			puttext "$LATER_MSG"
			exit 3
		elif [ $appl_inst = $active_inst ]; then
			Is_Inst=1
			Skip=1
		fi
	fi
	if [ $Skip = 1 ]; then
		Skip=0
	else
		NEW_LIST="${NEW_LIST} $patchappl"
	fi
done

if [ $Is_Inst = 0 ]; then
	puttext "$NOPATCH_MSG"
	exit 3
fi

#
# OK, all's well. Now condition the key variables.
#
echo "PATCHLIST=${NEW_LIST}" >> $1
echo "SUNW_PATCHID=" >> $1
echo "PATCH_INFO_$ACTIVE_PATCH=backed out" >> $1

# remove the used parameters
echo "ACTIVE_OBSOLETES=" >> $1
echo "SUNW_OBSOLETES=" >> $1

#XXXSpecial_CommandsXXX

SP_ACTION="patchrm"
##
## we just take whatever we have left from patchadd in pkginfo
##
##LIBS_TO_OVERLAY_SYMMETRIC="lib/libdevinfo.so.1 lib/sparcv9/libdevinfo.so.1"
##LIBS_TO_OVERLAY_LATEST="lib/libsec.so.1 lib/sparcv9/libsec.so.1"

# 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"
CAT="/bin/cat"
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; }'
}

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

TMPMOUNTDIR="`RebootID`"
PATCH_LIBS_STORE="/tmp/.libstore/${TMPMOUNTDIR:-TeMpObJeCtLoCaTiOn}"
SP_LD_LIBRARY_PATH="`GetLibStroreLibPath $PATCH_LIBS_STORE`"

# pass common variables into rest of scripts

echo "SP_ACTION=$SP_ACTION" >>$1
echo "LIBS_TO_OVERLAY_SYMMETRIC=$LIBS_TO_OVERLAY_SYMMETRIC" >>$1
echo "LIBS_TO_OVERLAY_LATEST=$LIBS_TO_OVERLAY_LATEST" >>$1
echo "PATCH_LIBS_STORE=$PATCH_LIBS_STORE" >>$1
echo "SP_LD_LIBRARY_PATH=$SP_LD_LIBRARY_PATH" >>$1

exit 0

