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

SUCCESS=0
FAILURE=1

warns_to_start=no
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
	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 restart a DPS instance on the target server
warns_manual_restart () {

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

}

# Manually update the CDS
warns_manual_update_cds () {

    echo ""
    echo "*********************************************************"
    echo "You need to update the CDS after 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
    cmd_cds=`echo /${cmd_sync_cds} | sed "s,^$ROOTDIR,,"`
    cmd_cds=`echo ${cmd_sync_cds} | sed "s,//*,/,g"`
    echo "${cmd_cds}\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 [ ${warns_to_start} = yes ]
    then
	warns_manual_restart
    else
	# patch is applied locally	
	if [ -f ${cmd_torestart_dps} ]
	then
	    ${cmd_torestart_dps}

	    if [ ! -f ${instance_name}/tmp/DPS.pid ]
	    then
		warns_manual_restart
	    fi
	fi
    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 dir path
get_serverroot
err=$?

# No serverroot => no upgrade (CDS & serverroot)

if [ ${err} = ${SUCCESS} ]
then
    if [ "x"$SERVERROOT != "x" ]
    then
	# Synchronize dps part of serverroot only 
        # if it has been configured by a first quickstart
	if [ -h ${SERVERROOT}/bin/dps ]
	then

	    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:$LD_LIBRARY_PATH

	    # Upgrade the 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 upgrade -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=$?
	
	    if [ ! ${err} = ${SUCCESS} ]
	    then
		echo "$0 script aborted."
		exit ${err}
	    else

		# Upgrade the 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} ${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
		    warns_manual_update_cds
		    warns_to_start=yes
		fi
		
		if [ ! "x${ROOTDIR}" = "x" ] && [ ! "x${ROOTDIR}" = "x/" ]
		then
		    warns_to_start=yes
		fi

		# Restart instances .. if possible
		restart_instances    

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

exit ${err}