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

SUCCESS=0
FAILURE=1

start_failure=no
SERVERROOT=
instance_list=
instance_name=

# 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
	exit 1
    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"
}

# Get list of dps instances on the target server
get_instance_list () {
    instance_list=`ls -d ${SERVERROOT}/dps-* 2>/dev/null`
}

# Warns to manually restart a DPS instance on the target server
warns_manual_restart () {

    if [ ${start_failure} = no ]
    then
	start_failure=yes
	echo "You need to restart the servers after removing 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_torestart_dps=`echo /${cmd_torestart_dps} | sed "s,^$ROOTDIR,,"`
    cmd_torestart_dps=`echo ${cmd_torestart_dps} | sed "s,//*,/,g"`
    echo "${cmd_torestart_dps}\n"
}

# restart_instance ()
#
# If ran on the target server
# Then 
#   Tries to start a DPS instance
#   Best effort, no exit
# Else
#   Warns to manually stop
#
restart_instance () {

    cmd_torestart_dps="${instance_name}/restart-dps"
    cmd_torestart_dps=`echo ${cmd_torestart_dps} | sed "s,//*,/,g"`

    if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    then
	# patch is applied locally	
	if [ -f ${cmd_torestart_dps} ]
	then
	    ${cmd_torestart_dps}
	fi
	   
	if [ ! -f ${instance_name}/tmp/DPS.pid ]
	then
	    warns_manual_restart
	fi
    else
	warns_manual_restart
    fi
}

# Tries to restart DPS instances
restart_instances () {

    get_instance_list

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

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

get_serverroot
err=$?

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

	if [ -h ${SERVERROOT}/bin/dps ]
	then
	    # Restart instances
	    restart_instances
	    err=$?
	    exit ${err}
	fi
    fi
    debug "No action required from $0 script."
    err=${SUCCESS}
fi

exit ${err}


