#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          mynotify
# Required-Start:    bpinetd $remote_fs $network
# Required-Stop:
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: mynotify
# Description:       Unitrends filesystem event monitor
### END INIT INFO

# Source function library.
failure_msg=""
success_msg=""

if [ -f /etc/init.d/functions ] ; then
	. /etc/init.d/functions
	failure_msg=echo_failure
	success_msg=echo_success
elif [ -f /etc/rc.d/init.d/functions ] ; then
	. /etc/rc.d/init.d/functions
	failure_msg=echo_failure
	success_msg=echo_success
elif [ -f /lib/lsb/init-functions ] ; then
	. /lib/lsb/init-functions
	failure_msg=log_success_msg
	success_msg=log_failure_msg
else
	exit 0
fi

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MYNOTIFY_BIN_PATH="/usr/local/bin/mynotify"
pidfile="/var/run/mynotify.pid"
RETVAL=0

start()
{
	if [ -e $pidfile ]; then
		echo -n "Process already running..."
		echo
		return 0
	fi

	echo -n "Starting File System Event Notification...:"
	daemon $MYNOTIFY_BIN_PATH "start"
	RETVAL=$?

	if [ $RETVAL -eq 0 ]; then
		touch $pidfile
		chmod 644 $pidfile
		chown $2:$2 $pidfile
		echo
		return $RETVAL
	fi

	$failure_msg
	echo "Failed to start notification."
	return $RETVAL
}

stop()
{
	echo -n "This service should not be stopped."
	$failure_msg
	echo
	return 1
}

restart()
{
	stop
	start
}

rstatus()
{
	if command -v status > /dev/null 2>&1; then
		status $MYNOTIFY_BIN_PATH
		RETVAL=$?
	elif command -v pgrep > /dev/null 2>&1; then
		PID=$(pgrep mynotify)
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "mynotify running, process $PID"
		fi
	elif command -v pidof > /dev/null 2>&1; then
		PID=$(pidof mynotify)
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "mynotify running, process $PID"
		fi
	fi

	return $RETVAL
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		rstatus
		;;
	*)
		echo $"Usage: $0 {start|status|stop}"
		exit 1
esac
