#!/bin/bash
#
# ercole_agent
#
# chkconfig:   2345 80 30
# description: The Ercole Agent retrieves information about Oracle DB instances
# processname: ercole-agent
# config:      /opt/ercole-agent/config.json
#
### BEGIN INIT INFO
# Provides: ercole-agent
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop ercole-agent
# Description: ercole-agent retrieves information about Oracle DB instances
### END INIT INFO

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

# Source networking configuration.
. /etc/sysconfig/network

exec="/opt/ercole-agent/ercole-agent"
prog=$(basename $exec)
lockfile=/var/lock/subsys/$prog

LOGFILE=/var/log/ercole-agent.log

start() {
	touch $LOGFILE && chown ercole $LOGFILE
 	echo -n $"Starting $prog:"
	su - ercole -c "nohup $exec >> $LOGFILE 2>&1 < /dev/null &"
	retval=$?
	echo
	[ $retval -eq 0 ] && touch $lockfile
	return $retval
}

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

restart() {
	stop
	start
}

reload() {
	echo -n $"Reloading $prog: "
	killproc $prog -HUP
	echo
}

force_reload() {
	restart
}

fdr_status() {
	status $prog
}


case "$1" in
	start|stop|restart|reload)
  		$1
		;;
	force-reload)
		force_reload
		;;
	status)
		fdr_status
		;;
	condrestart|try-restart)
		[ -f $lockfile ] && restart
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
		exit 1
esac
