#!/bin/ksh

# Copyright (c) 2005 by Sun Microsystems, Inc.
# All rights reserved


patch_manifest_files() {
	## exit if no r.manifest files are installed
	cd  $ROOTDIR/var/sadm/pkg
	##  we exit if pspool doe not exit, ie in a localzone
	/bin/ls */save/pspool/*/install/r.manifest > /dev/null 2>&1 || return 0

        cd $patchdir || exit 1
	
        ## Update all r.manifest files in the actual patch
	for mf_file in `/bin/ls */install/r.manifest 2> /dev/null` ; do
		## pkg name
		mfpkg=`echo $mf_file | awk -F/ '{print $1}'`
                ## get rin of .us or .u etc
		mfpkg=${mfpkg%.*}

		[ -z $mfpkg ] && exit 1
		pspool=$ROOTDIR/var/sadm/pkg/$mfpkg/save/pspool
		[ ! -d $ROOTDIR/var/sadm/pkg/$mfpkg ] && continue
 
		typeset  manifest_sum=$(/usr/bin/sum $mf_file | awk '{print $1}')
		typeset  manifest_sz=$(/usr/bin/wc -c $mf_file | awk '{print $1}')
 		## now update the installed pkgmap with correct size and sum	
		/usr/bin/nawk -v pi="$manifest_sz" -v pm="$manifest_sum" '
			$3 ~ /r\.manifest/ {
				printf("%s %s %s %s %s %s\n", $1, $2, $3, pi, pm, $6)
			}
			$3 !~ /r\.manifest/ {
				print
			}' $pspool/$mfpkg/pkgmap > $pspool/$mfpkg/pkgmap.$$ || exit 1
		/usr/bin/mv -f $pspool/$mfpkg/pkgmap.$$ $pspool/$mfpkg/pkgmap     || exit 1
		dst_mf=$pspool/${mfpkg}/install/r.manifest
		sav_mf=${dst_mf}.${PatchNum}
		src_mf=$patchdir/$mf_file

		if [ -f $dst_mf ] ; then
			## file exists
			if ! cmp -s $dst_mf $src_mf ; then
				## It is the old version. Update it.
				cp -p $dst_mf $sav_mf
				cp -p $src_mf $dst_mf
			fi
		fi
	done
}

## Main

patch_manifest_files

exit 0
