#!/usr/local/bin/bash

# Copyright C 2002-2011 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.


ERROR=0
RUNNING=0
DEBUG=1

ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

SNMPD="/usr/local/sbin/snmpd"

MCP="/etc/mcp"
RECONFIG="$MCP/scripts/snmpd.py"
PIDLIST=`pgrep snmpd`

for ARG in $@ $ARGS
do

    if [ "x$PIDLIST" != "x" ]; then
        STATUS="snmpd (pids $PIDLIST) running"
        RUNNING=1
    else
        STATUS="snmpd not running"
        RUNNING=0
    fi

    case $ARG in
    start)
        if [ $RUNNING -eq 1 ]; then
            echo "$0 $ARG: snmpd (pids $PIDLIST) already running"
            continue
        fi


        $SNMPD
        sleep 1

        PIDLIST=`pgrep snmpd`
        

        if [ "x$PIDLIST" != "x" ]; then
            echo "snmpd (pids $PIDLIST) running"
            RUNNING=1
        else
            echo "snmpd failed to start"
            RUNNING=0
            ERROR=1
        fi
        ;;


    stop)
        if [ $RUNNING -eq 0 ]; then
            echo "snmpd not running"
            continue
        fi

        if killall snmpd ; then
            echo "$0 $ARG: snmpd stopped"
        else
            echo "$0 $ARG: snmpd could not be stopped"
            ERROR=4
        fi
        sleep 3
        ;;

    restart)
        if [ $RUNNING -eq 0 ]; then
            echo "snmpd not running"
            continue
        fi

        if killall -HUP snmpd ; then
            echo "$0 $ARG: snmpd restarted"
        else
            echo "$0 $ARG: snmpd could not be restarted"
            ERROR=4
        fi
        sleep 3
        ;;
        
    stopwait)
            for I in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
            do
              PIDLIST=`pgrep snmpd`
              if [ "x$PIDLIST" != "x" ]; then
                  sleep 1
                  continue
              else
                  break
              fi
            done
            exit 0
            ;;

    isiconfigure)

        $RECONFIG
        
        echo $?

        echo "snmpd.conf configured"
        ;;

    *)
        echo "usage: $0 (start|stop|help)"
        cat <<EOF

start      - start snmpd
stop       - stop snmpd
restart
help       - this screen

EOF
        ERROR=2
        ;;
    esac
done

exit $ERROR
