
# 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 () {
    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
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/run/$mypatchid.basedir_list

#########################################################
# smpatch will be moved from SUNWswmt to another package.
# Save it so we can undo on backout.
#########################################################
smpatch_pkg=`grep "smpatch f" $ROOTDIR/var/sadm/install/contents | awk '{print $10}'`
if [ "$smpatch_pkg" == "SUNWswmt" ]; then
    cp -p $ROOTDIR/usr/sbin/smpatch $ROOTDIR/var/run/$mypatchid.smpatch.SUNWswmt.undo

    # Inform the system that we're updating the database
    removef -R $ROOTDIR SUNWswmt /usr/sbin/smpatch > /dev/null 2>&1
    removef -R $ROOTDIR SUNWswmt /usr/sadm/bin/smpatch > /dev/null 2>&1

    # Remove the files
    rm $ROOTDIR/usr/sadm/bin/smpatch
    rm $ROOTDIR/usr/sbin/smpatch

    # Update the database
    removef -R $ROOTDIR -f SUNWswmt 
fi

# Work around 6312956 - removef does not update zones saved
# pkgmap files.  Note that we use cp -p to preserve permissions
# on the pkgmap file.  We're cleaning up after both ourselves and
# -03, which is why we do this no matter what package smpatch is
# in.  -03 may have successfully moved it to SUNWpprou, but
# won't have cleaned up the SUNWswmt saved pkgmap.
(
    cd $ROOTDIR/var/sadm/pkg/SUNWswmt/save/pspool/SUNWswmt

    if grep "smpatch" pkgmap >/dev/null
    then
	backoutpkgmap=$ROOTDIR/var/run/$mypatchid.smpatch.SUNWswmt.undo.pkgmap
	cp -p pkgmap $backoutpkgmap
	grep "smpatch" pkgmap > $backoutpkgmap

	cp -p pkgmap pkgmap.tmp
	grep -v "smpatch" pkgmap > pkgmap.tmp
	mv pkgmap.tmp pkgmap
    fi
)

#########################################################
# Change the BASEDIR for SUNWbreg and SUNWdc
#########################################################
for pkg in SUNWbreg SUNWdc; do
    pkginfo_filename=$ROOTDIR/var/sadm/pkg/$pkg/pkginfo

    if [ ! -f $pkginfo_filename ]; then
        continue
    fi

    egrep 'BASEDIR *= */opt' $pkginfo_filename > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        
        cp -p $pkginfo_filename ${pkginfo_filename}.new
        sed 's/BASEDIR=.*/BASEDIR=\//' $pkginfo_filename > ${pkginfo_filename}.new
        if [ $? -eq 0 ]; then
            mv ${pkginfo_filename}.new $pkginfo_filename
            touch $basedir_list_file
            echo $pkg >> $basedir_list_file
        else
            rm -f ${pkginfo_filename}.new
        fi
    fi
done

exit 0
