# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond

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
}

LocalZones () {
  # Only for FCS
  return 0
}

ExecuteDefaultCmds () {
    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
    return 0
}

ExecuteInProperEnvironment () {
    # check for local zones first. If we're not in local zone
    # then we must be in global zone.

    if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
        # Execute non-global whole root zone commands.
        # Should be same action as the default action.
        GLOBAL_ZONE="false"
        return 0
    fi

    if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
        # Execute non-global zone commands. Should be no action here
        GLOBAL_ZONE="false"
        return 0
   fi

    if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
        # Execute commands applicable to patching the mini-root.
        # There are usually no actions to take here since your patching
        # the mini-root on an install server.
        GLOBAL_ZONE="false"
        return 0
    fi

    if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
        # Execute commands specific to the mini-root
        GLOBAL_ZONE="false"
        return 0
    fi

    if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
        # Execute commands specific to diskless client
        GLOBAL_ZONE="false"
        return 0
    fi

    if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
        # Execute commands specific to an alternate root
        GLOBAL_ZONE="true"
        ExecuteDefaultCmds
        return $?
    fi

    if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
        # In a global zone and system is mounted on /.
        # Execute all commands.
        GLOBAL_ZONE="true"
        ExecuteDefaultCmds
        return $?
    fi

    return 1
} 

mypatchid=121454-02

UNINSTALL_ORDER="SUNWupdatemgrr SUNWupdatemgru SUNWppro-plugin-sunos-base SUNWpprou SUNWppror SUNWccinv SUNWccfwctrl SUNWccccfg SUNWcctpx SUNWbreg SUNWdc SUNWccfw SUNWcsmauth SUNWccsign SUNWccccr SUNWccccrr "
ALWAYS_UNINSTALL=""
ZONENAME=global
GLOBAL_ZONE="false"
[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

status=0
if [ -x $PKGCOND ] ; then
   ExecuteInProperEnvironment
   status=$?
else
   CheckZones
   if [ "${GLOBAL_ZONE}" = "true" ]; then
        ExecuteDefaultCmds
        status=$?
   else
        LocalZones
        status=$?
   fi
fi 

if [ $status -ne 0 ]; then
    exit $status
fi

# For now, we only execute special commands in global zone.
if [ "${GLOBAL_ZONE}" != "true" ]; then
    exit 0
fi


#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

exit 0
