#!/bin/ksh 
#
#  This script verifies that the machine OS level
#  and version of Instant Image match this version
#  of the patch.

# Declare the required version of Solaris for this patch
PATCH_OS="Solaris_7-899"
PATCH_OS_DESC=", which is Solaris 7 with kernel patch 106541-06 or higher applied."

# Find the version of Solaris on this machine
case "X$(uname -r)" in
	X5.6) 
	   OS=Solaris_2.6 
	   OS_DESC="." ;;
	X5.7)
	   VER_OS=`uname -v | cut -b16-72`
	   if [ -z "${VER_OS}" ];then
			OS=Solaris_7
			OS_DESC=" FCS \n(Solaris 7 where kernel is either unpatched or patched with 106541-05 or lower)."
	   else
		case ${VER_OS} in
		   01|02|03|04|05) 
				    OS=Solaris_7 
				    OS_DESC=" FCS \n(Solaris 7 where kernel is either unpatched or patched with 106541-05 or lower)."
				    ;;
		   *) 
		    	OS=Solaris_7-899 
		    	OS_DESC=", which is Solaris 7 with kernel patch 106541-06 or higher applied." ;;
		esac
	   fi
	;;

	X5.8) 
	   OS=Solaris_8 
	   OS_DEC="." ;;
	*) 
	   OS=`uname -r` 
	   OS_DESC="." ;;
esac

# If $OS does not equal $PATCH_OS, echo message and exit 
if [[ $OS != $PATCH_OS ]]
then
	echo "ERROR"
	echo "This patch is intended for: ${PATCH_OS}${PATCH_OS_DESC}"
	echo "This machine is running: ${OS}${OS_DESC}"
	exit 1
fi

#
# script to modify pkginfo files of patch packages to ensure 
# patchadd will succeed

#
# make list of packages to be installed by patch
#

pkg=
for i in */pkginfo X
do
        if [ "$i" = "X" ]
        then
                break
        fi

        pkg=`expr $i : '\(.*\)/pkginfo'`
        pkglist="$pkglist $pkg"
done


#
# for each package in list
#

for j in $pkglist
do
	
	#
	# Get the package abbreviation, architecture, version
	# and target filesystem.
	#

	Pkginst=$(basename $j)
	Pkgabbrev=$(pkgparam -f $j/pkginfo PKG)
	Pkgarch=$(pkgparam -f $j/pkginfo ARCH)
	Pkgpatchver=$(pkgparam -f $j/pkginfo DS_ID_STRINGS)
	old_version=$(pkgparam -f $j/pkginfo VERSION)

	#
	# find package in /var/sadm/pkg
	#

	inst_dir=/var/sadm/pkg

	#
	# if package is not found in /var/sadm/pkg
	#

	if [ ! -d $inst_dir/$Pkgabbrev ] && [ ! -d $inst_dir/$Pkgabbrev.* ]
	then

		# if package is not found do nothing...this package is not 
		# installed on the user's machine...patchadd will fail for 
		# us because it should.  Continue for loop.

		continue

	fi # end if package is not found in /var/sadm/pkg


	#
	# At this point, there's a package of that name installed.
	# Check to see if the VERSION string of the installed package
	# matches one of the DS_ID_STRINGS shipped with the patch.
	#

	#
	# for string in DS_ID_STRINGS (from pkginfo of patch package)
	#

	for ds_pkg in $Pkgpatchver
	do

		#
		# look for match in installed package via pkginfo
		#

		pkginst=$(pkginfo -a $Pkgarch -v $ds_pkg $Pkgabbrev.\* 2>/dev/null | /usr/bin/nawk ' { print $2 } ')

		#
		# if match is found
		#

		if [[ -n $pkginst ]]
		then

			#
			# move pkginfo file to pkginfo.sed
			# and pkgmap file to pkgmap.nwk
			#

			mv $j/pkginfo $j/pkginfo.sed
			mv $j/pkgmap $j/pkgmap.nwk

			#
			# sed VERSION string in pkginfo.sed file in 
			# patch package with matching DS_ID_STRING
			# and output to pkginfo
			#

			sed "s/^VERSION=.*/VERSION=$ds_pkg/" \
				$j/pkginfo.sed > $j/pkginfo

			#
			# find new sum and size of pkginfo
			# update entry in pkgmap with new values using nawk
			#

			Isum=`/usr/bin/sum $j/pkginfo | \
				/usr/bin/nawk '{ print $1 }'`
			Isize=`/usr/bin/wc -c $j/pkginfo | \
				/usr/bin/nawk '{ print $1 }'`


  			/usr/bin/nawk -v sum=$Isum -v size=$Isize '
       				/i pkginfo/ {
            				printf("%d %s %s %d %d %d\n", \
                   				$1, $2, $3, size, sum, $6)
            				next
       				}
       				{ print }
       				' $j/pkgmap.nwk > $j/pkgmap

			#
			# remove pkginfo.sed and pkgmap.nwk temp files
			#

			rm $j/pkginfo.sed
			rm $j/pkgmap.nwk
			

			#
			# break from for loop
			#

			break
			
		# else if match is not found

				# do nothing...the version of the package
				# installed on this machine is NOT one of
				# the 2.0 versions we are looking for...
				# patchadd will fail for us because it should
			
		fi # end if match is found

	done # end for string in DS_ID_STRINGS loop


done # end for each package in list loop
