#!/bin/bash
#
#Generic daemon script
#

### BEGIN INIT INFO
# Provides: objecthandler
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the xevent daemon
### END INIT INFO


# source function library
set -e # end if anything fails
#set -x # trace script

SYSTEMCTL_SKIP_REDIRECT=1
_SYSTEMCTL_SKIP_REDIRECT=1

test -x /opt/xeventhandler/objecthandlerd || exit 0

export PATH=$PATH:/opt/xeventhandler:/opt/timerhandler:/usr/lib/knxd
export LD_LIBRARY_PATH=/opt/xeventhandler:$LD_LIBRARY_PATH

syslog=/dev/null
syserr=/tmp/xeventhandler.err

RETVAL=0

. /lib/lsb/init-functions

usage() {
	echo "$0 [options] start|restart|stop"
	echo "  -c <configfile> DEFAULT: /etc/xevent-handler.conf"
	echo "  -l <syslog>     DEFAULT: /dev/null"
	echo "  -e <errlog>     DEFAULT: /tmp/xeventhandler.err"
	echo "  -u <user>       DEFAULT: xeventhandler, if started as root"
}

start_handler() {
	# $1: directory of libhandledobject.so
	# $2: incarnation number
	# $3: r/u: start as [r]oot or [u]ser
	# $4 (optional): further parameters
	if ! [ -d "$1" ]
	then
		echo "$1: path does not exist."
		exit 1
	fi
	LD_LIBRARY_PATH_old=$LD_LIBRARY_PATH
	export LD_LIBRARY_PATH=$1:$LD_LIBRARY_PATH

	log_daemon_msg "Starting Event Handler in $1 with incarnation $2" "objecthandlerd"
	if [ -n "$4" ]
	then
		fp=" -- $4"
	fi
	(objecthandlerd $2 -c $configfile $fp &) 1>>$syslog 2>>$syserr
	sleep 2
	status_of_proc "objecthandlerd" "Event Handler Daemon in $1"
	if [ $? == "0" ] 
	then
		log_end_msg 0 || true
	else
	    log_end_msg 1 || true
	fi
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH_old

	if [ $1 == "/opt/timerhandler" ]
	then
		log_daemon_msg "Starting Heartbeat Daemon" "heartbeatd" || true
		(heartbeatd 1000 $configfile &) 1>>$syslog 2>>$syserr
		sleep 2
		status_of_proc "heartbeatd" "Heartbeat Daemon"
		if [ $? == "0" ] 
		then
			log_end_msg 0 || true
		else
			 log_end_msg 1 || true
		fi
	fi	
}

stop_process() {
	# $1: process name
	# $2: process description
	
	status=0
	pid=$(pidofproc $1) || status=$?
	if [ $status == 0 ]; then
		log_daemon_msg "Stopping $2" $1
		kill -9 $pid
		sleep 2
		status_of_proc "$1" "$2" || status=$?
		if [ $status == "0" ]
		then
			log_end_msg 1
			return 1
		fi
	fi
	return 0
}	

start () {

	# check config file
	if [ ! -f $configfile ]
	then
		echo "cannot open $configfile"
		log_end_msg 1
		exit 1
	fi

	# check, if process is already running
	status=0
	status_of_proc "objecthandlerd" "Event Handler Daemon for simple test" || status=$?
	if [ $status == "0" ] 
	then
		log_end_msg 1
		exit 1
	fi
	
	# init gpio
	if [ $(arch) = "armel" ]
	then
		echo "14" > /sys/class/gpio/export
		echo "in" > /sys/class/gpio/gpio14/direction
	fi

	# delete queue content
	key=$(ipcs -q |tail -2 |awk '{print $1;}')
	if [ "${key:0:2}" == "0x" ]
	then
		ipcrm -Q $key
	fi
	
	#delete error log
	if [ -f $syserr ]; then rm $syserr; fi
	
	# parse config file
	
	status=start

	while read line
	do
		case $status in
		start)
			if [ "$line" == "[handler]" ]
			then
				status=handler
				echo "starting handler ..."
			fi
		;;
		handler)
			if [ -z "$line" -o "${line:0:1}" == "#" ]
			then
				dummy=1 #do nothing
			elif [ "${line:0:1}" == "[" ]
			then
				# new chapter starting
				status="processed"
			else
				start_handler $line
			fi
		esac
	done <$configfile
}

status() {
	if [ `ps -ef | grep $PROG | grep -v grep | grep -v bash | wc -l` -gt 0 ]
	then
		echo -n $PROG" is running..."
		echo ""
	else
		echo -n $PROG" is stopped"
		echo ""
	fi
}

stop () {
	stop_process "heartbeatd" "Heartbeat Daemon"
	sleep 2
	stop_process "objecthandlerd" "Event Handler Daemon"
	stop_process "timerhandlerd" "Timer Handler Daemon"
	log_end_msg 0
}

# evaluate parameters

operation="NOP"
configfile="/etc/xevent-handler.conf"

while [ -n "$1" ]
do
	case "$1" in
	start)
		operation=start
		shift
		;;
	restart)
		operation=restart
		shift
		;;
	stop)
		operation=stop
		shift
		;;
	-c)
		configfile=$2
		shift 2
		;;
	-l)
		syslog=$2
		if [ -f $syslog ]; then rm $syslog; fi
		shift 2
		;;
	-e)
		syserr=$2
		shift 2
		;;
	-h)
		usage
		exit 0
		;;
	*)
		usage
		exit 1
		;;
	esac
done

case $operation in
start)
	start
	;;
stop)
	stop
	;;
restart)
	stop
	start
	;;
*)
	usage
	exit 1
	;;
esac

exit 0

