#!/bin/bash
#
# Copyright (c) 2013 SafeNet, Inc. All rights reserved.
#
# All rights reserved.  This file contains information that is
# proprietary to SafeNet, Inc. and may not be distributed
# or copied without written consent from SafeNet, Inc.
#
# chkconfig: 3 96 14
# description: Luna SNMP subagent
# processname: luna-snmp
#
# TODO: refactor common areas with HTL client script


LUNA_SNMP_NAME="luna-snmp"
# Time period to wait after sending a killall signal to the process
KILLALL_DELAY=10

case `uname -s` in
    HP-UX)
        OS="hpux"
        LUNA_SNMP="/opt/safenet/lunaclient/snmp/$LUNA_SNMP_NAME"
        ;;

    AIX)
        OS="aix"
        LUNA_SNMP="/usr/safenet/lunaclient/snmp/$LUNA_SNMP_NAME"
        ;;

    [sS]unOS)
        OS="solaris"
        LUNA_SNMP="/opt/safenet/lunaclient/snmp/$LUNA_SNMP_NAME"
        ;;

    [lL]inux)
        OS="linux"
        LUNA_SNMP="/usr/safenet/lunaclient/snmp/$LUNA_SNMP_NAME"
        ;;

    *)
        # Should never happen since we went through it in the client install
        OS=""
        ;;

esac


if [ "$OS" = "linux" ] ; then
    REDHAT=""
    DEBIAN=""
    SUSE=""
    if [ -e /etc/redhat-release ] ; then
        REDHAT="yes"
    elif [ -e /etc/debian_version ] ; then
        DEBIAN="yes"
    elif [ -e /etc/SuSE-release ] ; then
        SUSE="yes"
    fi

    if [ "$REDHAT" != "" ] ; then
        LOCKFILE="/var/lock/subsys/$LUNA_SNMP_NAME"
        . /etc/init.d/functions
    elif [ "$DEBIAN" != "" ] ; then
        LOCKFILE="/var/lock/$LUNA_SNMP_NAME"
        . /lib/lsb/init-functions
    elif [ "$SUSE" != "" ] ; then
        LOCKFILE="/var/lock/subsys/$LUNA_SNMP_NAME"
        . /etc/rc.status
    fi
fi


RETVAL=0
PROCCOUNT=0
echoprog="$LUNA_SNMP_NAME"


check_proc()
{
    PROCCOUNT=`ps -ef | grep -v grep | grep -c $LUNA_SNMP`
}


start()
{
    echo -n $"Starting $echoprog:"
    check_proc

    if [ "$PROCCOUNT" -eq 1 ]; then
    echo "OK"
          if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
              touch $LOCKFILE
          fi
    else
        if [ -d "/usr/share/snmp/mibs" ]; then
            MIBDIRS="/usr/share/snmp/mibs"
            export MIBDIRS
        fi
        $LUNA_SNMP -LS0-6d -Lf /dev/null
    sleep 3
        check_proc

        if [ "$PROCCOUNT" -eq 1 ]; then
            echo "OK"
            if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
                touch $LOCKFILE
            fi
        else
            echo "FAILED"
            RETVAL=1
        fi
    fi
}

stop()
{
    echo -n $"Stopping $echoprog:"
    check_proc

    if [ "$PROCCOUNT" -ne 0 ]; then 
        LUNA_SNMP_P=`pgrep -f $LUNA_SNMP`
        if [ "$LUNA_SNMP_P" != "" ] ; then
            # First send a kill signal and wait a bit
			if [ "$OS" = "hpux" ] || [ "$OS" = "solaris" ]; then
				pkill -9 -f $LUNA_SNMP
			else
				killall $LUNA_SNMP
			fi
            sleep $KILLALL_DELAY

            # If it is still running we now force kill it
            LUNA_SNMP_P=`pgrep -f $LUNA_SNMP`
            if [ "$LUNA_SNMP_P" != "" ] ; then
                # try three times just in case
                kill -9 $LUNA_SNMP_P > /dev/null 2>&1
                kill -9 $LUNA_SNMP_P > /dev/null 2>&1
                kill -9 $LUNA_SNMP_P > /dev/null 2>&1
            fi
        fi

        check_proc
        if [ "$PROCCOUNT" -eq 0 ]; then
            echo "OK"
            if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
                rm -f $LOCKFILE
            fi
        else
            echo "FAILED"
            RETVAL=1
        fi

    else
    echo "OK"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        LUNA_SNMP_P=`pgrep -f $LUNA_SNMP`
        if [ "$LUNA_SNMP_P" != "" ] ; then
            echo "$LUNA_SNMP_NAME (pid $LUNA_SNMP_P) is running..."
        else
            echo "$LUNA_SNMP_NAME is stopped"
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
        ;;
esac

exit $RETVAL
