#! /bin/sh
### BEGIN INIT INFO
# Provides:          Start-Skript des Symcon-Dienstes
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: IP-Symcon
# Description:       IP-Symcon, Your Smart Home Software!
### END INIT INFO
# Author: Michael Maroszek <office@symcon.de>
 
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi 
 
# Actions
case "$1" in
    start)
	if [ "$(pidof symcon)" ]; then
      		echo "IP-Symcon is already running"
    	else
        	/usr/bin/symcon service > /dev/null &
			sleep 1
			echo "IP-Symcon started with PID $(pidof symcon)"
	fi
        ;;
        
    stop)
    PID=$(pidof symcon)
	if [ -z $PID ]; then
		echo "IP-Symcon is not running"
    else
		kill -15 $PID
		while ps -p $PID > /dev/null; do sleep 1; done;
		echo "IP-Symcon stopped"
	fi
        ;;
        
    restart)
	$0 stop
	sleep 1
	$0 start
        ;;

    status)
	pidof symcon >/dev/null
	status=$?
	if [ $status -eq 0 ]; then
		echo "IP-Symcon is running."
	else
		echo "IP-Symcon is not running."
	fi
	return $status
		;;
        
    *)
	echo "Usage $0 {start|stop|restart|status}"
	;;
	
esac

exit 0
