#!/bin/sh
#
# /etc/init.d/pingpanel                  Startup script for the PingPanel process
#
### BEGIN INIT INFO
# Provides:          pingpanel
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PingPanel monitoring of network devices
# Description:       PingPanel is a utility for monitoring network devices
#                    and provides a web-based service, the panel, for
#                                        displaying the captured statuses.
### END INIT INFO

. /lib/lsb/init-functions

DAEMON=/opt/PingPanel/PingPanel.py
CONFIG=/opt/PingPanel/config.cfg
NAME=pingpanel
DESC="PingPanel monitor service"
LOGFILE=/var/log/pingpanel.log
PINGPANEL_OPTS=

set -e

# Check if DAEMON binary exist
[ -f $DAEMON ] || exit 0

[ -f "/etc/default/$NAME" ] && . /etc/default/$NAME

PINGPANEL_OPTS="-c $CONFIG $PINGPANEL_OPTS"

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
	
	start_daemon /usr/bin/python3 -u $DAEMON $PINGPANEL_OPTS 1>>$LOGFILE 2>>$LOGFILE &

    if pgrep -f $DAEMON
    then
      log_success_msg "$DAEMON running."
    else
      log_failure_msg "$DAEMON start FAILED!"
    fi
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"

    kill -9 $(pgrep -f $DAEMON)
    if pgrep -f $DAEMON
    then
      log_end_msg 1
    else
      log_end_msg 0
    fi
    ;;
  restart)
    $0 stop
    sleep 2
    $0 start
    ;;
  status)
    if pgrep -f $DAEMON
	then
      log_end_msg 0
      exit 0
    else
      log_end_msg 1
      exit 1
    fi
    ;;
  *)
    log_action_msg "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
    ;;
esac

exit 0