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

SUCCESS=0
FAILURE=1

stop_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 stop DPS instance on the target server
warns_manual_stop () {

    if [ ${stop_failure} = no ]
    then
	stop_failure=yes
	echo "You need to stop the servers before 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_tostop_dps=`echo /${cmd_tostop_dps} | sed "s,^$ROOTDIR,,"` 
    cmd_tostop_dps=`echo ${cmd_tostop_dps} | sed "s,//*,/,g"`
    echo "${cmd_tostop_dps}\n"
}

# Manually update the CDS
errors_manual_update_cds () {

    echo ""
    echo "*******************************************"
    echo "The CDS will not be updated by this script. "
    echo "If you have not updated it manually before removing that patch, you will have to"
    echo "reapply that patch, check that the CDS is running "
    if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    then
    echo "and run the following commands:"
    else
    echo "log on the machine on which this patch is to be installed and run the following commands:"
    fi
    cmd_cds=`echo /${cmd_sync_cds} | sed "s,^$ROOTDIR,,"` 
    cmd_cds=`echo ${cmd_sync_cds} | sed "s,//*,/,g"`
    echo "${cmd_cds}\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
	    servername=`/bin/basename $instance_name | /bin/awk '{ print substr($0,7,length($0)-6) }'` 
	    ${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 server 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

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

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

get_serverroot
err=$?

if [ ${err} = ${SUCCESS} ]
then
    if [ "x"$SERVERROOT != "x" ]
    then
	# Synchronize dps part of serverroot only 
        # if it has been configured (at least one instance exists)
	if [ -d ${SERVERROOT}/bin/dps ]
	then
	    stop_instances
	    if [ ${stop_failure} = yes ]
	    then
		echo "$0 aborted."
		exit ${FAILURE}
	    fi

	    if [ "x"$VERBOSE != "x" ] ; then
		VERBOSE_OPTION="-verbose"
	    else
		VERBOSE_OPTION=""
	    fi

	    # Set LD_LIBRARY_PATH is required when patching from remote server
	    INSTAL_LD_LIBRARY_PATH=${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/lib:${AS_BASEDIR}/usr/lib/mps/secv1:${AS_BASEDIR}/usr/lib/mps:${AS_BASEDIR}/usr/lib/mps/sasl2:${AS_BASEDIR}/usr/lib

	    # Downgrade serverroot - env is required when remote and instal servers have different OS levels
	    cmd_sync_dps="${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/bin/tcl8.2/tclsh ${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/bin/dps/install/script/sync-dps.tcl -syncType downgrade -cid ${AS_BASEDIR}/usr/sadm/mps/admin/v5.2 -serverroot ${SERVERROOT} ${VERBOSE_OPTION}"
	    cmd_sync_dps=`echo ${cmd_sync_dps} | sed "s,//*,/,g"`
	    env LD_LIBRARY_PATH=$INSTAL_LD_LIBRARY_PATH ${cmd_sync_dps}
	    err=$?
	    # Do not stop in error case

	    # Downgrade CDS
	    cmd_sync_cds="${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/bin/tcl8.2/tclsh ${AS_BASEDIR}/usr/sadm/mps/admin/v5.2/bin/dps/install/script/sync-dps-cds.tcl -cid ${AS_BASEDIR}/usr/sadm/mps/admin/v5.2 -serverroot ${SERVERROOT} -syncVersion 5.2 ${VERBOSE_OPTION}"
	    cmd_sync_cds=`echo ${cmd_sync_cds} | sed "s,//*,/,g"`
	    env LD_LIBRARY_PATH=$INSTAL_LD_LIBRARY_PATH ${cmd_sync_cds} >/dev/null 2>&1
	    err=$?

	    if [ ! ${err} = ${SUCCESS} ]
	    then	        
		errors_manual_update_cds
		# Considers as a warning
		err=${SUCCESS}
	    fi
	    exit ${err}

	fi
    fi
    debug "No action required from $0 script."
    err=${SUCCESS}
fi

exit ${err}
