#! /bin/sh
#
# $Id: gst.sh,v 1.33.20.2.24.11 2012/02/27 23:23:20 grands1 Exp $ Copyright (c) 2000-2005 EMC Corporation
#

#
# Copyright (c) 2000-2010 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

# chkconfig: 35 97 03
# description: NetWorker Management Console

### BEGIN INIT INFO
# Provides:       gst
# Required-Start: networker
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description:    gst
### END INIT INFO

GST_HOME=''
PUREBINDIR=''
ARCH='linux86'
BRAND='NetWorker'

if [ "${GST_PURE}" = "true" ] ; then
	bindir="${PUREBINDIR}"
else
	bindir="bin"
fi

if [ -n "${GST_HOME}" ] ; then
	productdir="${GST_HOME}"
else
	productdir=`/bin/csh -cf 'echo ~gst' 2>/dev/null`
fi

gstd="${productdir}/${bindir}/gstd"
code=0
arg0=`basename $0`
PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:$PATH:${productdir}/sybasa/bin:${productdir}/bin"
export PATH
NMC_TAG_FILE="NMC.clustersvr"

if [ "`whoami`" != "root" ]; then

    echo "$arg0: You must be root to execute this command." 
    exit 1
fi

get_dbsrv_name()
{
    # First check if we have a virtual host name
    gst_host_name=`awk '/gst_virt_host/ { print substr($4,2,length($4)-3) }' "$GST_HOME/etc/gstd.conf"`
    if [ ! "$gst_host_name" ]; then
	# No virtual host name so use physical host name
	gst_host_name=`hostname`
    fi
    # Prepare the database server name
    # Get the short name for the host in case it's fully qualified
    gst_db_name=`echo "gst_on_$gst_host_name" | sed 's/\..*//g' | sed -e 's/^\(.\{29\}\).*/\1/'`
    echo "$gst_db_name"
}

getnamefrompid()
{
	pid=$1
	
	if [ -x /usr/bin/ps ] ; then
		/usr/bin/ps -e | awk "/^ *$pid/ {print \$4}"
	else
		/bin/ps -e | awk "/^ *$pid/ {print \$4}"
	fi
}

stoptrapd()
{
	trapd_pid=`getpid gstsnmptrapd`
	# On some platforms, ps will truncate name to 8 characters 
	if [ -z "$trapd_pid" ]; then
		trapd_pid=`getpid gstsnmpt`
	fi
	if [ -n "$trapd_pid" ] ; then
		echo "A GST snmptrapd from previous instance is still running, killing..." >&2
		kill -TERM $trapd_pid
			sleep 1 
	fi
}

stopwebserver()
{
	if [ -f ${productdir}/apache/logs/httpd.pid ] ; then
		httpd_pid=`cat ${productdir}/apache/logs/httpd.pid`
		procname=`getnamefrompid $httpd_pid`
		if [ "X${procname}" = "Xhttpd" -o "X${procname}" = "Xhttpdapache" ] ; then
			echo "A GST web server (httpd) from previous instance is still running, killing..." >&2
			kill -TERM $httpd_pid
			sleep 3
		fi
	fi
}

stopdbserver()
{
	# For these platforms, since Sybase is 32 bit, reset library paths
	case "$ARCH" in
	    sol*)
		LD_LIBRARY_PATH="${productdir}/sybasa/lib"
		export LD_LIBRARY_PATH
		;;
	    aix*)
		LIBPATH="${productdir}/sybasa/lib"
		export LIBPATH
		;;
	    hp*)
		SHLIB_PATH="${productdir}/sybasa/lib"
		export SHLIB_PATH
		;;
	esac
	GST_DBSRV_NAME=`get_dbsrv_name`
	echo "A GST database server (dbsrv9) from previous instance is still running, stopping..." >&2
	${productdir}/sybasa/bin/dbstop -q -y ${GST_DBSRV_NAME} \
		    -o ${productdir}/logs/dbstop_output
}

getpid()
{
	if [ -x /usr/bin/ps ] ; then
		/usr/bin/ps -e | awk "/$1\$/ {print \$1}"
	else 
		/bin/ps -e | awk "/$1\$/ {print \$1}"
	fi
}

case "$ARCH" in
	sol*|aix*|hp*|sgi*|decaxp)
		bsd_echo="false"
		;;
	*)
		bsd_echo="true"
		;;
esac

if [ "$bsd_echo" = "true" ] ; then
	echo_n () {
		echo -n "$*"
	}
else
	echo_n () {
		/bin/echo "${*}\c"
	}
fi
 
# environment specific variables
case "$ARCH" in
	sol*)
		unset LD_LIBRARY_PATH_64	
		LD_LIBRARY_PATH="${productdir}/sybasa/lib64:${productdir}/${bindir}:${productdir}/apache/lib:$LD_LIBRARY_PATH"
		export LD_LIBRARY_PATH
		R_SHLIB_LD_LIBRARY_PATH="${productdir}/sybasa/lib64:${productdir}/${bindir}:${productdir}/apache/lib:$R_SHLIB_LD_LIBRARY_PATH"
		export R_SHLIB_LD_LIBRARY_PATH
		;;
	lin*)
		LD_LIBRARY_PATH="${productdir}/sybasa/lib:${productdir}/${bindir}:${productdir}/apache/lib:$LD_LIBRARY_PATH"
		export LD_LIBRARY_PATH
		R_SHLIB_LD_LIBRARY_PATH="${productdir}/sybasa/lib:${productdir}/${bindir}:${productdir}/apache/lib:$R_SHLIB_LD_LIBRARY_PATH"
		export R_SHLIB_LD_LIBRARY_PATH
		;;
	aix*)
		LIBPATH="${productdir}/sybasa/lib64:${productdir}/${bindir}:${productdir}/apache/lib:$LIBPATH"
		export LIBPATH
		LD_LIBRARY_PATH="${productdir}/sybasa/lib64:${productdir}/${bindir}:${productdir}/apache/lib:$LD_LIBRARY_PATH"
		export LD_LIBRARY_PATH
		R_SHLIB_LD_LIBRARY_PATH="${productdir}/sybasa/lib64:${productdir}/${bindir}:${productdir}/apache/lib:$R_SHLIB_LD_LIBRARY_PATH"
		export R_SHLIB_LD_LIBRARY_PATH
		;;
	hp*)
		SHLIB_PATH="${productdir}/${bindir}:${productdir}/sybasa/lib64:${productdir}/apache/lib:$SHLIB_PATH"
		export SHLIB_PATH
		LD_LIBRARY_PATH="${productdir}/${bindir}:${productdir}/sybasa/lib64:${productdir}/lib:${productdir}/apache/lib:$LD_LIBRARY_PATH"
		export LD_LIBRARY_PATH
		R_SHLIB_LD_LIBRARY_PATH="${productdir}/${bindir}:${productdir}/sybasa/lib64:${productdir}/apache/lib:$R_SHLIB_LD_LIBRARY_PATH"
		export R_SHLIB_LD_LIBRARY_PATH
		;;
esac

# OS specific code
case "$ARCH" in
	lin*)
		DEFAULT_HOME=/root
		;;
	sol*)
		OS_REL=`uname -r`
		OS_REL_COMP=`echo $OS_REL |sed 's?\.??'`
		if [ "$OS_REL_COMP" -lt "511" ]; then
			DEFAULT_HOME=/
		else
			DEFAULT_HOME=/root
		fi
		;;
	aix*|hp*)
		DEFAULT_HOME=/
		;;
esac

# Get PIDs of some important daemons
nsrexecd_pid=`getpid nsrexecd`
gstd_pid=`getpid gstd`

case "$1" in
    start)
	if [ -z "$nsrexecd_pid" ]; then
	    echo "ERROR: ${BRAND} client (nsrexecd) is not running" >&2
	    exit 1;
	fi
	if [ -n "$gstd_pid" ]; then
	    echo "ERROR: GST is already running!" >&2
	    exit 1;
	fi
	if [ ! -f "$HOME/.odbc.ini" ]; then
	    if [ -f "$DEFAULT_HOME/.odbc.ini" ]; then
		HOME="$DEFAULT_HOME"
		export HOME
	    else
		echo "ERROR: Could not access $HOME/.odbc.ini."
		echo "       Please verify that the HOME environment variable is set to"
		echo "       root's home directory."
		exit 1;
	    fi
	fi
	if [ -f "${productdir}/${bindir}/${NMC_TAG_FILE}" ]; then
		echo "GST has been configured as a HA application within a cluster."
		echo "Not starting standalone gstd daemon via init.d startup."
		exit 1;
	fi
	`stopwebserver`
	`stoptrapd`

	echo_n "Starting GST: "
	if [ -x "$gstd" ] ; then
	    "$gstd"
	    code=$?
	    echo "done."
	else
	    code=253
	    echo "ERROR: $gstd not found!" >&2
	fi
	;;
    stop)
	echo_n "Stopping GST: "
	if [ -n "$gstd_pid" ] ; then
	    kill -TERM $gstd_pid
	    if [ $? -ne 0 ]; then
		echo "ERROR: Could not kill -TERM $gstd_pid" >&2
		exit 1
	    fi;
	    
	    # wait a little bit for gstd to stop naturally
	    loop_delay=4
	    loop_count=5
	    loop_index=0
	    
	    while [ -n "$gstd_pid" ]; do
		sleep $loop_delay;
		echo_n '.'
		loop_index=`expr $loop_index + 1`
		if [ $loop_index -ge $loop_count ]; then
		    break;
		fi
		gstd_pid=`getpid gstd`
	    done;
	    echo ""
	    
	    # If gstd isn't down yet, print out warning and kill it.
	    gstd_pid=`getpid gstd`
	    if [ -n "$gstd_pid" ] ; then
		echo "WARNING: using kill -KILL to stop gstd" >&2
		kill -KILL $gstd_pid

		`stopdbserver`

		# Stop the web server if running
		`stopwebserver`

		# Stop trapd if running
		`stoptrapd`
	    fi
	    echo "done."
	    code=0
	else
	    code=254
	    echo "WARNING: $gstd not running!" >&2
	    # Stop the web server if running
	    `stopwebserver`
	    # Stop the db server if running
	    dbsrv9_pid=`getpid dbsrv9`
	    if [ -n "$dbsrv9_pid" ] ; then
		`stopdbserver`
	    fi
	    # Stop trapd if running
	    `stoptrapd`
	fi
	;;
    *)
	echo "usage: $arg0 { start | stop }" >&2
	code=255
	;;
esac

exit $code
