#!/bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Role
# ----
# - stop DPS instances
#
# set -x

#=============================================================================
#
# To have debug traces.
#
#=============================================================================

debug()
{
    [ "$VERBOSE2" != "" ] && echo $*
}

#=============================================================================
#
# Get a package name on the target server
#
#=============================================================================

get_pkgname()
{
    debug "get_pkgname $1"
    PKG=$1
    pkginfo -R $ROOTDIR $PKG.\* 1>/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Package $1 is not installed"
        return 1
    else
        PKGNAME=`pkginfo -R ${ROOTDIR} $1.\* | awk '{print $2}'`
    fi
    debug "get_pkgname returns $PKGNAME"
    return 0
}

#=============================================================================
#
# Get a package basedir on the target server. Result includes ROOTDIR
#
#=============================================================================

get_basedir()
{
    debug "get_basedir $1"    
    get_pkgname $1 || return 1

    PKGBASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
    if [ ${ROOTDIR} != '/' ] ; then
       PKGBASEDIR=${ROOTDIR}${PKGBASEDIR}
       PKGBASEDIR=`echo ${PKGBASEDIR}| sed "s,//*,/,g"`
    fi

    debug "get_basedir returns $PKGBASEDIR"
    return 0
}

#=============================================================================
#
# Get the SERVERROOT from serverroot.conf. Result includes ROOTDIR
#
#=============================================================================

get_serverroot ()
{
    get_basedir SUNWasvu || return 1
    AS_BASEDIR=$PKGBASEDIR

    _serverroot=/etc/mps/admin/v5.2/shared/config/serverroot.conf
    if [ -f ${AS_BASEDIR}$_serverroot ] ; then
        SERVERROOT=${ROOTDIR}`cat ${AS_BASEDIR}$_serverroot`
        SERVERROOT=`echo ${SERVERROOT} | sed "s,//*,/,g"`
    fi
    debug "get_serverroot returns $SERVERROOT"
    return 0
}

#=============================================================================
#
# Get list of dps instances on the target server
#
#=============================================================================

get_instance_list ()
{
    instance_list=`ls -d ${SERVERROOT}/dps-* 2>/dev/null`
    debug "instance list = [$instance_list]"
}

#=============================================================================
#
# Warns to manually stop a DPS instances on the target server
#
#=============================================================================

warns_manual_stop ()
{
    if [ ${stop_failure} = no ]
    then
        stop_failure=yes
        echo "You need to stop the servers before applying this patch."
        if [ "${ROOTDIR}" = "" ] || [ "${ROOTDIR}" = "/" ]
        then
            echo "Please run the following commands:"
        else
            echo "Please log on the machine on which this patch is to be"
            echo "installed and run the following commands:"
        fi
    fi
    cmd_tostop_dps=`echo /${cmd_tostop_dps} | sed "s,^$ROOTDIR,," ` 
    cmd_tostop_dps=`echo ${cmd_tostop_dps} | sed "s,//*,/,g"`
    echo "${cmd_tostop_dps}\n"
}

#=============================================================================
#
# stop_instance ()
#
# If ran on the target server
# Then 
#   Tries to stop a DPS instance
#   Exit if failed : stopping is a requested prerequisite to patchadd
# Else
#   Warns to manually stop and exit
#
#=============================================================================

stop_instance ()
{
    inst_name=$1

    debug "Stop DPS instances..."

    # If server is running then stop it
    if [ -f ${inst_name}/tmp/DPS.pid ]
    then
        rm -f /var/tmp/.shouldRestart_DPS_`basename ${inst_name}`
        cmd_tostop_dps="${inst_name}/stop-dps"
        cmd_tostop_dps=`echo ${cmd_tostop_dps} | sed "s,//*,/,g"`
        if [ "${ROOTDIR}" = "" ] || [ "${ROOTDIR}" = "/" ]
        then
            # patch is applied locally
            ${cmd_tostop_dps}

            # Failed to be stopped
            if [ -f ${inst_name}/tmp/DPS.pid ]
            then
                warns_manual_stop
            else
                touch /var/tmp/.shouldRestart_DPS_`basename ${inst_name}`
            fi
        else
            # we can't stop the instance from distant server
            warns_manual_stop
        fi
    fi
}

#=============================================================================
#
# Try to stop all DPS instances
#
#=============================================================================

stop_instances ()
{
    get_instance_list
    for instance_name in "${instance_list}"
    do
       stop_instance $instance_name
    done
}

#=============================================================================
#
# main
#
#=============================================================================

#
# Checks
# - Init SERVERROOT variable, exit if error
# - No ServerRoot -> nothing to do -> exit
# - DPS is either a symlink or directory. If not then nothing to do
#
get_serverroot          || exit 1
[ "$SERVERROOT" != "" ] || exit 0
[ -h ${SERVERROOT}/bin/dps -o -d ${SERVERROOT}/bin/dps ]  || exit 0

#
# Let's stop all DPS instances
#
err=0
stop_failure=no
stop_instances
[ ${stop_failure} = yes ] && err=1

#
# That's all
#
exit $err

