#!/bin/bash
#
# NINO init file for services
#
# /etc/init.d/nino
#
### BEGIN RedHat INFO
# chkconfig: 35 99 01
# description: NINO services
# processname: /var/nino/mod_perl/services.pl
# config: /var/nino/conf/snmp.ini
### END RedHat INFO
#
### BEGIN INIT INFO
# Provides:       nino
# Required-Start: $remote_fs $syslog $time
# X-UnitedLinux-Should-Start: sendmail postfix
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    NINO services
### END INIT INFO
#
# System startup script for the NINO services daemon
# NINO services will control trapd and eventaction
#
### Setup vars:

BASEDIR="/var/nino"
RETVAL=0
PROG="${BASEDIR}/mod_perl/services.pl"
PARAM=$1

### Functions


unixstart() {
        gprintf "NINO services start"
        perl $PROG start > /dev/null 2>&1
}

unixstop() {
        perl $PROG stop

        PID=`ps -ef | grep $PROG | grep -v grep | awk '{print $2}' `
        if [ "$PID" != "" ] ; then
          kill $PID
        fi
}

susestart() {
        perl $PROG start

        # Remember status and be verbose
        rc_status -v
}

susestop() {
        perl $PROG stop

        PID=`ps -ef | grep mod_perl/services | grep -v grep | awk '{print $2}' `
	if [ "$PID" != "" ] ; then
          kill $PID
	fi

        # Remember status and be verbose
        rc_status -v

}

rhstart() {
        gprintf "NINO services start"
        daemon $PROG start
	RETVAL=$?
	echo
        touch /var/lock/subsys/nino
	return $RETVAL
}

rhstop() {
        perl $PROG stop
	killproc $PROG
	RETVAL=$?
	echo
	rm -f /var/lock/subsys/nino
	return $RETVAL
}

reload(){
	rhstop
	rhstart
}

restart(){
	rhstop
	rhstart
}


rhcontrol(){

  case "$PARAM" in
    start)
	rhstart
	;;
    stop)
	rhstop
	;;
    restart)
	restart
        ;;
    reload)
	reload
        ;;
    status)
        status services.pl
	RETVAL=$?
        ;;
    *)
	gprintf "Usage: %s {start|stop|status|restart|reload}\n" "$0"
	RETVAL=1
        ;;
  esac
  exit $RETVAL
}

susecontrol(){
  rc_reset

  case "$PARAM" in
    start)
	susestart
        ;;
    stop)
	susestop
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    *)
        gprintf "Usage: %s {start|stop|restart}\n" "$0"
        exit 1
        ;;
  esac
  rc_exit
}

unixcontrol(){

  case "$PARAM" in
    start)
        unixstart
        ;;
    stop)
        unixstop
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start

        ;;
    *)
        gprintf "Usage: %s {start|stop|restart}\n" "$0"
        exit 1
        ;;
  esac
  exit
}

### Source SuSE config

if [ -e /etc/rc.status ] ; then
. /etc/rc.status
  susecontrol
  exit
fi

### Source fedora/redhat function library

if [ -e /etc/init.d/functions ] ; then
. /etc/init.d/functions
  rhcontrol
  exit
fi

### Unix

  unixcontrol

### End
