#!/bin/sh
#
# hinemos_agent This shell script takes care of starting and stopping
#              	hinemos_agent.
#
# chkconfig: 2345 99 01
# description: Hinemos Agent

#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_agent/bin/hinemos_agent.cfg

RETVAL=0
HINEMOS_AGENT=${HINEMOS_AGENT_HOME}/bin/agent_start.sh
prog="hinemos_agent"

start() {
	echo -n $"Starting $prog: "
	daemon ${HINEMOS_AGENT}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch ${HINEMOS_AGENT_LOCK_FILE}
        return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
        killproc $prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f ${HINEMOS_AGENT_LOCK_FILE} ${HINEMOS_AGENT_PID}
	return $RETVAL
}

# See how we were called.
case "$1" in        
	start)
		start
		;;
        stop)
		stop
		;;
	status)
                if [ -f $HINEMOS_AGENT_PID ] ; then
                        read AGENT_PID < $HINEMOS_AGENT_PID
                        ps -p $AGENT_PID e | grep $HINEMOS_AGENT_HOME > /dev/null && TMP=1
                        if [ "$TMP" ] ; then
                                echo "Hinemos Agent (pid $AGENT_PID) is running..."
                                RETVAL=0
                        else
                                echo "Hinemos Agent is stopped"
                                RETVAL=3
                        fi
                else
                        echo "Hinemos Agent is stopped"
                        RETVAL=3
                fi
		;;
	restart)
		stop
		start
		;;
        *)
                echo $"Usage: $prog {start|stop|restart|status}"
                exit 1
esac

exit $RETVAL
