#!/bin/bash
#
# pwrstatd    This shell script takes care of starting and stopping
#             standalone pwrstatd.
#
# description: PowerPanel for Linux is a software program that monitors 
#              the status of your CyberPower Systems UPS.
# processname: pwrstatd
# config: /etc/pwrstatd.conf
# /etc/rc.d/pwrstatd symbolic link /usr/sbin/pwrstatd

PWRSTATD_BIN=/usr/sbin/pwrstatd
PROG="pwrstatd"
VERSION="1.3.3"

# Source function library.
. /etc/rc.d/functions

# check file whether is executable
[ -x $PWRSTATD_BIN ] || exit 0

# check process whether is exist 
PID=$(pidof -o %PPID $PWRSTATD_BIN)
case "$1" in
  start)
    stat_busy "Starting $PROG $VERSION"
    [ -z "$PID" ] # check string length
    if [ $? -gt 0 ]; then
      stat_fail
    else
      $PWRSTATD_BIN &
      add_daemon $PROG
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping $PROG $VERSION"
    [ ! -z "$PID" ]  && kill -TERM $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon $PROG
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
   stat_busy "Checking $PROG $VERSION"
   ck_status pwrstatd
   ;;
  reload)
   stat_busy "Reloading $PROG $VERSION"
   [ -z "$PID" ] # check string length
   if [ $? -gt 0 ]; then
   	kill -HUP $PID
	stat_done
   else
   	stat_fail
   fi
   ;;
  *)
  echo "usage: $0 {start|stop|status|restart|reload}"
esac
exit 0
