#!/usr/bin/ksh
#
#***************************************************************************
# File name: perfpr.ksh                                                       
# This script will start, stop warm, shutdown, restart and show the status of   # Prognosis and the last 50 lines of the log file.
# Usage: perfpr start|stop|shutdown|restart|status|log
# Return value:                                                                
# 		0   successful                                                
#		1   failure                                                   
# Author: Sanda Oancea - MRSS                                                 
# Date: February 04 , 2002                                                     
#
#  Modified (fairly heavily) - Rod O....  08/2005->04/2006
#
#
#***************************************************************************

#       Setting the PATH variable
#***************************************************************************

PATH=$PATH:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:.
export PATH

#       Declare Variables and Set Environment
#****************************************************************************

PROGNOSIS_HOME="/tools/prognosis/perf"
export PROGNOSIS_HOME
PATH="$PROGNOSIS_HOME/server:$PATH"
export PATH

PROGNOSIS_SCRIPTS="/tools/prognosis/scripts"
export PROGNOSIS_SCRIPTS

CONFIGDIR=$PROGNOSIS_HOME/server/configuration

#return value
rval=0 
failure=1
permission_denied=99

# calculating the number of prognosis processes
no_proc=`ps -ef |grep -v grep | grep /tools/prognosis/perf/server |awk '{print $1}' |wc -l `

SYSTEM=`uname`	#get the OS
#*****************************************************************************

usage()
{
	echo "Usage is: $0 start|stop|shutdown|restart|stoptran|starttran|status|log|addnet|rmnet|addpoststart|irnet|setdbase"
}

start_check()	#check to see if Prognosis is running
{
                if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -lt "1" ]
                then
                        echo "Failed to start Prognosis"
                        rval=$failure
                else
                        echo "Prognosis started"
                fi
}

stop_check()	#check to see if Prognosis is stopped
{
       if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -gt "0" ]
                then
                        echo "Failed to stop Prognosis"
                        rval=$failure
                else
                        echo "Prognosis stopped."
                fi
}

set_dbasefirst()
{
	poststartf=$PROGNOSIS_HOME/server/configuration/first_poststartprog.sh
	set_dbase
}

set_dbase()
{
	newdb=$1

	echo new dbase is $1

	if [ -z "$newdb" ]  ; then return 1 ; fi

	if [ ! -f "$newdb" ] ; then
		echo "$newdb doesn't exist, not adding it to $poststartf"
		return 2
	fi


	#safety first
	[ ! -f "$poststartf" ] && echo "$poststartf doesn't exist" && return 1

	cat $poststartf | grep -v `basename $newdb` > /tmp/`basename $poststartf`
	echo ircmd $newdb >> /tmp/`basename $poststartf`

	[ -s $poststartf ]  && cat /tmp/`basename $poststartf` > $poststartf

	RC=$?

	if [ $RC -ne 0 ] ; then
		echo "Couldn't update $poststartf"
		rm /tmp/`basename $poststartf`
		return $RC
	else
		echo "$poststartf updated"
		rm /tmp/`basename $poststartf`
	fi

	ircmd $newdb
}

update_irnetmon()
{
	echo "updating irnetmon.ini"
	IRSCRIPT=/tools/prognosis/scripts/updateirnetmon.sh 
	[ -f "$IRSCRIPT" ] && $IRSCRIPT
}


stop_tran()
{
	echo "Stopping transient process monitor"
	$PROGNOSIS_HOME/server/stopuxpcsrv
}


addpoststart()
{
	FIRSTPSCMD=$CONFIGDIR/first_poststartprog.sh
	PSCMD=$CONFIGDIR/poststartprog.sh 
	echo "Adding $1 to $PSCMD"

	if [ ! -f ${PSCMD} ]; then
		echo ; echo "Creating $PSCMD first"
		cp -p $FIRSTPSCMD $PSCMD
	fi
	
	TMPFILE=/tmp/prog.cmd.tmp


	NEW_PCMD=$1
	PCMD_NAME=`echo $NEW_PCMD | sed -e 's/^.*\///'`

	echo "Removing $PCMD_NAME from $PSCMD (if it's already there) "
	cat $PSCMD | grep -v $PCMD_NAME > $PSCMD.new

	echo "adding \"ircmd $NEW_PCMD\" to $PSCMD"
	echo "ircmd $NEW_PCMD" >> $PSCMD.new

	echo "Making a copy at $PSCMD.old"
	mv $PSCMD $PSCMD.old 
	mv $PSCMD.new $PSCMD
	chown perf:perf $PSCMD 
	chmod 774 $PSCMD  

}

start_tran()
{
	echo "Starting transient process monitor"
	$PROGNOSIS_HOME/server/startuxpcsrv
}


date    #print the date as requested by Pat Morassutti

if [ ! -f /tools/prognosis/perf/server/irnetrtr ]; then
           echo "Prognosis not installed"
           rval=$failure 
else

 case $1 in

'start_msg')
        echo "Starting Prognosis"
;;

'stop_msg')
        echo "Stopping Prognosis"
;;

irnet )
	update_irnetmon
;;

start )

if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -gt "0" ]
then
	echo "Prognosis is already started."
	rval=$failure
else

	if [ "`whoami`" = "perf" ]
	then
		$PROGNOSIS_HOME/server/startprog  
		start_check

	elif [ "`whoami`" = "root" ]
	then
		su   - perf -c "/tools/prognosis/perf/server/startprog"
		start_check
	else
                echo "You need to be PERF or ROOT to start Prognosis "
                rval=$permission_denied
	fi

fi
;;
stop )

	if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -lt "1" ]
	then
        	echo "Prognosis is already down. No need to stop it."
		rval=$failure
	else

		if [ "`whoami`" = "perf" ]
        	then
			/tools/prognosis/perf/server/stopprog  > /tools/prognosis/scripts/shutdown.log    
			stop_check

		elif [ "`whoami`" = "root" ]
	        then
		 	su - perf -c "/tools/prognosis/perf/server/stopprog >/tools/prognosis/scripts/shutdown.log"
			stop_check
		else
	                echo "You need to be PERF or ROOT to stop Prognosis "
                	rval=$permission_denied
		fi
	
	sleep 2	#give prognosis some time to shut down

        fi  
;;

shutdown )

        if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -lt "1" ]
        then
                echo "Prognosis is already down. No need to stop it."
                rval=$failure
        else

                if [ "`whoami`" = "perf" ]
                then
                        /tools/prognosis/perf/server/stopprog cold
                        stop_check

                elif [ "`whoami`" = "root" ]
                then
                        su - perf -c "/tools/prognosis/perf/server/stopprog cold"
                        stop_check
                else
                        echo "You need to be PERF or ROOT to stop Prognosis "
                        rval=$permission_denied
                fi
        
        sleep 2 #give prognosis some time to shut down

        fi
;;

restart )

                if [ "`whoami`" = "perf" ]; then
		       $PROGNOSIS_HOME/server/restartprognosis.sh
                elif [ "`whoami`" = "root" ]
                then
                        su - perf -c "/tools/prognosis/perf/server/restartprognosis.sh"
                else
                        echo "You need to be PERF or ROOT to stop Prognosis "
                        rval=$permission_denied
                fi
;;
status )
	if [ "`ps -ef |grep -v grep | grep -c irpromgr`" -gt "0" ]
	then
		echo "\nPrognosis is UP\n"
		echo "There are $no_proc prognosis processes running."


	if [ -f "$PROGNOSIS_HOME/server/configuration/prodver" ]; then
		echo "Prognosis version `cat $PROGNOSIS_HOME/server/configuration/prodver`" 
	elif [ -f "$PROGNOSIS_HOME/prodver" ] ; then
		echo "Prognosis version `cat $PROGNOSIS_HOME/prodver`" 
	else
		echo "Prognosis version undetermined"
	fi	

	echo


	# Determine the format of the ps command to use
	case $SYSTEM in
	   SunOS)
		ps -e -o user,ruser,pid,pgid -o comm |grep -v grep| grep /tools/prognosis/perf/server/ir
          ;;
           HP-UX)      export UNIX95=1
	 	ps -e -o user,ruser,pid,pgid -o comm |grep -v grep| grep ir
	  ;;
	   AIX) 
		ps -e -o user,ruser,pid,pgid,comm |grep -v grep| grep ir
	  ;;
         esac

	 else
		echo "\nPrognosis is DOWN\n"
	fi
;;			

log )

	tail -50 /tools/prognosis/perf/server/configuration/wvlog.txt
	echo "\nThe logfile is /tools/prognosis/perf/server/configuration/wvlog.txt\n"

 ;;

setdbase )
	if [ ! -z "$2" ]  ; then 
		poststartf=$PROGNOSIS_HOME/server/configuration/first_poststartprog.sh
		set_dbase $2
		poststartf=$PROGNOSIS_HOME/server/configuration/poststartprog.sh
		set_dbase $2
	else
		usage
	fi
	;;

starttran )
	start_tran
	;;

stoptran )
	stop_tran
	;;

rmnet )
	if [ ! -z "$2" ]  ; then 
		/tools/prognosis/scripts/updateconf.sh -D $2
	else
		usage
	fi
	;;
addnet )
	if [ ! -z "$2" ]  ; then
		/tools/prognosis/scripts/updateconf.sh -A $2
	else
		usage
	fi
	;;
ircmd )
	cd /tools/prognosis/perf/server/configuration

	shift
	echo "Executing ircmd $*"

	ircmd $*

	RC=$?
	cd - > /dev/null
	exit $RC
;;
addpoststart )
	if [ ! -z "$2" ]  ; then
		addpoststart $2
	else
		usage
	fi
	;;
* )		usage
		rval=$failure
		;;
esac
fi
date    #print the date, as per Pat Morassutti's request
exit $rval


