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

# 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

# bash configuration
. /opt/hinemos/hinemos.cfg
. /opt/hinemos/sbin/hinemos_utility.sh

export PROG=`basename $0`
DIR=`dirname $0`
USER=`/usr/bin/whoami`
HOST=`hostname`

########################################
#  Local Variable
########################################

# lock file path for this service
HINEMOS_MANAGER_LOCK_FILE=/var/lock/subsys/hinemos_manager

# for twiddle JVM
JAVA_OPTS_TWIDDLE="-Xms16m -Xmx32m"

########################################
#  Local Message
########################################

# INFO
MSG_I001="Hinemos Manager is running..."
MSG_I002="Hinemos Manager is stopped"
MSG_I003="  - JBoss Process ID      :"
MSG_I004="  - PostgreSQL Process ID :"
MSG_I005="- Starting Hinemos RDBMS Server (PostgreSQL) : "
MSG_I006="- Starting Hinemos Application Server (JBoss) : "
MSG_I007="- Stopping Hinemos RDBMS Server (PostgreSQL) : "
MSG_I008="- Stopping Hinemos Application Server (JBoss) : "

# WARN
#MSG_W001="xxx"

# ERROR
MSG_E001="usage : ${PROG} [start|stop|status|restart]"

########################################
# Function
########################################

function hinemos_manager_start() {
	
	local RET=0
	
	# PostgreSQL
	CheckPostgreSQLStatus
	if [ $? -ne 0 ]
	then
		echo
		Logging "${MSG_I005}"
		daemon --user=${HINEMOS_PG_USER} /opt/hinemos/bin/pg_start.sh
		RET=$?
		if [ ${RET} -eq 0 ]
		then
			echo_success
			echo
		else
			echo_failure
			echo
			return 1
		fi
	fi
	
	# JBoss
	CheckJBossStatus
	if [ $? -ne 0 ]
	then
		echo
		Logging "${MSG_I006}"
		JBOSS_OPTS=""
		if [ "x${SERVICE_STARTUP_WAIT}" != "xtrue" ]
		then
			JBOSS_OPTS="-W"
		fi
		daemon --user=${HINEMOS_JBOSS_USER} /opt/hinemos/bin/jboss_start.sh ${JBOSS_OPTS}
		RET=$?
		if [ ${RET} -eq 0 ]
		then
			echo_success
			echo
		else
			echo_failure
			echo
			return 1
		fi
		echo
	fi
	
	return 0
}

function hinemos_manager_stop() {
	
	local RET=0
	
	# JBoss
	CheckJBossStatus
	if [ $? -eq 0 ]
	then
		echo
		Logging "${MSG_I008}"
		daemon --user=${HINEMOS_JBOSS_USER} /opt/hinemos/bin/jboss_stop.sh
		RET=$?
		if [ ${RET} -eq 0 ]
		then
			echo_success
			echo
		else
			echo_failure
			echo
			return 1
		fi
	fi
	
	# PostgreSQL
	CheckPostgreSQLStatus
	if [ $? -eq 0 ]
	then
		echo
		Logging "${MSG_I007}"
		daemon --user=${HINEMOS_PG_USER} /opt/hinemos/bin/pg_stop.sh
		RET=$?
		if [ ${RET} -eq 0 ]
		then
			echo_success
			echo
		else
			echo_failure
			echo
			return 1
		fi
		echo
	fi
	
	return 0
}

function hinemos_manager_status() {
	
	local FUNC_RET=0
	
	# Check ALL
	CheckHinemosManagerStatus
	FUNC_RET=$?
	
	return ${FUNC_RET}
}

########################################
# SHELL
########################################

case "$1" in
	start)
		hinemos_manager_start
		RET=$?
		;;
	stop)
		hinemos_manager_stop
		RET=$?
		;;
	status)
		hinemos_manager_status
		RET=$?
		;;
	restart)
		hinemos_manager_stop && hinemos_manager_start
		RET=$?
		;;
	*)
		echo "${MSG_E001}"
		RET=1
esac

exit ${RET}
