#! /bin/sh
#
# nut    Script to start and stop Network UPS Tools daemon(s)

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
NAME=nut
DESC="Network UPS Tools"

UPSD_CONF=/etc/nut/upsd.conf
UPSMON_CONF=/etc/nut/upsmon.conf

start_stop_model_daemons () {
  (cat $UPSD_CONF | grep "^UPS") |
  while read directive identifier statepath model args ; do
    # note that the following construction of the pidfile assumes
    # that the user has configured their upsd.conf CORRECTLY!
    pidfile="/var/run/nut/${statepath#/var/lib/nut/}.pid"
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p $pidfile \
            -x /lib/nut/$model -- $args 2>/dev/null >/dev/null
        echo -n " ${statepath#/var/lib/nut/}"
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p $pidfile \
            -n $model 2>/dev/null >/dev/null
        echo -n " ${statepath#/var/lib/nut/}"
        ;;
    esac
  done
}

start_stop_upsd () {
  (cat $UPSD_CONF | grep "^UPS") |
  while read directive identifier statepath model args ; do
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p /var/run/nut/upsd.pid \
            -x /sbin/upsd 2>/dev/null >/dev/null
        echo -n " upsd"
        break # only start upsd once
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p /var/run/nut/upsd.pid \
            -n upsd 2>/dev/null >/dev/null
        echo -n " upsd"
        break # only stop upsd once
        ;;
    esac
  done
}

start_stop_upsmon () {
  (cat $UPSMON_CONF | grep "^MONITOR" ) |
  while read directive system powervalue password master_slave ; do
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p /var/run/nut/upsmon.pid \
            -x /sbin/upsmon 2>/dev/null >/dev/null
        echo -n " upsmon"
        break # only start upsmon once
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p /var/run/nut/upsmon.pid \
            -n upsmon 2>/dev/null >/dev/null
        echo -n " upsmon"
        break # only stop upsmon once
        ;;
    esac
  done
}

word_count() {
    echo $#
}

poweroff() {
  (cat $UPSMON_CONF | grep "^POWERDOWNFLAG") |
  while read directive flagfile ; do
    if [ -f $flagfile ] ; then
      (cat $UPSD_CONF | grep "^UPS") |
      while read directive identifier statepath model args ; do
        case $(word_count $args) in
          0)
            # ok... this is bad
            echo "unable to poweroff $identifier because upsd.conf is incorrect"
            ;;
          1)
            # 'args' contains only one item (the port)
            eval /lib/nut/$model -k $args
            ;;
          *)
            # 'args' contains at least two items; need to extract the port
            the_port=`echo $args | sed -ne 's#^.* \([^ ]*\)$#\1#p'`
            the_args=`echo $args | sed -ne 's#^\(.*\) [^ ]*$#\1#p'`
            eval /lib/nut/$model $the_args -k $the_port
            ;;
        esac
      done
    fi
  done
}


case "$1" in

  start)
    echo -n "Starting $DESC:"
    start_stop_model_daemons start
    start_stop_upsd start
    start_stop_upsmon start
    echo "."
    ;;

  stop)
    echo -n "Stopping $DESC:"
    start_stop_upsmon stop
    start_stop_upsd stop
    start_stop_model_daemons stop
    echo "."
    ;;

  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;

  poweroff)
    echo "Powering off $DESC."
    poweroff
    ;;

  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload|poweroff}" >&2
    exit 1
    ;;

esac

exit 0
