#!/bin/sh
#
# Copyright (c) 1999-2001 by Sun Microsystems, Inc.
# All rights reserved.

#pragma ident	"@(#)bv_utils.sh	1.5	01/12/12 SMI"

#
# What:
#	This file contains various utility functions that
#	are called by the Broadvision service and monitor programs.
#	The inputs to this file are 
#
#	BVUSER:		The user who owns the BV files
#	BV1TO1_VAR:	The environment variable BV1TO1_VAR set in bv1to1.conf
#	HOSTNAME:	The BV_LOCAL_HOST to be used to start BV processes
#	ACTION:One of the following actions that has to be performed
#		-validate
#		-start
#		-stop
#		-postnet_stop
#		-probe
#		-check_config
#
# Return Value:
#	0 for successful completion of action
#	1 for failure to complete the action.
#set -x

PATH=/bin:/usr/bin:/usr/cluster/bin:/usr/sbin:/usr/cluster/lib/sc:$PATH;export PATH
syslog_tag="SC[SUNW.bv,bv_utils]"

# Checking the command syntax

if [ $# -ne 6 ]
then
	scds_syslog -p error -t $syslog_tag -m \
		 "INTERNAL ERROR usage:$0 BVUSER BV1TO1_VAR bv_local_host action IT_CONNECT_ATTEMPTS BV_ORB_CONNECT_TIMEOUT"
	exit 1
fi

BVUSER=$1
BV1TO1_VAR=$2
HOSTNAME=$3
action=$4
it_connect_attempts=$5
bv_orb_connect_timeout=$6

scds_syslog -p debug -t $syslog_tag -m \
		"arguments to bv_utils are $* "

# Temp Directory for any BV  or orbix logs or output"
TEMP_BV_DIR="/var/run/cluster/bv"

# The checkpoint file and the output file to be used to start orbixd
ORBIXD_CHECKPOINT_FILE="$TEMP_BV_DIR/orbixd-checkpoint-file"
ORBIXD_OUTPUT_FILE="$TEMP_BV_DIR/orbixd-output-file"
ORBIXD_TEMP_PID_FILE="$TEMP_BV_DIR/temp_pid_file"

#Failed daemons output file
FAILED_BV_DAEMONS="$TEMP_BV_DIR/failed_bv_daemons"

# sleep time and the number of times to check while waiting for hosts in the
# startup order to start

SLEEP_WHILE_HOSTS_STARTUP=2
NUMBER_OF_CHECKS=2

# Sleep time while waiting for BV processes to stop in bv_stop method

SLEEP_WHILE_STOPPING=2

#######################################################################
#check_bv_host_config()
# What:
#	Checks if any BV servers are configured to run on HOSTNAME
# How:
#	Login as BVUSER and then source the bv1to1.conf.sh file.
#	Get the list of all the hosts that are configured to run bv 
#	servers and bv daemons.
#
# Return value:
#	Return 0 if HOSTNAME is in the list 
#	Return 1 if HOSTNAME is not in the list
#
#######################################################################

check_bv_host_config()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=`/usr/bin/hostname`;export BV_LOCAL_HOST;

        # Sourcing the config file here
        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

	# Get the list of all the hosts configured to run BV servers
        bvhosts=\`xbvconf get_member -f @my-site host\`

        if [ -z "\$bvhosts" ]
        then
		scds_syslog -p error -t $syslog_tag -m \
                        "No hosts are configured in bv1to1.conf."
                exit 1
        fi

	# Check if HOSTNAME is in the list
        for i in \$bvhosts
        do
                if [ z"\$i" = z"$HOSTNAME" ]
                then
                        exit 0
                fi
        done
        exit 1

EOF

return $?

}
#End of function check_bv_server_config()


#######################################################################
# check_orbixd()
# What:
#	Check if the orbix deamon is running
#
# How:
#	Use pgrep to check if the orbix daemon is running
#
# Return values:
#	Return 0 if orbixd is running
#	Return 1 if orbixd is not running
#
#######################################################################

check_orbixd()
{

/usr/bin/pgrep -f "^orbixd" > /dev/null
if [ $? -ne 0 ]
then
	return 1
else
	return 0
fi

}
# End of check_orbixd function

#######################################################################
# trim_checkpoint_file()
#What:
#	Remove stale entries(PIDs) from the checkpoint file.
#How:
#	For each PID in the checkpoint file(the 4th field ) check if
#	the process is still running or not(use kill -0 PID).If all the
#	processes are running then do nothing.If some processes are running
#	and some are not,then remove the stale entries from the checkpoint 
#	file(use a temp file to keep the list of running processes).
#Return values:
#	Return 0 if successful
#	Return 1 if failure
#######################################################################

trim_checkpoint_file()
{
	chkpt=$ORBIXD_CHECKPOINT_FILE				
	somelive=no						
	dead="dummy"					
	for pid in `cut -d: -f 4 $chkpt`; do	
		if /bin/kill -0 $pid 2> /dev/null; then
			somelive=yes
			continue
		fi
		dead="$dead|$pid"
	done

	if [ "$dead" = dummy ]; then
		return 0
	fi

	if [ "$somelive" != yes ]; then
		> $chkpt
		return $?
	fi

	pattern="^[^:]*:[^:]*:[^:]*:($dead):"
	/usr/bin/rm -f $ORBIXD_TEMP_PID_FILE

	egrep -v "$pattern" $chkpt > $ORBIXD_TEMP_PID_FILE || exit $?
	/usr/bin/cat $ORBIXD_TEMP_PID_FILE > $chkpt || exit $?
	/usr/bin/rm -f $ORBIXD_TEMP_PID_FILE
	return 0
}
	
#######################################################################
# start_orbixd()
# What:
#	Start the orbix daemon 
# 
# How:
#	Login as BVUSER .Set the BV_LOCAL_HOST to the local physical 
#	hostname(refer design) and then source the bv1to1.conf.sh file.
#	Start the orbixd with the checkpoint file feature and redirect
#	the output to a output file.
#
# Return Value:
#	Return 0 if orbixd could be successfully started.
#
#	If orbixd could not be started then the function will wait in
#	an infinite while loop till orbixd could be started.We rely on
#	RGM to timeout.
#
#######################################################################

start_orbixd()
{
# The reason su - $BVUSER is not done is it will source the users shell
# Instead we run su $BVUSER -c /bin/sh and then source the bv1to1.conf.sh

#Change the permissions of the temp BV directory 
chown $BVUSER $TEMP_BV_DIR

#Check if the orbixd checkpointfile exists.
if [ -f $ORBIXD_CHECKPOINT_FILE ];then
	trim_checkpoint_file;
	if [ $? -ne 0 ];then
		scds_syslog -p error -t $syslog_tag -m \
			"Could not clear stale entries in the orbixd checkpoint file $ORBIXD_CHECKPOINT_FILE"
	return 1
	fi
fi
	
/usr/bin/su  $BVUSER -c /bin/sh  <<EOF

        BV_LOCAL_HOST=`/usr/bin/hostname`;export BV_LOCAL_HOST;

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

        orbixd -s -c $ORBIXD_CHECKPOINT_FILE > $ORBIXD_OUTPUT_FILE 2>&1 &

EOF

/usr/bin/pgrep -f "^orbixd" > /dev/null
if [ $? -eq 0 ]
then
        return 0
else
	sleep 2
        while true
        do
                /usr/bin/pgrep -f "^orbixd" > /dev/null
                if [ $? -eq 0 ]
                then
                        return 0
                else
			scds_syslog -p info -t $syslog_tag -m \
				"Waiting for orbixd to start."
                        sleep 2
			#We rely on RGM to timeout and terminate the program 
                fi
        done

	#Actually we may never reach here
        return 1
fi

}
# End of function start_orbixd


#######################################################################
#check_if_roothost()
# What:
#	Checks if HOSTNAME is the ROOT HOST
#
# How:
#	Login as BVUSER and set the BV_LOCAL_HOST to HOSTNAME and then
#	source the bv1to1.conf.sh file.This will set the BV1TO1_ROOT_HOST
#	variable.Check if this variable is set and if it is set check if
#	HOSTNAME is BV1TO1_ROOT_HOST.
#
# Return value:
#	Return 0 if HOSTNAME is the ROOT HOST
#	Return 1 if HOSTNAME is not the ROOT HOST
#	
#######################################################################
# Check if this is the ROOT HOST.

check_if_roothost()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST;

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

        if [ z"\$BV1TO1_ROOT_HOST" = z"$HOSTNAME" ]
        then
		exit 0
        else

                exit 1
        fi

EOF

return $?

}
#End of function checking if this is the root host


#######################################################################
# check_startup_order()
# What:
#	Check if the hosts in the startuporder are up and running or not.
#	This is required as the hosts in the startup order(defined in 
#	bv1to1.conf) should be running before processes on current host 
#	can be started.	
#
# How:
#	Login as BVUSER and source the bv1to1.conf.sh file after setting the
#	environment variable BV_LOCAL_HOST to the local physical hostname.
#	Get the list of all the hosts that are configured to run BV processes.
#	This list will be in the order the HOSTS should be started.
#	For each host in the list(till the current host is encountered)
#	use BV utility "checkdaemon" to check if the HOST is up or not.
#	Wait till is up.
#
# Return Value:
#	Return 0 is all the hosts configured in bv1to1.conf before HOSTNAME
#	are up and running.
#	
#	If any host in the list before HOSTNAME is not up then NUMBER_OF_CHECKS
#	times we check for the hosts each time sleeping for 
#	SLEEP_WHILE_HOSTS_STARTUP seconds
#
#######################################################################

# Check if the other hosts in the startup order are up and running

check_startup_order()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=`/usr/bin/hostname`;export BV_LOCAL_HOST;

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

        bvhosts=\`xbvconf get_member -f @my-site host\`

        # echo Configured bvhosts are \$bvhosts

        if [ -z "\$bvhosts" ]
        then
		scds_syslog -p error -t $syslog_tag -m \
                        "No hosts are configured in bv1to1.conf."
                exit 1
        fi

	# Begin for loop
        for i in \$bvhosts
        do
                if [ z"\$i" = z"$HOSTNAME" ] ;then
                        exit 0
                fi
		#check if this is an IM host
		xbvconf ps -c -h "\$i" -f -D -online -q | awk '{print \$3}' | /usr/bin/grep bvsmgr >/dev/null
		if [ \$? -eq 0 ] ;then
			break
		fi

		#The BV_ORB_CONNECT_TIMEOUT value is the timeout value for checkdaemon
		#trying to connect to orb servers and it would try IT_CONNECT_ATTEMPTS 
		#times before throwing an error.The BV_ORB_CONNECT_TIMEOUT is honoured
		#if BV_ORB_ABORT_SLOW_CONNECT is set to 1.

                BV_ORB_CONNECT_TIMEOUT=$bv_orb_connect_timeout ;export BV_ORB_CONNECT_TIMEOUT;
                IT_CONNECT_ATTEMPTS=$it_connect_attempts ; export IT_CONNECT_ATTEMPTS;
		BV_ORB_ABORT_SLOW_CONNECT=1;export BV_ORB_ABORT_SLOW_CONNECT;
                checkdaemon "\$i"
                if [ \$? -eq 1 ]
                then
			scds_syslog -p error -t $syslog_tag -m \
                                "The Host "\$i" is not yet up."
                        no_of_waits=0
                        daemon_up=1
                        while [ "\$no_of_waits" -lt $NUMBER_OF_CHECKS ]
                        do
                                checkdaemon "\$i"
                                if [ \$? -eq 0 ];then
                                        daemon_up=0
                                        break
                                else
					scds_syslog -p error -t $syslog_tag -m \
                                        "Waiting for the host \$i to startup."
                                        sleep $SLEEP_WHILE_HOSTS_STARTUP
                                        no_of_waits=\`expr \$no_of_waits + 1\`

                                fi
                        done
                        if [ "\$daemon_up" -eq 1 ]
                        then
                                exit 1
                        fi
                fi

        done
	# End for loop
        exit 0

EOF

return $?


}
# End of function checking the startup order

#######################################################################
# start_bv_orbix_servers()
# What:
#       Start the orbix servers
# How:
#       Login as BVUSER and then source the bv1to1.conf.sh file after
#       setting the BV_LOCAL_HOST variable to HOSTNAME.Start the servers
#       by running the BV command
#               "bvconf ping -l -a -q -h $HOSTNAME"
#
# Return Value:
#       Return 0 (Even if some orbix servers cannot be started we dont
#                consider it as an error as the orbixd will try to restart
#                the servers again.
#######################################################################
start_bv_orbix_servers()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF
        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

        PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

	#Remove the file in the following directory before starting.
	#This is to make sure the servers can be started.

	if [ -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME ];then
		/usr/bin/rm -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME
	fi

        #Forcing the orbix daemon to launch all the servers

        BV_ORB_CONNECT_TIMEOUT=$bv_orb_connect_timeout; export BV_ORB_CONNECT_TIMEOUT;
        IT_CONNECT_ATTEMPTS=$it_connect_attempts; export IT_CONNECT_ATTEMPTS;
        BV_ORB_ABORT_SLOW_CONNECT=1;export BV_ORB_ABORT_SLOW_CONNECT;

        \$BV1TO1/bin/bvconf ping -l -a -q -h \$BV_LOCAL_HOST >/dev/null

        if [ \$? -eq 0 ]
        then
                scds_syslog -p info -t $syslog_tag -m \
                        "Successfully started BV servers on $HOSTNAME."
        else
                scds_syslog -p info -t $syslog_tag -m \
                "Some BV servers could not be launched on $HOSTNAME.Check BV logs"
        fi

        exit 0
EOF

return $?

}
#End of start_bv_orbix_servers function


#######################################################################
# start_bv_server()
# What:
#	Start the processes on HOSTNAME
#
# How:
#	Login as BVUSER and then source the bv1to1.conf.sh file after
#	setting the BV_LOCAL_HOST variable to HOSTNAME.Start the servers
#	and daemons by using the BV command 
#		"bvconf restart -local -skip_orbixd"
#
# Return Value:
#	check the return value of bvconf and ....
#	Return 0 if the processes are started.
#	Return 1 if the processes cannot be started.
#	
#
#######################################################################

start_bv_server()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

	#Remove the file in the following directory before starting.
	#This is to make sure the servers can be started.

	if [ -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME ];then
		/usr/bin/rm -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME
	fi

        \$BV1TO1/bin/bvconf restart -local -d -skip_orbixd

        if [ \$? -eq 0 ]
        then

		scds_syslog -p info -t $syslog_tag -m \
			"Successfully started BV daemons on $HOSTNAME."

                exit 0
        else
		scds_syslog -p error -t $syslog_tag -m \
                        "Could not start BV 121 daemons on $HOSTNAME."
                exit 1
        fi
EOF

# Waiting for all the daemons to start is done in svc_wait and not here
#So we just exit from here and let the probe handle the part of checking if
#all the processes(daemons) are running or not

return $?

}
#End of start_bv_server function


#######################################################################
#bv_validate()
# What:
#	Validate the BV configuration for HOSTNAME
#
# How:
#	Check if the directory BV1TO1_VAR exists or not.Check if the
#	BV configuration file bv1to1.conf file is available or not.The
#	existance of bv1to1.conf.sh file is already checked in 
#	get_bv_extn_props function in BV.c file.The BVUSER is also checked 
#	in the above function.
#	After the above validation ,login as BVUSER and check if the HOSTNAME 
#	is configured to run BV servers or daemons.This is done by calling the 
#	function check_bv_host_config.Also check if a ROOT HOST is configured 
#	or not.Check if the BV utilities "bvconf and xbvconf" are available
#	in $BV1TO1/bin directory.
#
# Return Value:
#	Returns 0 if validation is successful
#	Returns 1 if validation is not successful
#	
#######################################################################

bv_validate()
{

if [ ! -d $BV1TO1_VAR ];then
	scds_syslog -p error -t $syslog_tag -m \
		"$BV1TO1_VAR is not a directory."
        return 1

fi
if [ ! -f ${BV1TO1_VAR}/etc/bv1to1.conf ];then
	scds_syslog -p error -t $syslog_tag -m \
                "No configuration file  ${BV1TO1_VAR}/etc/bv1to1.conf."
        return 1
fi

# As BVUSER check for $BV1TO1,bin,xbvconf,bvconf
# Check if any BV servers are configured to run on BV_LOCAL_HOST
# Also,check for the ROOT HOST and other parameters here.

/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=`/usr/bin/hostname`;export BV_LOCAL_HOST;

        #Sourcing the config file here
        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

        ROOTHOST=\`echo \$BV1TO1_ROOT_HOST\`

        if [ -z \$ROOTHOST ];then
		scds_syslog -p error -t $syslog_tag -m \
                        "No ROOT HOST CONFIGURED."
                exit 1
        fi

        BV1TO1_DIR=\`echo \$BV1TO1\`

        if [ -z \$BV1TO1_DIR ];then
		scds_syslog -p error -t $syslog_tag -m \
                        "BV1TO1 variable not set."
                exit 1
        fi

        if [ ! -f "\$BV1TO1/bin/bvconf" ];then
		scds_syslog -p error -t $syslog_tag -m \
                        "No executable \$BV1TO1/bin/bvconf"
                exit 1
        fi

        if [ ! -f "\$BV1TO1/bin/xbvconf" ];then
		scds_syslog -p error -t $syslog_tag -m \
                        "No executable \$BV1TO1/bin/xbvconf"
                exit 1
        fi

EOF

if [ $? -eq 1 ];then
        return 1
fi

# Check if this host is configured in the bv1to1.conf file
check_bv_host_config
if [ $? -eq 0 ]
then
	return 0
else
	scds_syslog -p error -t $syslog_tag -m \
		"$HOSTNAME is not configured for BV processes."
	return 1
fi

}
#End of bv_validate function

#######################################################################
# bv_start()
# What:
#	Starts servers/daemons on HOSTNAME if everything is OK(read below).
#
# How:
#	First checks if orbixd is running or not(calls check_orbixd)
#	If orbixd is running ,checks if this HOSTNAME is the ROOT HOST
#	by calling the function check_if_roothost().If this is the ROOT 
#	HOST then the processes on this host are started by calling 
#	start_bv_server.The reason we check if this is the ROOT HOST is,
#	ROOT HOST is the first host to be started and there is no need 
#	to check the startup order to start the processes on this host
#	and hence we can avoid that step of calling check_startup_order().
#
#	If this is not the ROOT HOST then check if the hosts in the startup
#	order are up and running.If they are running then start the processes
#	on this host otherwise return error
#
# Return Value:
#	Returns 0 if processes on HOSTNAME can be started started successfully
#	Returns 1 if processes cannot be started or if the hosts inthe startup 
#	order are not up and running.
#
#
#######################################################################

bv_start()
{

check_orbixd 
if [ $? -eq 1 ]
then
	return 1
fi

check_if_roothost 
if [ $? -eq 0 ]
then
	start_bv_server
	return $?
fi

# Check if the hosts in the startup order are up and running.
# The wait for the hosts to start up is done in the function itself
# and hence we just have to check for the exit status

check_startup_order 
if [ $? -eq 0 ]
then
	start_bv_server 
	return $?
else

	# The Hosts in the startup order arent up so dont try to start
	# the BV processes on the current host.The Probe would wait for
	# the dependent hosts to be up and then start the processes.
	# Just return 0.

	scds_syslog -p error -t $syslog_tag -m \
		"Dependent hosts are not up.Not starting BV servers on $HOSTNAME."
		 
	return 0
fi

}
# end bv_start()


#######################################################################
#bv_stop()
# What:
#	Stops all the BV servers and daemons in STOP method
#				(while HOSTNAME IP is UP)
#
# How:
#	Login as BVUSER and set the BV_LOCAL_HOST to HOSTNAME where the
#	processes have to be stopped.SOurce the bv1to1.conf.sh.Check if
#	any processes are running on HOSTNAME by using "xbvconf ps .."
#	If there are any processes running on HOSTNAME then shutdown the
#	server by using the bv command "bvconf shutdown...".
#
# Return value:
#	Return 0 if all the processes are stopped
#	
#	We wait in an infinite loop till all the processes are stopped.
#	We rely on the hatimerun timeout for the amount of wait time.
#
#
#######################################################################
bv_stop()
{
/usr/bin/su $BVUSER -c /bin/sh <<EOF

	#Touch the file in the following directory before stopping.
	#This is to make sure the orb servers dont get re-started .
	#This can happen if there is a client request.

	if [ ! -d ${BV1TO1_VAR}/host.disabled ];then
		/usr/bin/mkdir ${BV1TO1_VAR}/host.disabled
	fi
	if [ ! -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME ];then
		/usr/bin/touch ${BV1TO1_VAR}/host.disabled/$HOSTNAME
	fi

        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

        PROCESSES=\`xbvconf ps -D -f -q 2> /dev/null\`

        if [ -z "\$PROCESSES" ]
        then
		scds_syslog -p info -t $syslog_tag -m \
                        "Processes on $HOSTNAME are stopped."
                exit 0
        else
		#Shutdown the processes on the host specified.

                bvconf shutdown -local -f -skip_orbixd >/dev/null
		exit 0

        fi
	
	#We dont wait for all the processes to stop here because 
	#we anyway stop all the processes in postnet_stop.

EOF
if [ $? -eq 1 ];then
	scds_syslog -p error -t $syslog_tag -m \
		"Could not stop the BV processes on $HOSTNAME"

	#Remove the host.disabled/$HOSTNAME file created.
	if [ -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME ];then
		/usr/bin/rm -f ${BV1TO1_VAR}/host.disabled/$HOSTNAME
	fi
	
        return 1
else
        return 0
fi

}
# End of function to stop BV servers 

#######################################################################
# bv__postnet_stop()
# What:
#	Stops all the BV servers and daemons in POSTNET STOP method
#				(while HOSTNAME IP is DOWN)
#
# How:
#	Login as BVUSER and set the BV_LOCAL_HOST to HOSTNAME where the
#	processes have to be stopped.SOurce the bv1to1.conf.sh.Check if
#	any processes are running on HOSTNAME by using "xbvconf ps ..".
#	But unlike in STOP method(while the IP is UP), the BV_LOCAL_ORBIXD
#	variable has to be set to 127.0.0.1 so that the orbixd on the local
#	node is considered to get the process status.
#	If there are any processes running on HOSTNAME then shutdown the
#	server by using the bv command "bvconf shutdown...".
#
# Return value:
#	Return 0 if all the processes are stopped
#	
#	We wait in an infinite loop till all the processes are stopped.
#	We rely on the hatimerun timeout for the amount of wait time.
#
#######################################################################

bv_postnet_stop()
{

/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH

        BV_LOCAL_ORBIXD=127.0.0.1;export BV_LOCAL_ORBIXD;

        xbvconf ps -D -f -q 2> /dev/null
        PROCESSES=\`xbvconf ps -D -f -q 2> /dev/null\`

        if [ -z "\$PROCESSES" ]
        then
		scds_syslog -p info -t $syslog_tag -m \
                        "Processes on $HOSTNAME are stopped."
                exit 0
        else

                bvconf shutdown -local -f -skip_orbixd >/dev/null

        fi

        while true
        do
                PROCESSES=\`xbvconf ps -D -f -q 2> /dev/null\`
                if [ -z "\$PROCESSES" ]
                then
			scds_syslog -p info -t $syslog_tag -m \
                        	"Processes on $HOSTNAME are stopped."

                        exit 0
                else
			scds_syslog -p info -t $syslog_tag -m \
                        "some BV processes are still running on $HOSTNAME"

                        sleep $SLEEP_WHILE_STOPPING

                        xbvconf ps -f -D -q 2>/dev/null |/usr/bin/awk '{print \$6}' |xargs kill -9

                fi

        done
        exit 1

EOF
if [ $? -eq 1 ];then
	scds_syslog -p error -t $syslog_tag -m \
		"Could not stop the BV processes on $HOSTNAME"
        return 1
else
        return 0
fi
}
#End of function stopping BV servers in POST NET STOP

#######################################################################
# bv_probe()
# What:
#	Probe the orbixd and all BV configured daemons only,because the 
#	servers are automaticaly started by orbixd when they are needed.
#
# How:
#	Login as BVUSER and source the bv1to1.conf.sh
#	Check for orbix daemon by calling the check_orbixd() function.
#	Get the list of hosts configured to run BV processes and check
#	if HOSTNAME is in the list or not.If HOSTNAME is in the list check if
#	it is up or not by using the BV utility "checkdaemon".If the
#	host is up then check if all the configured online processes are
#	running or not.
#
# Return Value:
#	Return 0 if probe successful
#	Return 1 if Probe failed.
#
#######################################################################
bv_probe()
{

# Check for orbix daemon

check_orbixd 
if [ $? -eq 1 ]
then
	return 1
fi

# Check if this host is configured in the bv1to1.conf file
# Just another sanity check here to make sure the configuration hasnt
# changed after restartign the servers.

check_bv_host_config
if [ $? -ne 0 ]
then
	scds_syslog -p error -t $syslog_tag -m \
		"$HOSTNAME is not configured for BV processes."
	return 1
fi

# Check for other BV daemons
# If we detect a failure here we restart the processes in the
# start method.

/usr/bin/su $BVUSER -c /bin/sh <<EOF

        BV_LOCAL_HOST=$HOSTNAME;export BV_LOCAL_HOST;

        . $BV1TO1_VAR/etc/bv1to1.conf.sh

	PATH=/usr/cluster/bin:/usr/cluster/lib/sc:\$PATH;export PATH


	BV_ORB_CONNECT_TIMEOUT=$bv_orb_connect_timeout ;export BV_ORB_CONNECT_TIMEOUT;
	IT_CONNECT_ATTEMPTS=$it_connect_attempts ; export IT_CONNECT_ATTEMPTS;
	BV_ORB_ABORT_SLOW_CONNECT=1;export BV_ORB_ABORT_SLOW_CONNECT;
	checkdaemon "\$BV_LOCAL_HOST"

	if [ \$? -eq 1 ]
	then
		scds_syslog -p info -t $syslog_tag -m \
			"checkdaemon failed for $HOSTNAME."
		exit 1
	fi

	#Checking the daemon status
	xbvconf ps -f -d -z -q -online > $FAILED_BV_DAEMONS 2>/dev/null
	daemon_status=\` wc -l $FAILED_BV_DAEMONS 2>/dev/null| /usr/bin/awk '{print \$1}' \`

	if [ "\$daemon_status" -eq 0 ]
       	then
		scds_syslog -p info -t $syslog_tag -m \
               		"ALL the daemons are running on $HOSTNAME"

               	exit 0
	else
		scds_syslog -p info -t $syslog_tag -m \
               		"\$daemon_status daemons are not running on $HOSTNAME"
		exit 1

       	fi

EOF

return $?

}
# End of Probe function


#######################################################################
# main()
# What:
#	Calls various functions based on the action request.
#
# Return Value:
#	Returns whatever return value returned by the functions.
#
#######################################################################
main()
{

	case "$action" in

		validate)
			bv_validate 
			rc=$?
			;;
		start)
			bv_start 
			rc=$?
			;;
		stop)
			bv_stop 
			rc=$?
			;;
		postnet_stop)
			bv_postnet_stop 
			rc=$?
			;;
		probe)
			bv_probe 
			rc=$?
			;;
		orbixd_start)
			start_orbixd 
			rc=$?
			;;
		check_host_config)
			check_bv_host_config
			rc=$?
			;;
		check_host_startup_order)
			check_startup_order
			rc=$?
			;;
		start_orbix_servers)
			start_bv_orbix_servers
			rc=$?
			;;
		*)
			rc=1;;

	esac

	exit $rc

}
main
