# 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=119108-06

UNINSTALL_ORDER="SUNWupdatemgrr SUNWupdatemgru 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/${mypatchid}/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/${mypatchid}/pkgrm_list

#XXXSpecial_CommandsXXX#
basedir_list_file=$ROOTDIR/var/sadm/patch/$mypatchid/basedir_list
version_list_file=$ROOTDIR/var/sadm/patch/$mypatchid/version_list

#########################################################
# Restore the BASEDIR for SUNWbreg and SUNWdc
#########################################################
if [ -f $basedir_list_file ]; then
    for pkg in `cat $basedir_list_file`; do
        pkginfo -R $ROOTDIR $pkg > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            continue
        fi

        pkginfo_filename=$ROOTDIR/var/sadm/pkg/$pkg/pkginfo
        cp -p $pkginfo_filename ${pkginfo_filename}.new
        sed 's/BASEDIR=.*/BASEDIR=\/opt/' $pkginfo_filename > ${pkginfo_filename}.new
        if [ $? -eq 0 ]; then
            mv ${pkginfo_filename}.new $pkginfo_filename
        else
            rm -f ${pkginfo_filename}.new
        fi
    done
    rm $basedir_list_file
fi


#########################################################
# Restore the VERSION for SUNWdc
#########################################################
if [ -f $version_list_file ]; then
    for pkg in `cat $version_list_file`; do
        pkginfo -R $ROOTDIR $pkg > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            continue
        fi

        pkginfo_filename=$ROOTDIR/var/sadm/pkg/$pkg/pkginfo
        cp -p $pkginfo_filename ${pkginfo_filename}.new
        sed 's/VERSION=.*/VERSION=0.1/' $pkginfo_filename > ${pkginfo_filename}.new
        if [ $? -eq 0 ]; then
            mv ${pkginfo_filename}.new $pkginfo_filename
        else
            rm -f ${pkginfo_filename}.new
        fi
    done
    rm $version_list_file
fi


#########################################################
# Restore the files that were removed from SUNWswmt at patchadd
#########################################################
if [ -f $ROOTDIR/var/sadm/patch/$mypatchid/smpatch.SUNWswmt.undo ]; then
    mv $ROOTDIR/var/sadm/patch/$mypatchid/smpatch.SUNWswmt.undo $ROOTDIR/usr/sbin/smpatch
    rm -f $ROOTDIR/usr/sadm/bin/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

# If necessary, restore the zones saved pkgmap entries that were removed in
# the workaround for 6312956.
savedpkgmap=$ROOTDIR/var/sadm/pkg/SUNWswmt/save/pspool/SUNWswmt/pkgmap
backoutpkgmap=$ROOTDIR/var/sadm/patch/$mypatchid/smpatch.SUNWswmt.undo.pkgmap
if test -s $backoutpkgmap
then
    # We did save something.  However, installf might have been fixed
    # in the meantime, so we check whether we still need the workaround.
    if grep "smpatch" $savedpkgmap >/dev/null; then
	:
    else
	cat $backoutpkgmap >> $savedpkgmap
    fi
fi

#########################################################
# Restore the ppro* packages to the ones installed by -01
#########################################################
pkgadd_file=$ROOTDIR/var/sadm/patch/$mypatchid/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/$mypatchid/postbackout_undo $pkg
    rm -rf $ROOTDIR/var/sadm/patch/$mypatchid/postbackout_undo/$pkg
done

rm $ADMINFILE


