#!/bin/sh

### BEGIN INIT INFO
# Provides:        onlp-snmpd
# Required-Start:  $snmpd $syslog
# Required-Stop:   $snmpd $syslog
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start ONLP SNMP Agent
# Description:        NET SNMP AGENTX
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

DAEMON=/usr/bin/onlp-snmpd
PIDFILE=/var/run/onlp-snmpd.pid
ONLP_SNMPD_OPTS="-dr -pid $PIDFILE"
QUIET=

test -x $DAEMON || exit 5

RUNASUSER=root
UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true

case $1 in
	start)
		log_daemon_msg "Starting ONLP SNMP Agent" "onlp-snmpd"
		if [ -z "$UGID" ]; then
			log_failure_msg "user \"$RUNASUSER\" does not exist"
			exit 1
		fi
  		start-stop-daemon --start $QUIET --oknodo --pidfile $PIDFILE --startas $DAEMON -- $ONLP_SNMPD_OPTS $ONLP_SNMPD_EXTRA_OPTS
		status=$?
		log_end_msg $status
  		;;
	stop)
		log_daemon_msg "Stopping ONLP SNMP Agent" "onlp-snmpd"
  		start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE
		log_end_msg $?
		rm -f $PIDFILE
  		;;
	restart|force-reload)
		$0 stop && sleep 2 && $0 start
  		;;
	try-restart)
		if $0 status >/dev/null; then
			$0 restart
		else
			exit 0
		fi
		;;
	reload)
                log_daemon_msg "Reloading ONLP SNMP Agent" "onlp-snmpd"
                start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE --signal 1
		status=$?
	        log_end_msg $status
		;;
        pause)
                log_daemon_msg "Pausing ONLP SNMP Agent" "onlp-snmpd"
                start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE --signal 19
		status=$?
	        log_end_msg $status
		;;
        continue)
                log_daemon_msg "Continuing ONLP SNMP Agent" "onlp-snmpd"
                start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE --signal 18
		status=$?
	        log_end_msg $status
		;;
	status)
		status_of_proc $DAEMON "ONLP SNMP Agent"
		;;
	*)
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|reload|status}"
		exit 2
		;;
esac
