#! /bin/sh
#
# chkconfig: 2345 98 02
# description: Hinemos JBoss

#Copyright (C) since 2006 NTT DATA Corporation
#
#This program is free software; you can redistribute it and/or
#Modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation, version 2.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details

# Source function library.
. /etc/rc.d/init.d/functions

# Source config
. /opt/hinemos/hinemos.cfg


HINEMOS_JBOSS_LOCK_FILE=/var/lock/subsys/hinemos_manager

RETVAL=0
PROG="hinemos_manager"
USER=hinemos

JBOSS_STDOUT_LOG=/opt/hinemos/var/log/jboss_stdout.log

# Hinemosで使用するOpenLDAPを起動する
# 正常に起動した場合		: 0
# 起動に失敗した場合		: 1
# JBossが起動していない場合	: 2
# すでに起動している場合		: 3
ldap_start() {
	
	echo ""
	echo -n "Starting hinemos_ldap: "
	
	# check hinemos_jboss
	check_hinemos_jboss
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 2 
	fi
	
	# check hinemos_ldap
	check_hinemos_ldap
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 3 
	fi
	
	
	#daemon --user=${USER} ${LDAP}/libexec/slapd -h "ldap://0.0.0.0:24000/"
	daemon ${LDAP}/libexec/slapd -u ${USER} -h "ldap://0.0.0.0:24000/"
	
	RETVAL=$?
	
	if [ $RETVAL -eq 0 ] ; then
		for CNT in `seq 1 60`
		do
			sleep 1
			if [ -f ${HINEMOS_LDAP_PID} ] ; then
				return $RETVAL
					echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21
			fi
		done
	else
		return 1
	fi
}

# Hinemosで使用するOpenLDAPを停止する
ldap_stop() {
	
	echo ""
	echo -n "Stopping hinemos_ldap: "
	
	# check hinemos_jboss
	check_hinemos_jboss
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 1 
	fi
	
	
	kill -INT `cat ${HINEMOS_LDAP_PID}`
	RETVAL=$?
	
	if [ $RETVAL -eq 0 ] ; then
		echo_success
		echo ""
	else
		echo_failure
		echo ""
	fi
}

# Hinemosで使用するPostgreSQLを起動する
# 正常に起動した場合		: 0
# 起動に失敗した場合		: 1
# JBossが起動していない場合	: 2
# すでに起動している場合		: 3
pg_start() {
	
	echo ""
	echo -n "Starting hinemos_pg: "
	
	# check hinemos_jboss
	check_hinemos_jboss
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 2 
	fi
	
	# check hinemos_pg
	check_hinemos_pg
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 3 
	fi
	
	daemon --user=${USER} "${PG}/bin/pg_ctl start -D '${PGDATA}' -w -s -l ${HINEMOS_HOME}/var/log/postgresql.log" 2>&1
	
}

# Hinemosで使用するPostgreSQLを停止する
pg_stop() {
	
	echo ""
	echo -n "Stopping hinemos_pg: "
	
	# check hinemos_jboss
	check_hinemos_jboss
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 1 
	else 
		su - $USER -c "${PG}/bin/pg_ctl stop -D '${PGDATA}' -w -s -m fast"
		RETVAL=$?
		if [ $RETVAL -eq 0 ] ; then
			echo_success
			echo ""
		else
			echo_failure
			echo ""
			return $RETVAL
		fi
	fi
}

# Hinemosで使用するJBossを起動する
# 正常に起動した場合				: 0
# 起動に失敗した場合				: 1
# PostgreSQLが起動していない場合		: 2
# OpenLDAPが起動していない場合		: 3
# hinemos_jbossが起動している場合	: 4
# 他のJBossが起動している場合		: 5
jboss_start() {
	
	echo ""
	echo -n "Starting hinemos_jboss: "
	
	sleep 3
	
	# check hinemos_pg
	check_hinemos_pg
	TMP=$?
	if [ $TMP -eq 0 ] ; then
		return 2 
	fi
	
	# check LDAP
	check_hinemos_ldap
	TMP=$?
	if [ $TMP -eq 0 ] ; then
		return 3 
	fi
	
	# check hinemos_jboss
	check_hinemos_jboss
	TMP=$?
	if [ $TMP -eq 1 ] ; then
		return 4 
	fi
	
	# check another jboss
	if  ( ps -ef | grep "[o]rg.jboss.Main" > /dev/null ); then
		return 5
	fi
	
	# start jboss
	daemon --user=${USER} ${JBOSS_HOME}/bin/run_hinemos.sh -b ${IP_ADDRESS} > $JBOSS_STDOUT_LOG 2>&1 
	
	RETVAL=$?
	
	if [ $RETVAL -eq 0 ] ; then
		chown hinemos:hinemos $JBOSS_STDOUT_LOG
		touch ${HINEMOS_JBOSS_LOCK_FILE}
		echo_success
		echo ""
	else 
		echo_failure
	fi
	
	return $RETVAL
}

# Hinemosで使用するJBossを停止する
jboss_stop() {
	echo -n "Stopping hinemos_jboss: "
	${JBOSS_HOME}/bin/shutdown_hinemos.sh -S -s ${IP_ADDRESS} > /dev/null	
	RETVAL=$?
	TMP=0
	if [ $RETVAL -eq 0 ] ; then
		for CNT in `seq 1 60`
		do
			sleep 1
			ps -ef | grep ${JBOSS_HOME} | grep "org.jboss.Main" > /dev/null || TMP=1
			echo -n .
			if [ ${TMP} -ne 0 ] ; then
				echo ""
				success $"$PROG shutdown"
				rm -f ${HINEMOS_JBOSS_LOCK_FILE} ${HINEMOS_JBOSS_PID}
				return $RETVAL
			fi
		done
		failure $"$PROG shutdown"
	else 
		failure $"$PROG shutdown"
	fi
	return $RETVAL
}


# Hinemosで使用するPostgreSQLが起動しているか確認する
# 起動している場合		: 1
# 起動していない場合		: 0
check_hinemos_pg() {
	
	TMP=0
	# pid file exist
	if  [ -f ${HINEMOS_PG_PID} ];  then
		read PG_PID < ${HINEMOS_PG_PID}
		ps -p ${PG_PID} e | grep $PG > /dev/null && TMP=1
		
		# pg process exist
		if [ $TMP -eq 1 ] ; then
			return 1
		fi
	fi

	return 0
}

# Hinemosで使用するOpenLDAPが起動しているか確認する
# 起動している場合		: 1
# 起動していない場合		: 0
check_hinemos_ldap() {
	
	TMP=0
	# pid file exist
	if  [ -f ${HINEMOS_LDAP_PID} ];  then
		read LDAP_PID < ${HINEMOS_LDAP_PID}
		ps -p ${LDAP_PID} e | grep $LDAP > /dev/null && TMP=1
		
		# ldap process exist
		if [ $TMP -eq 1 ] ; then
			return 1
		fi
	fi

	return 0
}


# Hinemosで使用するJBossが起動しているか確認する
# 起動している場合		: 1
# 起動していない場合		: 0
check_hinemos_jboss() {
	
	TMP=0
	# pid file exist
	if  [ -f ${HINEMOS_JBOSS_PID} ];  then
		read JBOSS_PID < ${HINEMOS_JBOSS_PID}
		ps -p ${JBOSS_PID} e | grep "org.jboss.Main" > /dev/null && TMP=1
		
		# jboss process exist
		if [ $TMP -eq 1 ] ; then
			return 1
		fi
	fi

	return 0
}

# プロセスの有無を確認し、Hinemosの起動状態を出力する
# あわせてPIDファイルの内容を出力する
check_status() {
	
	# jboss
	check_hinemos_jboss
	JBOSS_RESULT=$?
	
	# postgresql
	check_hinemos_pg
	PG_RESULT=$?
	
	# openldap
	check_hinemos_ldap
	LDAP_RESULT=$?
	
	if [ $JBOSS_RESULT -eq 1 -a $PG_RESULT -eq 1 -a $LDAP_RESULT -eq 1 ] ; then
		RETVAL=0
		echo "Hinemos is running..."
	else
		RETVAL=3
		echo "Hinemos is stopped"
	fi
	
	echo "-------------------"
	echo "JBoss pid      : $JBOSS_PID"
	echo "PostgreSQL pid : $PG_PID"
	echo "OpenLDAP pid   : $LDAP_PID"
	echo "-------------------"
	
	return $RETVAL
}

# Hinemosマネージャを起動する
# PostgreSQL, OpenLDAPの起動に失敗した場合は、
# その時点で終了(exit 1)する
hinemos_manager_start() {
	
	pg_start
	PG_START=$?
#	echo $PG_START
	if [ $PG_START -eq 1 ] ; then 
		echo ""
		exit 1
	elif [ $PG_START -eq 2 ] ; then 
		failure "hinemos_jboss (pid $JBOSS_PID) is running..., ${PROG} startup"
		echo ""
		echo "hinemos_jboss (pid $JBOSS_PID) is running..."
		echo ""
		exit 1
	elif [ $PG_START -eq 3 ] ; then 
		failure "hinemos_pg (pid $PG_PID) is running..., ${PROG} startup"
		echo ""
		echo "hinemos_pg (pid $PG_PID) is running..."
		echo ""
	fi
	
	ldap_start
	LDAP_START=$?
#	echo $LDAP_START
	if [ $LDAP_START -eq 1 ] ; then
		echo "" 
		exit 1
	elif [ $LDAP_START -eq 2 ] ; then 
		failure "hinemos_jboss (pid $JBOSS_PID) is running..., ${PROG} startup"
		echo ""
		echo "hinemos_jboss (pid $JBOSS_PID) is running..."
		echo ""
		exit 1
	elif [ $LDAP_START -eq 3 ] ; then 
		failure "hinemos_ldap (pid $LDAP_PID) is running..., ${PROG} startup"
		echo ""
		echo "hinemos_ldap (pid $LDAP_PID) is running..."
		echo ""
	fi
	
	jboss_start
	JBOSS_START=$?
#	echo $JBOSS_START
	if [ $JBOSS_START -eq 1 ] ; then
		echo "" 
		exit 1
	elif [ $JBOSS_START -eq 2 ] ; then
		failure "hinemos_pg  is not running..., ${PROG} startup"
		echo ""
		echo "hinemos_pg is not running..."
		echo ""
	elif [ $JBOSS_START -eq 3 ] ; then
		failure "hinemos_ldap is not running..., ${PROG} startup"
		echo ""
		echo "hinemos_ldap is not running..."
		echo ""
	elif [ $JBOSS_START -eq 4 ] ; then
		failure "hinemos_jboss (pid $JBOSS_PID) is running..., ${PROG} startup"
		echo ""
		echo "hinemos_jboss (pid $JBOSS_PID) is running..."
		echo ""
	elif [ $JBOSS_START -eq 5 ] ; then
		failure "another jboss is running, ${PROG} startup"
		echo ""
		echo "another jboss is running"
		echo ""
	fi
}

# Hinemosマネージャを停止する
hinemos_manager_stop() {
	jboss_stop
	ldap_stop
	pg_stop
}

# See how we were called.
case "$1" in
	start)
		hinemos_manager_start
		;;
	stop)
		hinemos_manager_stop
		;;
	status)
		check_status
		;;
	restart)
		hinemos_manager_stop
		hinemos_manager_start
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

exit $RETVAL

