# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
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
}

CheckZones
if [ "${GLOBAL_ZONE}" != "true" ]; then
    exit 0
fi

mypatchid=120335-02

INSTALL_ORDER=" SUNWcbreg SUNWdbreg SUNWebreg SUNWfbreg SUNWhbreg SUNWibreg SUNWjbreg SUNWkbreg SUNWsbreg SUNWcupdatemgru SUNWdupdatemgru SUNWeupdatemgru SUNWfupdatemgru SUNWhupdatemgru SUNWiupdatemgru SUNWjupdatemgru SUNWkupdatemgru SUNWsupdatemgru"                  
ALWAYS_ADDED=""
ADD_IF_NOT_INSTALLED=" SUNWcbreg SUNWdbreg SUNWebreg SUNWfbreg SUNWhbreg SUNWibreg SUNWjbreg SUNWkbreg SUNWsbreg SUNWcupdatemgru SUNWdupdatemgru SUNWeupdatemgru SUNWfupdatemgru SUNWhupdatemgru SUNWiupdatemgru SUNWjupdatemgru SUNWkupdatemgru SUNWsupdatemgru"                  
ADMINFILE=/var/run/admin.quiet.$$

cat << EOF > $ADMINFILE
mail=
instance=overwrite
partial=ask
runlevel=ask
idepend=nocheck
rdepend=nocheck
space=ask
setuid=nocheck
conflict=nocheck
action=nocheck
networktimeout=60
networkretries=3
authentication=quit
keystore=/var/sadm/security
proxy=
EOF

# install package if not already installed
rm -f ${ROOTDIR}/var/run/pkgrm_list_${mypatchid}; touch ${ROOTDIR}/var/run/pkgrm_list_${mypatchid}

usr_install=0
root_install=0
pkginfo -R ${ROOTDIR} SUNWcsu > /dev/null 2>&1
if [ $? -eq 0 ]; then
    usr_install=1
fi

pkginfo -R ${ROOTDIR} SUNWcsr > /dev/null 2>&1
if [ $? -eq 0 ]; then
    root_install=1
fi

for pkg in $INSTALL_ORDER; do    
    pkgtype=`grep $pkg: pkgtypes | awk -F: '{print $2}'`
    if [ "$pkgtype" = "root" ]; then
        if [ $root_install -ne 1 ]; then
            continue;
        fi
    elif [ "$pkgtype" = "usr" ]; then
        if [ $usr_install -ne 1 ]; then
            continue;
        fi
#    elif [ "$pkgtype" != "root,usr" ]; then
#        continue
    else
        continue
    fi
    
    # Is this package always added.
    echo $ALWAYS_ADDED | grep $pkg > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        #pkgadd -n -R ${ROOTDIR} -a $ADMINFILE -d $pkg.pkg all
        pkgadd -n -R ${ROOTDIR} -a $ADMINFILE -d payload.pkg $pkg
        continue
    fi

    pkginfo -R ${ROOTDIR} $pkg > /dev/null 2>&1
    stat=$?
    if [ $stat -ne 0 ]; then
        #pkgadd -n -R ${ROOTDIR} -a $ADMINFILE -d $pkg.pkg all
        pkgadd -n -R ${ROOTDIR} -a $ADMINFILE -d payload.pkg $pkg
        
        grep ALWAYS_UNINSTALL postbackout | grep $pkg > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            # We added the package and it's not in our backout
            # script. This means we should only uninstall it
            # if we added it. Add it to the list
            echo $pkg >> ${ROOTDIR}/var/run/pkgrm_list_${mypatchid}
        fi
        continue
    fi
done
mv ${ROOTDIR}/var/run/pkgrm_list_${mypatchid} ${ROOTDIR}/var/sadm/patch/${mypatchid}/pkgrm_list

#XXXSpecial_CommandsXXX#


