# checkinstall script to validate backing out a patch.
# directory format option.
#
#       @(#)patch_checkinstall 1.2.2 96/05/08 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

# Create new DRVUPDATELIST; sed does not work with strings separated by spaces
# Create new TOPDRVLIST
if [ "$OLDDRVLIST" != "" ]; then
	OLDDRVLIST2=`echo $OLDDRVLIST | sed 's/ /?/g'`
	OLDDRV2=`echo $OLDDRV | sed 's/ /?/g'`
	NEW_DRV_LIST=`echo $OLDDRVLIST2 | sed 's/'$OLDDRV2'//' | sed 's/??/?/' | sed 's/?$//'| sed 's/?/ /g'`
	echo "DRVUPDATELIST=${NEW_DRV_LIST}" >> $1
	echo "SUNW_DRV=" >> $1
	rm -rf /tmp/drvlist?.$$
	for i in ${NEW_DRV_LIST}
	do
	        echo $i >> /tmp/drvlist1.$$
	done
	sort /tmp/drvlist1.$$ | uniq | while read current
	do
	        echo $current > /tmp/drvlist2.$$
	        if [ x$previous != "x" ]
	        then
	                root1=`expr $previous : "\(.*\)\-.*"`
	                root2=`expr $current : "\(.*\)\-.*"`
	                if [ "$root1" != "$root2" ]
	                then
	                        echo $previous >> /tmp/drvlist3.$$
	                fi
	        fi
	        previous=$current
	done
	cat /tmp/drvlist2.$$ >> /tmp/drvlist3.$$
	for i in `cat /tmp/drvlist3.$$`
	do
		SORTED="$SORTED $i"
	done
	echo "TOPDRVLIST=${SORTED}" >> $1
	rm -rf /tmp/drvlist?.$$
fi

echo "SUNW_PATCHID=" >> $1
echo "PATCH_INFO_$ACTIVE_PATCH=backed out" >> $1

# Get the current PATCH_OBSOLETES and condition it
Old_Obsoletes=$PATCH_OBSOLETES

echo $ACTIVE_OBSOLETES | sed 'y/\ /\n/' | \
nawk -v PatchObsList="$Old_Obsoletes" '
	BEGIN {
		printf("PATCH_OBSOLETES=");
		PatchCount=split(PatchObsList, PatchObsComp, " ");

		for(PatchIndex in PatchObsComp) {
			Atisat=match(PatchObsComp[PatchIndex], "@");
			PatchObs[PatchIndex]=substr(PatchObsComp[PatchIndex], 0, Atisat-1);
			PatchObsCnt[PatchIndex]=substr(PatchObsComp[PatchIndex], Atisat+1);
		}
	}
	{
		for(PatchIndex in PatchObs) {
			if (PatchObs[PatchIndex] == $0) {
				PatchObsCnt[PatchIndex]=PatchObsCnt[PatchIndex]-1;
			}
		}
		next;
	}
	END {
		for(PatchIndex in PatchObs) {
			if ( PatchObsCnt[PatchIndex] > 0 ) {
				printf("%s@%d ", PatchObs[PatchIndex], PatchObsCnt[PatchIndex]);
			}
		}
		printf("\n");
	} ' >> $1

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

exit 0

