# 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

    # install package if not already installed
    rm -f ${ROOTDIR}/var/run/pkgrm_list_${mypatchid}; touch ${ROOTDIR}/var/run/pkgrm_list_${mypatchid}

    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_${mypatchid}
            fi
            continue
        fi
    done
    mv ${ROOTDIR}/var/run/pkgrm_list_${mypatchid} ${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
ZONENAME=global
GLOBAL_ZONE="false"
INSTALL_ORDER=" SUNWccccrr SUNWccccr SUNWccsign SUNWcsmauth SUNWccfw SUNWdc SUNWbreg SUNWcctpx SUNWccccfg SUNWccfwctrl SUNWccinv SUNWppror SUNWpprou SUNWppro-plugin-sunos-base SUNWupdatemgru SUNWupdatemgrr"                
ALWAYS_ADDED=""
ADD_IF_NOT_INSTALLED=" SUNWccccrr SUNWccccr SUNWccsign SUNWcsmauth SUNWccfw SUNWdc SUNWbreg SUNWcctpx SUNWccccfg SUNWccfwctrl SUNWccinv SUNWppror SUNWpprou SUNWppro-plugin-sunos-base SUNWupdatemgru SUNWupdatemgrr"                

[ -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#
Jan_version_list="SUNWppror SUNWpprou SUNWppro-plugin-sunos-base"
May_version_list="SUNWcsmauth SUNWswmt SUNWupdatemgrr SUNWupdatemgru"


SUNWppror_VERSION="5.0,REV=2005.01.09.21.19"
SUNWpprou_VERSION="5.0,REV=2005.01.09.21.19"
SUNWpproXXXpluginXXXsunosXXXbase_VERSION="5.0,REV=2005.01.09.21.19"

# 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.10"
SUNWswmt_VERSION="11.10,REV=2005.01.10.15.35"

SUNWcsmauth_VERSION="0.1,REV=2005.05.12.11.43"
SUNWupdatemgrr_VERSION="0.1,REV=2005.05.20.11.37"
SUNWupdatemgru_VERSION="0.1,REV=2005.05.20.11.37"


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 (-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/$mypatchid/pkgadd_list
for pkg in $Jan_version_list ; do
    ver=`pkgparam -R $ROOTDIR $pkg VERSION`
    if [ "$ver" == "5.0,REV=2005.05.13.17.01" ]; then
        if [ ! -f $pkgadd_file ]; then
            touch $pkgadd_file
            mkdir $ROOTDIR/var/sadm/patch/$mypatchid/postbackout_undo
        fi
        pkgadd -a $ADMINFILE -R $ROOTDIR -d payload.pkg $pkg

        pkgtrans undo_pkgs.pkg $ROOTDIR/var/sadm/patch/$mypatchid/postbackout_undo $pkg

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

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

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

exit 0



