#!/bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Role : This script
# ----     - will stop DPS instances running on the server targeted by the patch
#
# set -x

SUCCESS=0
FAILURE=1
 
stop_failure=no
SERVERROOT=
instance_list=
instance_name=
err=${SUCCESS}

# Processes the VERBOSE env var to have more traces
debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

# 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
	debug "No package $1 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
    if [ $? -ne 0 ]; then
	return 1
    else
	PKGBASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
	if [ ${ROOTDIR} != '/' ] ; then
	    PKGBASEDIR=${ROOTDIR}${PKGBASEDIR}
            PKGBASEDIR=`echo ${PKGBASEDIR}| sed "s,//*,/,g"`
	fi
    fi
    debug "get_basedir returns $PKGBASEDIR"
    return 0
}


# Get the SERVERROOT from serverroot.conf. Result includes ROOTDIR
get_serverroot () {

    get_basedir SUNWasvu
    if [ $? -ne 0 ]; then
	return ${FAILURE}
    fi
    
    AS_BASEDIR=$PKGBASEDIR

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

# Get list of dps instances on the target server
get_instance_list () {

    instance_list=`ls -d ${SERVERROOT}/dps-* 2>/dev/null`
    # errors or not found => to empty list
    err=${SUCCESS}
}

# 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 [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
	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 () {

    #  Test if server is running
    if [ -f ${instance_name}/tmp/DPS.pid ]
    then
	cmd_tostop_dps="${instance_name}/stop-dps"
	cmd_tostop_dps=`echo ${cmd_tostop_dps} | sed "s,//*,/,g"`
	if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
	then
	    # patch is applied locally
	    ${cmd_tostop_dps}

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

# Try to stop DPS instances
stop_instances () {

    get_instance_list

    if [ "x"$VERBOSE != "x" ] ; then
	echo "Instances: ${instance_list}"
    fi

    if [ ${err} = ${SUCCESS} ]
    then
	# Stop all instances
	if [ ! "x${instance_list}" = "x" ]
	then
	    for instance_name in ${instance_list}
	    do
		stop_instance
	    done
	fi
    fi
}

######
# main
######

get_serverroot
err=$?

if [ ${err} = ${SUCCESS} ]
then
    
    if [ "x"$SERVERROOT != "x" ] ; then

	# Synchronize dps part of serverroot only when it exists
	if [ -h ${SERVERROOT}/bin/dps ]
	then
	    stop_instances
	    if [ ${stop_failure} = yes ]
	    then
		echo "$0 aborted."
		exit ${FAILURE}
	    fi
	    exit ${SUCCESS}
	fi
    fi
    debug "No action required from $0 script."
fi

exit ${err}
