#!/bin/sh
# description: Start or stop the fhem server
# Added by Alex Peuchert

### BEGIN INIT INFO
# Provides:             fhem.pl
# Required-Start:       $local_fs $remote_fs
# Required-Stop:        $local_fs $remote_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    FHEM server
### END INIT INFO

set -e
cd /opt/fhem
port=7072

if test "$2" != "noaptmark"; then
  apt-mark hold fhem > /dev/null
fi

case "$1" in
'start')

        echo "Starting fhem..."

# if you need to start hmland for use with
# Homematic, please start the hmland daemon
# like this (please use correct path and port, 
# depending on your installation!)
#
#       /opt/hmcfgusb/hmland -d -p 1234 -r 0
#

        perl fhem.pl fhem.cfg

# if you want to use configDB for configuration,
# use this command to start fhem:
#
#       perl fhem.pl configDB
#
# and remove/comment the above line including fhem.cfg

        RETVAL=$?
        ;;
'stop')
        echo "Stopping fhem..."

# if you want to stop hmland during fhem stop:
#       pkill hmland

        pkill -U fhem perl
        RETVAL=$?
        ;;
'status')
        cnt=`ps -ef | grep "fhem.pl" | grep -v grep | wc -l`
        if [ "$cnt" -eq "0" ] ; then
                echo "fhem is not running"
        else
                echo "fhem is running"
        fi
        ;;
*)
        echo "Usage: $0 { start | stop | status }"
        RETVAL=1
        ;;
esac
exit $RETVAL
