# 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

UNINSTALL_ORDER="SUNWupdatemgru SUNWupdatemgrr SUNWppro-plugin-sunos-base SUNWpprou SUNWppror SUNWccinv SUNWccfwctrl SUNWccccfg SUNWcctpx SUNWbreg SUNWdc SUNWccfw SUNWcsmauth SUNWccsign SUNWccccr SUNWccccrr "
ALWAYS_UNINSTALL=""
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

for pkg in $UNINSTALL_ORDER; do

    pkginfo -R ${ROOTDIR} $pkg > /dev/null 2>&1
    stat=$?
    if [ $stat -ne 0 ]; then
        continue
    fi

    echo $ALWAYS_UNINSTALL | grep $pkg > /dev/null 2>&1
    stat=$?
    if [ $stat -eq 0 ]; then
        pkgrm -R ${ROOTDIR} -n -a $ADMINFILE $pkg
        continue
    fi

    grep $pkg ${ROOTDIR}/var/sadm/patch/119107-03/pkgrm_list > /dev/null 2>&1
    stat=$?
    if [ $stat -eq 0 ]; then
        pkgrm -R ${ROOTDIR} -n -a $ADMINFILE $pkg
        continue
    fi
done
rm ${ROOTDIR}/var/sadm/patch/119107-03/pkgrm_list

#XXXSpecial_CommandsXXX#
#########################################################
# Restore the BASEDIR for SUNWbreg
#########################################################
pkginfo SUNWbreg > /dev/null 2>&1
if [ $? -eq 0 ]; then
    cp -p $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo.new
    sed 's/BASEDIR=.*/BASEDIR=\/opt/' $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo > $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo.new
    if [ $? -eq 0 ]; then
        mv $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo.new $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo
    else
        rm -f $ROOTDIR/var/sadm/pkg/SUNWbreg/pkginfo.new
    fi
fi


#########################################################
# Restore the BASEDIR for SUNWdc
#########################################################
pkginfo SUNWdc > /dev/null 2>&1
if [ $? -eq 0 ]; then
    cp -p $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo.new
    sed 's/BASEDIR=.*/BASEDIR=\/opt/' $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo > $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo.new
    if [ $? -eq 0 ]; then
        mv $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo.new $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo
    else
        rm -f $ROOTDIR/var/sadm/pkg/SUNWdc/pkginfo.new
    fi
fi

#########################################################
# Restore the files that were removed from SUNWswmt at patchadd
#########################################################
if [ -f $ROOTDIR/var/sadm/patch/119107-03/smpatch.SUNWswmt.undo ]; then
    mv $ROOTDIR/var/sadm/patch/119107-03/smpatch.SUNWswmt.undo $ROOTDIR/usr/sbin/smpatch
    ln -s  ../../sbin/smpatch $ROOTDIR/usr/sadm/bin/smpatch

    # Inform the system that we're updating the database
    do_install=1
    installf -R $ROOTDIR SUNWswmt /usr/sbin/smpatch f || do_install=0
    installf -R $ROOTDIR SUNWswmt /usr/sadm/bin/smpatch=../../sbin/smpatch s || do_install=0

    # Update the database
    if [ $do_install ]; then
        installf -R $ROOTDIR -f SUNWswmt 
    fi
fi

#########################################################
# Restore the ppro* packages to the ones installed by 119107-01
#########################################################
pkgadd_file=$ROOTDIR/var/sadm/patch/119107-03/pkgadd_list
if [ ! -f $pkgadd_file ]; then
    exit 0
fi

for pkg in `cat $pkgadd_file`; do
    # smpatch was added to SUNWpprou in GA. If we pkgadd without pkgrm 
    # the package database will still show it as being owned by SUNWpprou.
    # This will clean up the /var/sadm/install/contents
    pkgrm -n -a $ADMINFILE -R $ROOTDIR $pkg
    pkgadd -n -a $ADMINFILE -R $ROOTDIR -d $ROOTDIR/var/sadm/patch/119107-03/postbackout_undo $pkg
    rm -rf $ROOTDIR/var/sadm/patch/119107-03/postbackout_undo/$pkg
done

rm $ADMINFILE



