#!/bin/bash

# general config
. /etc/rc.conf
. /etc/rc.d/functions

LINKNX_BIN=/usr/bin/linknx
LINKNX_ARGUMENTS="-c -w -d"
PID=`pidof -o %PPID $LINKNX_BIN`

case "$1" in
    start)
        stat_busy "Starting linknx"
        if [ ! -f /var/lib/linknx/linknx.xml ]; then
          cp /usr/share/doc/linknx/linknx.xml /var/lib/linknx/linknx.xml
        fi
        if [ -z "$PID" ]; then
          stat_busy "Starting linknx"
          $LINKNX_BIN $LINKNX_ARGUMENTS &
        fi
        if [ ! -z "$PID" -o $? -gt 0 ]; then
          stat_fail
        else
          add_daemon linknx
          stat_done
        fi
        ;;
    stop)
        stat_busy "Stopping linknx"
        [ ! -z "$PID" ] && kill $PID &> /dev/null
        if [ $? -gt 0 ]; then
          stat_fail
        else
          rm_daemon linknx
          stat_done
        fi
        ;;
    *)
        echo "usage: $0 {start|stop|}"
	;;
esac
exit 0
