#!/bin/sh

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

# postpatch script to correct the corrupted pkgmap files

# Global declarations
CP=/usr/bin/cp
MV=/usr/bin/mv
LS=/usr/bin/ls
GETTEXT=/usr/bin/gettext
NAWK=/usr/bin/nawk
AWK=/usr/bin/awk
SED=/usr/bin/sed
CAT=/usr/bin/cat
CUT=/usr/bin/cut
GREP=/usr/bin/grep
EGREP=/usr/bin/egrep
RM=/usr/bin/rm
PKGPATH=${ROOTDIR}/var/sadm/pkg
SPOOLPATH=/save/pspool
pkg=SUNWpkgcmdsu
TMP=/tmp/pkgmap.tmp.$$
CheckZones()
{
        if [ -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
}


GlobalZones()
{
pkgmap=$PKGPATH/$pkg/$SPOOLPATH/$pkg/pkgmap
# bug 6328091, are we in a jumpstart with patchadd -C?
if [ ! -f $PKGPATH/$pkg/$SPOOLPATH/$pkg/pkgmap ]; then
     return 0
fi
$CP $pkgmap $PKGPATH/$pkg/$SPOOLPATH/$pkg/pkgmap.bak
EXPR=`egrep -e "^?+( l)" $pkgmap`
if [ "$EXPR" != "" ]; then
	EXPR=`egrep -e "^?+( l)" $pkgmap | $AWK -F= -e '{print $1}' | $AWK -e 'BEGIN { ORS="|" } {print $1" f "$3" "$4}'`
	egrep -v -e "$EXPR" $pkgmap >> $TMP
	$CP $TMP $pkgmap
	$RM $TMP
fi

}

NonGlobalZones()
{
# Any local zone stuff here,
# typically this fuction is not used
        return 0
}

# Main

CheckZones

if [ "${GLOBAL_ZONE}" = "true" ]; then
        GlobalZones
else
        NonGlobalZones
fi

exit 0

