#! /bin/bash
#
# This initscript was created by Ulises Vitulli <uvitulli@fi.uba.ar> for Auth2DB
#
# Start/stop the auth2db daemon
### BEGIN INIT INFO
# Provides:          auth2db
# Required-Start:    $syslog $remote_fs $time
# Required-Stop:     $syslog $remote_fs $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Log parser and database loader
# Description:       This daemon consists in a generic log parser and a database
#                    loader, making available to read either with a web frontend 
#		     or classic shell console UI.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/auth2db-daemon
NAME=auth2db
DESC="log examiner and database loader"
PIDFILE=/var/run/$NAME/$NAME.pid
USER=auth2db
GROUP=adm

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

# Include auth2db defaults if available
if [ -f /etc/default/auth2db ] ; then
        . /etc/default/auth2db
	if [ "$FREQUENCY" = "" ]; then
		log_failure_msg "FATAL ERROR: Daemon configuration is broken. 'FREQUENCY' parameter must have set a value."
		log_end_msg 1
		exit 1
	fi
	if [ "$FREQUENCY" -eq "0" ]; then
		log_failure_msg "FATAL ERROR: Frequency MUST be a value higher than 0. NOT STARTING."
		log_failure_msg "Check /usr/share/doc/auth2db/README.Debian for more information."
		log_end_msg 1
		exit 1
	elif [ "$FREQUENCY" -gt "0" ] && [ "$FREQUENCY" -lt "5" ] ; then
		if [ "$IWouldLoveToRosteMyDB" = "" ]; then
			log_warning_msg "It's recommended setting a frequency value higher than 5 seconds."
			log_warning_msg "Check /usr/share/doc/auth2db/README.Debian for more information."
		fi
	fi
else                            
        log_failure_msg "FATAL ERROR: auth2db daemon configuration file MISSING!"
	log_failure_msg "Check /usr/share/doc/auth2db/README.Debian"
        log_end_msg 1
	exit 1                  
fi

check_process() {
	# Is process really running???
	PID=$(cat "$PIDFILE")
	if ps --pid "$PID" | grep "$NAME" >/dev/null; then
		return 0
	else 
		return 1
	fi
}

case "$1" in
    start)
	log_daemon_msg "Starting $DESC daemon" "$NAME"
	if [ -s "$PIDFILE" ] && check_process ; then
	      log_action_msg "Already running."
	      log_end_msg 0
      	else 
		if [ ! -d "/var/run/$NAME" ]; then 
			mkdir -p /var/run/$NAME
			chown $USER:root /var/run/auth2db
		fi
		if start-stop-daemon --test --start --oknodo --user "$USER" >/dev/null \
				--pidfile "$PIDFILE" --exec $DAEMON $1; then
			su -p -s /bin/sh "$USER" -c "$DAEMON $1 3>/dev/null"
			sleep 1
		fi
	
		if [ ! -s "$PIDFILE" ]; then
			log_failure_msg "Oops, something went wront. CHECK SYSLOG!"
			log_end_msg $?
		fi
		log_end_msg $?
	fi
	;;
    stop)
	# Performing a full check of the status of the process
    	if [ -e "$PIDFILE" ]; then
		if [ -s "$PIDFILE" ]; then
			if check_process; then
				log_daemon_msg "Stopping $DESC daemon" "$NAME"
				$DAEMON $1
				log_end_msg $?
			else
				log_failure_msg "Pidfile exists but there is no process asossiated. (System halted prematurely?)"
				log_end_msg 1
			fi
		else
			log_begin_msg "Something went wrong. Pidfile exists but it is not assosiated with any process. Deleting..."
			rm "$PIDFILE"
			log_end_msg $?
		fi
	else
		log_progress_msg "$NAME is already stopped. Nothing to be done"
		log_end_msg 0
	fi		
	;;
    force-reload)
	$0 restart
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
    	if [ -s /var/run/$NAME/$NAME.pid ]; then
		log_action_msg "$NAME is RUNNING under PID $(cat /var/run/$NAME/$NAME.pid)." && exit 0
	else
		log_action_msg "$NAME is STOPPED!" && exit 1
	fi
	;;		
    *)
	log_success_msg "Usage: /etc/init.d/$NAME {start|stop|status|force-reload|restart}"
	exit 1
	;;
esac

exit 0
