# 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

INSTALL_ORDER=" SUNWccccrr SUNWccccr SUNWccsign SUNWcsmauth SUNWccfw SUNWdc SUNWbreg SUNWcctpx SUNWccccfg SUNWccfwctrl SUNWccinv SUNWppror SUNWpprou SUNWppro-plugin-sunos-base SUNWupdatemgrr SUNWupdatemgru"                
ALWAYS_ADDED=""
ADD_IF_NOT_INSTALLED=" SUNWccccrr SUNWccccr SUNWccsign SUNWcsmauth SUNWccfw SUNWdc SUNWbreg SUNWcctpx SUNWccccfg SUNWccfwctrl SUNWccinv SUNWppror SUNWpprou SUNWppro-plugin-sunos-base SUNWupdatemgrr SUNWupdatemgru"                
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_119107-03; touch ${ROOTDIR}/var/run/pkgrm_list_119107-03

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_119107-03
        fi
        continue
    fi
done
mv ${ROOTDIR}/var/run/pkgrm_list_119107-03 ${ROOTDIR}/var/sadm/patch/119107-03/pkgrm_list

#XXXSpecial_CommandsXXX#
Jan_version_list="SUNWppror SUNWpprou SUNWppro-plugin-sunos-base"
May_version_list="SUNWcsmauth SUNWswmt SUNWupdatemgrr SUNWupdatemgru"

# 
SUNWppror_VERSION="5.0,REV=2005.01.09.23.05"
SUNWpprou_VERSION="5.0,REV=2005.01.09.23.05"
SUNWpproXXXpluginXXXsunosXXXbase_VERSION="5.0,REV=2005.01.09.23.05"

# These values didn't change between PR and GA; no need to modify
SUNWbreg_VERSION="1.0"
SUNWccccfg_VERSION="1.0.0"
SUNWccccr_VERSION="001.000.000"
SUNWccccrr_VERSION="001.000.000"
SUNWccfw_VERSION="001.000.000"
SUNWccfwctrl_VERSION="1.0.0"
SUNWccinv_VERSION="1.0.0"
SUNWccsign_VERSION="001.000.000"
SUNWcctpx_VERSION="001.000.000"
SUNWdc_VERSION="0.1"

#doesn't change since we only patch it
SUNWswupcl_VERSION="1.0.2,REV=2005.04.08.08.22"
SUNWswmt_VERSION="11.10,REV=2005.01.10.17.19"

SUNWcsmauth_VERSION="0.1,REV=2005.05.12.10.35"
SUNWupdatemgrr_VERSION="0.1,REV=2005.05.20.10.51"
SUNWupdatemgru_VERSION="0.1,REV=2005.05.20.10.51"

modify_version()
{
    pkgname=$1
    var_prefix=`echo $1 | sed 's/-/XXX/g'`
    eval version_var=${var_prefix}_VERSION
    eval version=\${$version_var}
    sed "s/VERSION=.*/VERSION=$version/" $ROOTDIR/var/sadm/pkg/$pkgname/pkginfo > $ROOTDIR/var/sadm/pkg/$pkgname/pkginfo.tmp
    if [ $? -eq 0 ]; then
        echo MODIFYING VERSION FOR $pkgname
        mv $ROOTDIR/var/sadm/pkg/$pkgname/pkginfo.tmp $ROOTDIR/var/sadm/pkg/$pkgname/pkginfo
    fi
}


# The SUNWppro* packages with VERSION string dated May 2005
# were pkgadded by the PR patch (119107-01); this is done
# on an end user cluster. In this case we overlay them
# and restore them upon patch backout

pkgadd_file=$ROOTDIR/var/sadm/patch/119107-03/pkgadd_list
for pkg in $Jan_version_list ; do
    ver=`pkgparam -R $ROOTDIR $pkg VERSION`
    if [ "$ver" == "5.0,REV=2005.05.13.16.32" ]; then
        if [ ! -f $pkgadd_file ]; then
            touch $pkgadd_file
            mkdir $ROOTDIR/var/sadm/patch/119107-03/postbackout_undo
        fi
        pkgadd -a $ADMINFILE -R $ROOTDIR -d payload.pkg $pkg

        pkgtrans undo_pkgs.pkg $ROOTDIR/var/sadm/patch/119107-03/postbackout_undo $pkg

        echo $pkg >> $pkgadd_file
    fi
done
rm $ADMINFILE

if [ -f $ROOTDIR/var/run/119107-03.smpatch.SUNWswmt.undo ]; then 
    mv  $ROOTDIR/var/run/119107-03.smpatch.SUNWswmt.undo $ROOTDIR/var/sadm/patch/119107-03/smpatch.SUNWswmt.undo
fi

exit 0



