#!/bin/sh
#
# Startup script for the CLUSTERPRO webmanager server
#
# chkconfig: 35 91 04
# description: CLUSTERPRO WebManager Server Script
#
### LSB #############################################################
# /etc/init.d/clusterpro_webmgr
#
### BEGIN INIT INFO
# Provides: clusterpro_webmgr
# Required-Start: clusterpro_trn clusterpro_ib
# Required-Stop: clusterpro_trn clusterpro_ib
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the clusterpro webmanager server
### END INIT INFO

#####################################################################
#
# main()
#

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]
then
	. /etc/rc.d/init.d/functions
elif [ -f /etc/rc.status ]
then
	. /etc/rc.status
fi

. /opt/nec/clusterpro/bin/clpfunctions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/nec/clusterpro/bin
LD_LIBRARY_PATH=/opt/nec/clusterpro/lib
MALLOC_CHECK_=0
LANG=C

export PATH
export LD_LIBRARY_PATH
export MALLOC_CHECK_
export LANG

# See how we were called.
case "$1" in
  start)
	targetos=`/bin/uname`
	clp_filedel "$1" init_webmgr $targetos
	clp_logwrite "$1" "clusterpro_webmgr start process start." init_webmgr

	# don't remove next line
	#daemon clpwebmc

 	#
	cd /opt/nec/clusterpro/bin

	#
	if [ "$targetos" = "SunOS" ]
	then
		echo "Starting clusterpro webmanager server: \c"
	else
		echo -n "Starting clusterpro webmanager server: "
	fi
	#
	clp_logwrite "$1" "clppidof start." init_webmgr
	pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --webmgr 2>&1`
	clp_logwrite "$1" "clppidof end.(pids=${pids})" init_webmgr
	
	if [ "$pids" != "" ]
	then
		clp_logwrite "$1" "webmgr already exist." init_webmgr
		clp_logwrite "$1" "clusterpro_webmgr start process end." init_webmgr
		clp_failed $targetos
		exit 1
	fi

	# starting clusterpro webmanager server
	clp_logwrite "$1" "webmgr start process start." init_webmgr
	./clpmonp --webmgr -a 2 -o "-start" -r 0 -w 0 > /dev/null 2>&1
	monp_ret=$?
	clp_logwrite "$1" "webmgr start process end.("$monp_ret")" init_webmgr

	clp_checkerr "$monp_ret" $targetos
	chk_ret=$?
	if [ "$chk_ret" = "1" ]
	then
		clp_logwrite "$1" "clusterpro_webmgr start process end." init_webmgr
		exit 1
	fi

	#
	if [ "$targetos" = "Linux" ]
	then
		clp_logwrite "$1" "touch start." init_webmgr
		touch /var/lock/subsys/clusterpro_webmgr
		clp_logwrite "$1" "touch end." init_webmgr
	fi
	
	clp_logwrite "$1" "clusterpro_webmgr start process end." init_webmgr
	;;
  stop)
	targetos=`/bin/uname`

	clp_filedel "$1" init_webmgr $targetos
	clp_logwrite "$1" "clusterpro_webmgr stop process start." init_webmgr

	# don't remove next line
	#killproc clpwebmc

	#
	if [ "$targetos" = "SunOS" ]
	then
		echo "Shutting down clusterpro webmanager server: \c"
	else
		echo -n "Shutting down clusterpro webmanager server: "
	fi

	#
	cd /opt/nec/clusterpro/bin

	#
	clp_logwrite "$1" "clppidof start." init_webmgr
	pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --webmgr 2>&1`
	clp_logwrite "$1" "clppidof end.(pids=${pids})" init_webmgr
	
	if [ "$pids" != "" ]
	then
		# shutting down clusterpro webmanager server
		clp_logwrite "$1" "webmgr stop process start." init_webmgr
		if [ "$targetos" = "SunOS" ]
		then
			kill -17 "$pids" 2>/dev/null
			kill_ret=$?
		else
			kill -12 "$pids" 2>/dev/null
			kill_ret=$?
		fi
		clp_logwrite "$1" "webmgr stop process end.("$kill_ret")" init_webmgr

		i=0
		while [ "$i" -lt "25" ]
		do
			clp_logwrite "$1" "clppidof2 start." init_webmgr
			pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --webmgr 2>&1`
			clp_logwrite "$1" "clppidof2 end.(pids=${pids})" init_webmgr
			
			if [ "$pids" = "" ]
			then
				clp_logwrite "$1" "webmgr stop success." init_webmgr
				clp_success $targetos

				#
				if [ "$targetos" = "Linux" ]
				then
					rm -f /var/lock/subsys/clusterpro_webmgr
				fi
				clp_logwrite "$1" "clusterpro_webmgr stop process end." init_webmgr
				exit 0
			fi

			sleep 1
			i=`expr "$i" + 1`
		done

		clp_logwrite "$1" "webmgr stop timeout." init_webmgr
	else
		clp_logwrite "$1" "webmgr not exist." init_webmgr
		clp_success $targetos

		#
		if [ "$targetos" = "Linux" ]
		then
			rm -f /var/lock/subsys/clusterpro_webmgr
		fi
		clp_logwrite "$1" "clusterpro_webmgr stop process end." init_webmgr
		exit 0
	fi

	clp_logwrite "$1" "clusterpro_webmgr stop process end." init_webmgr
	clp_failed $targetos
	exit 1
	;;
  restart)
	$0 stop
	if [ "$?" != "0" ]
	then
		exit 1
	fi

	sleep 1
	$0 start
	;;
  status)
	targetos=`/bin/uname`
	if [ "$targetos" = "SunOS" ]
	then
		clp_status_s clpwebmc
	else
		clp_status clpwebmc
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac

exit 0
