#!/bin/sh
#
# Copyright (c) 2013 NetIQ Corporation and its affiliates. All Rights Reserved. 
# All rights reserved.
#
# chkconfig: - - - 
# description: NDS SNMP Subagent
# processname: ndssnmpsa
# config: /etc/opt/novell/eDirectory/conf/ndssnmp/ndssnmp.cfg
# pidfile: /etc/opt/novell/eDirectory/conf/ndssnmp/ndssnmpsa.pid
#
# Basic support for the LSB(Linux Standard Base) Specification
### BEGIN INIT INFO
# Provides: ndssnmpsa
# Required-Start: snmp ndsd
# Required-Stop: $null
# Default-Start: 2 3 5
# Default-Stop: 0 1 4 6
# Description: Start/Stop Script for NDS SNMP Subagent
### END INIT INFO

# Template configuration variables
localstatedir=/etc/opt/novell/eDirectory/conf/ndssnmp
initdir=/etc/init.d

PATH=$PATH:/usr/local/bin
export PATH
LD_LIBRARY_PATH=/opt/novell/eDirectory/lib64:/opt/novell/lib64:/opt/novell/eDirectory/eDir-exclusive/lib64
export LD_LIBRARY_PATH

#
# Start the ndssnmpsa daemon
#

StartNdssnmpSA()
{
	if [ -f /opt/novell/eDirectory/bin/ndssnmpsa ]
	then
		### if Linux and SNMP_MAJOR_VERSION is not defined, 
		### export the SNMP_MAJOR_VERSION with the major version of net-snmp libraries
		isLinux=`uname | grep -ic Linux`

		if [ $isLinux -ge 1 ];  then
			if test -f /opt/novell/eDirectory/lib/libtrappoll.so.0.0.0 ; then
				buildType=32
			elif test -f /opt/novell/eDirectory/lib64/libtrappoll.so.0.0.0 ; then
				buildType=64
			fi
			if [ "$buildType" = "64" ]; then
					if [ "$SNMP_MAJOR_VERSION" == "" ]; then
						ls /opt/novell/eDirectory/lib64/libnetsnmp.so.* > /dev/null 2>&1
						if [ $? -eq 0 ]; then
							SNMP_MAJOR_VERSION=`ls /opt/novell/eDirectory/lib64/libnetsnmp.so.* 2> /dev/null | head -n 1 | cut -d'.' -f3`
						else
							SNMP_MAJOR_VERSION=`ls /opt/novell/eDirectory/eDir-exclusive/lib64/libnetsnmp.so.* 2> /dev/null | head -n 1 | cut -d'.' -f3`
						fi
						export SNMP_MAJOR_VERSION
					fi
			elif [ "$buildType" = "32" ]; then 
					if [ "$SNMP_MAJOR_VERSION" == "" ]; then
						SNMP_MAJOR_VERSION=`ls /usr/lib/libnetsnmp.so.* 2> /dev/null | head -n 1 | cut -d'.' -f3`
						export SNMP_MAJOR_VERSION
					fi
			fi
		fi

		echo `gettext ndssnmpsa "Starting NDS SNMP Subagent ..."`
		#SNMP shows lots of error messages for missing modules Alm_octane-236366
		DIR="/usr/local/share/snmp/mibs"
		if [ ! -d "$DIR" ]; then
			# Take action if $DIR does not exists. #
			export MIBDIRS=/usr/share/snmp/mibs
		fi
		/opt/novell/eDirectory/bin/ndssnmpsa
		if [ $? -eq 0 ]
		then
			str1=`gettext ndssnmpsa "Please Wait..."`
			str2=`gettext ndssnmpsa "Done"`
			echo "$str1 "
			### sleep require as ndssnmpsa takes 15 to 20sec to update the pidfile
			sleep	15 
			echo "$str2"
		else
			echo `gettext ndssnmpsa "ndssnmpsa startup failed"`
			exit 1
		fi
	else
		echo `gettext ndssnmpsa "/opt/novell/eDirectory/bin/ndssnmpsa does not exist"`
		exit 1
	fi
	exit 0
}	

StopNdssnmpSA()
{
	if [ ! -f $localstatedir/ndssnmpsa.pid ]
	then
		echo `gettext ndssnmpsa "ndssnmpsa is not running"`
		exit 1
	fi
	pid=`cat $localstatedir/ndssnmpsa.pid`
	echo `gettext ndssnmpsa "Stopping NDS SNMP Subagent ..."`

	kill -INT $pid 1>/dev/null 2>&1
	if [ "$?" -ne 0 ]; then
		echo `gettext ndssnmpsa "ndssnmpsa is not running"`
		exit 1
	fi

	rm -f $localstatedir/ndssnmpsa.pid

	echo `gettext ndssnmpsa "Done"`
	exit 0
}


Usage()
{
	str1=`gettext ndssnmpsa "Usage: "`
	str2=`gettext ndssnmpsa " { start | stop } "`
	echo "$str1 $0 $str2"
}

case "$#" in
'1') # Valid number of arguments
	if [ "$1" = "start" ]
	then
		StartNdssnmpSA
	elif [ "$1" = "stop" ]
	then
		StopNdssnmpSA
	else
		Usage
	fi ;;
*) # Invalid number of arguments
	Usage ;;
esac
exit 0
