#!/bin/bash

LOGGER="/bin/logger -t init.d"
SERVICE=crossbar
PROCESS_NAME=crossbar
PIDFILE=/tmp/node.pid

if [ -f /etc/image_features ] ; then
	. /etc/image_features
else
	echo
	echo
	echo Fatal error: Could not find 'image_features' file
	echo
	exit 1
fi

ServiceCheckRunning1 () {
	local PID;
	if [ -r "$PIDFILE" ] ; then
		PID=$(/bin/cat $PIDFILE | sed '/pid/!d' | sed s/\"pid\":\ //g | sed s/\"//g | awk -F, '/,/{gsub(/ /, "", $1); print$1} ')
		if [[ "$PID" -eq "$PID" && ! "$PID" -eq "0" ]] ;  then
			# Check if running
			/bin/kill -0 $PID > /dev/null 2>&1
			if [ $? = 0 ] ; then
				$LOGGER $SERVICE: already running
				echo "$0 is already running."
				return 1
			fi

			$LOGGER ServiceCheckRunning1: $SERVICE: state undefined
			/bin/rm -f "$PIDFILE"
			if [ -n "$PROCESS_NAME" ] ; then
				/bin/killall $PROCESS_NAME
			fi
			echo "$0 is in an undefined state. PID file exists but no corresponding process. Killing alls apps and removing PID file."
			return 0
		fi
	fi
	return 0
}

ServiceCheckRunning2 () {
	local PID;
	local i;

	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do
		if [ -r "$PIDFILE" ] ; then
			PID=$(/bin/cat $PIDFILE | sed '/pid/!d' | sed s/\"pid\":\ //g | sed s/\"//g | awk -F, '/,/{gsub(/ /, "", $1); print$1} ')

			if [[ "$PID" -eq "$PID" && ! "$PID" -eq "0" ]] ;  then
			# Check if running
			/bin/kill -0 $PID > /dev/null 2>&1
			if [ $? = 0 ] ; then
				$LOGGER $SERVICE: started
				return 0
			fi

			$LOGGER $SERVICE: start failed
			echo "$0 start failed"
			return 1
			fi
		fi
		/bin/usleep 50000
	done

	$LOGGER $SERVICE: start failed
	echo "$0 start failed"

	return 1
}

ServiceStop () {
	local PID;

	if [ -r "$PIDFILE" ] ; then
		PID=$(/bin/cat $PIDFILE | sed '/pid/!d' | sed s/\"pid\":\ //g | sed s/\"//g | awk -F, '/,/{gsub(/ /, "", $1); print$1} ')
	fi

	if [[ "$PID" -eq "$PID" && ! "$PID" -eq "0" ]] ;  then
		# Check if still running
		/bin/kill -0 $PID > /dev/null 2>&1
		if [ $? != 0 ] ; then
			$LOGGER ServiceStop: $SERVICE: state undefined
			/bin/rm -f "$PIDFILE"
			if [ -n "$PROCESS_NAME" ] ; then
				/bin/killall $PROCESS_NAME
			fi
			echo "$0 is in an undefined state. PID file exists but no corresponding process. Killing alls apps and removing PID file."
			return 0
		fi

		/bin/kill $PID > /dev/null 2>&1

		/bin/rm -f "$PIDFILE"

	else
		echo "$0 is not running (no PID file)"
		return 1
	fi

	return 0
}

ServiceStatus () {
	local PID;
	if [ -r "$PIDFILE" ] ; then
		PID=$(/bin/cat $PIDFILE | sed '/pid/!d' | sed s/\"pid\":\ //g | sed s/\"//g | awk -F, '/,/{gsub(/ /, "", $1); print$1} ')
	fi
	if [[ "$PID" -eq "$PID" && ! "$PID" -eq "0" ]] ;  then
		# Check if still running
		/bin/kill -0 $PID > /dev/null 2>&1
		if [ $? != 0 ] ; then
			$LOGGER ServiceStatus: $SERVICE: undefined state
			/bin/rm -f "$PIDFILE"
			if [ -n "$PROCESS_NAME" ] ; then
				/bin/killall $PROCESS_NAME
			fi
			echo "$0 is in an undefined state. PID file exists but no corresponding process. Killing alls apps and removing PID file."
			return 1
		fi

		echo "$0 is up and running"
	else
		echo "$0 is not running (no PID file)"
		return 1
	fi

	return 0
}

do_start ()
{
	if [ "$FEATURE_WAMP" = "0" ] ; then
		return 0
	fi

	if ! ServiceCheckRunning1 ; then
		return $?
	fi

	/bin/setsid /usr/bin/python2 /usr/bin/crossbar start --cbdir /tmp/ --config /etc/crossbar/config.json > /tmp/logs/node.log 2>&1 & 
	/bin/sleep 3

	/etc/init.d/wamp-system start
	/bin/sleep 1
	/etc/init.d/wamp-im start
	/bin/sleep 1
	/etc/init.d/wamp-extension start
	/bin/sleep 1
	/etc/init.d/acd start
	return $?
}

do_stop ()
{
	if [ "$FEATURE_WAMP" = "0" ] ; then
		return 0
	fi
	ServiceStop

	/etc/init.d/acd stop
	/bin/sleep 2
	/etc/init.d/wamp-system stop
	/bin/sleep 1
	/etc/init.d/wamp-im stop
	/bin/sleep 1
	/etc/init.d/wamp-extension stop
	return $?
}


do_status ()
{
	if [ "$FEATURE_WAMP" = "0" ] ; then
		return 0
	fi
	ServiceStatus 
	
	return $?
}

case "$1" in
	start)
		do_start
		ServiceCheckRunning2
		;;
	stop)
		do_stop
		;;
	status)
		do_status
		if [ $? = "1" ]; then
		    exit 1
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit $?
