#!/bin/sh
#
# Starts inadyyn
#
RUN_D="/var/run"
NAME="inadyn"
PID_F=$RUN_D/$NAME.pid

inadyn_dir="/usr/bin"
inadyn_conf_dir="/etc"
inadyn_conf_file="$inadyn_conf_dir/$NAME.conf"

getPID(){
    if [ -f $PID_F ]; then
        echo $(cat $PID_F)
    else
        echo ""
    fi
}

start () {
        pid=$(getPID)

        if [ -f "$inadyn_conf_file" ]; then
           if [ -z "$pid" ]; then
              "$inadyn_dir/$NAME" --input_file "$inadyn_conf_file"
              echo "inadyn OK"
           else
              echo "INADYN is already running..."
           fi
        else
            echo "INADYN: no configuration file found..."
        fi
}

stop() {
        pid=$(getPID)
        if [ -n "$pid" ]; then
            echo -n "Stopping inadyn:..."
            ( { kill -SIGINT $pid >/dev/null 2>&1
              } && echo "OK" ) || echo "ERROR"
            rm -f "$PID_F"
        else
            echo "INADYN is not running"
        fi
}

restart() {
        stop
        start
}

[ ! -f "$inadyn_conf_file" ] && exit 0

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
