#!/bin/ksh

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

patch_manifest_files() {

	global_zone=false

	## exit if no r.manifest files are installed
	cd  $ROOTDIR/var/sadm/pkg
	
	/bin/ls */save/pspool/*/install/r.manifest > /dev/null 2>&1 && global_zone=true

        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
		[ ! -d $ROOTDIR/var/sadm/pkg/$mfpkg ] && continue

		pspool=$ROOTDIR/var/sadm/pkg/$mfpkg/save/pspool
		dst_mf=$pspool/${mfpkg}/install/r.manifest
		sav_mf=${dst_mf}.${PatchNum}
		src_mf=$patchdir/$mf_file
		dst_pkg_mf=$ROOTDIR/var/sadm/pkg/$mfpkg/install/r.manifest
		sav_pkg_mf=$ROOTDIR/var/sadm/pkg/$mfpkg/install/r.manifest.${PatchNum}

		if [ "$global_zone" = "true" ] ; then 
			## only modify pspool dirs in global zones.
			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

			## pspool manifest 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
		if ! cmp -s $dst_pkg_mf $src_mf ; then
			cp -p $dst_pkg_mf $sav_pkg_mf
			cp -p $src_mf $dst_pkg_mf
		fi
	done
}

CheckZones()
{
        if [ "$ROOTDIR" = "/" -a -x /usr/bin/zonename ]; then
        ZONENAME=`/usr/bin/zonename`
                if [ ${ZONENAME} = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
        # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}

LocalZones () {
# commands specific to non-gloabl zones
return 0

}

ExecuteInProperEnvironment () {
   if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global whole root zone commands.
       return 0
   fi

   if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global zone commands. Should be no action here
       return 0
   fi

   if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
       # Execute commands applicable to patching the mini-root.
       return 0
   fi

   if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
       # Execute commands specific to the mini-root
       return 0
   fi

   if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
       # Execute commands specific to diskless client
       return 0
   fi

   if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
       # Execute commands specific to an alternate root
       return 0
   fi

   if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
       # In a global zone and system is mounted on /.
       patch_manifest_files
       return 0
   fi
   return 1
}

## Main

if [ -x "$PKGCOND" ] ; then
   ExecuteInProperEnvironment && exit 0 || exit 1
else
   CheckZones
   if [ "${GLOBAL_ZONE}" = "true" ]; then
	patch_manifest_files
   else
        LocalZones
   fi
fi

exit 0
