#!/bin/sh

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

# prepatch script to correct the corrupted pkgmap files
# in Architecture specific packages

# Global declarations
CP=/usr/bin/cp
MV=/usr/bin/mv
LS=/usr/bin/ls
GETTEXT=/usr/bin/gettext
NAWK=/usr/bin/nawk
SED=/usr/bin/sed
CAT=/usr/bin/cat
CUT=/usr/bin/cut
GREP=/usr/bin/grep
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()
{
tmpfile=/tmp/tmpfile.$$
pkgmap=$PKGPATH/$pkg/$SPOOLPATH/$pkg/pkgmap
#incase the script corrupts pkgmap
$CP $pkgmap $PKGPATH/$pkg/$SPOOLPATH/$pkg/pkgmap.bak
if [ -f $TMP ]; then
	$RM $TMP
fi
while read line; do

        mode=`echo $line |$NAWK '{print $2}'`
        if [ "$mode" = l ]; then
                echo $line >> $tmpfile
        fi
done < $pkgmap
while read line; do
        mode=`echo $line |$NAWK '{print $2}'`
        if [ "$mode" = f ]; then
                flag=0
                while read line1; do
                        name1=`echo $line1 | $NAWK '{print $4}' | $CUT -f 1 -d=`
                        name2=`echo $line | $NAWK '{print $4}'`
                        if [ "$name1" = "$name2" ]; then
                                flag=1
                        fi
                done < $tmpfile
                if [ $flag -ne 1 ]; then
                        echo $line >> $TMP
                fi

        else
                echo $line >> $TMP
        fi
done < $pkgmap
$CP $TMP $pkgmap
$RM $tmpfile

}

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

