#! /bin/sh
# Copyright (c) 2005 SuSE Linux AG
#
# Modified for SMS : Rashmi Vittal <vrashmi@novell.com> 
#
#/etc/init.d/smdrd
#
# NO and symbolic its links
# NO /usr/sbin/rcnovell-smdrd
#
# 
# System startup script for the smdr daemon
#
### BEGIN INIT INFO
# Provides: smdr
# Required-Start: $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# X-UnitedLinux-Should-Start: nss novell-ncs
# X-UnitedLinux-Should-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start Novell Backup Services (SMS) 
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance

SMDRD_BIN=/opt/novell/sms/bin/smdrd
test -x $SMDRD_BIN || { echo "$SMDRD_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active        checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

load_smszapi()
{

	# loading the zapi kernel module
        # and create a major device number for it
	lsmod | grep smszapi > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		uname -r | grep default > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
		MODULE=default
		fi
		uname -r | grep smp > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
		MODULE=smp
		fi
		uname -r | grep bigsmp > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
		MODULE=bigsmp
		fi
	
		MODULE_EXISTS=0	
		test -f /lib/modules/`uname -r`/nss/smszapi.ko
		if [ $? -eq 0 ]
		then
			insmod /lib/modules/`uname -r`/nss/smszapi.ko > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				MODULE_EXISTS=1
			fi
		else
			insmod /opt/novell/sms/modules/smszapi-$MODULE.ko > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				MODULE_EXISTS=1
			fi
		fi
  	      	if [ $MODULE_EXISTS -eq 1 ]
        	then
                	major=$(cat /proc/devices | awk "\$2==\"smszapi\" {print \$1}")
			test -f /dev/smszapi
			if [ $? -eq 0 ]
			then
				rm -f /dev/smszapi
			fi
             	   	mknod --mode=600 /dev/smszapi c $major 0  > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				return 0
			else
				return 1
			fi
  	      	else
			return 1
        	fi
	else
		return 0
	fi
}

unload_smszapi()
{

        lsmod | grep smszapi >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
		rmmod smszapi  > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			rm -f /dev/smszapi  > /dev/null 2>&1
			return 0
		else
			return 1
		fi
	fi
}

start_smdrd()
{

	## Start daemon with startproc(8). If this fails
	## the echo return value is set appropriate.

	# NOTE: startproc return 0, even if service is 
	# already running to match LSB spec.
	


	# checking for the pid file. Remove the pid file 
	# if entry in the file does not correspond to running
	# process 

	if [ -f /var/opt/novell/run/smdrd.pid ]
	then
		PIDFILE_CONTENT=`cat /var/opt/novell/run/smdrd.pid | xargs ps --no-headers -o "%c" -p`
		if [ "$PIDFILE_CONTENT" != "smdrd" ] #The process does not exist
		then
			`\rm /var/opt/novell/run/smdrd.pid`
		fi
	fi
	startproc $SMDRD_BIN 
	if [ $? -eq 0 ]
	then
		return 0
	else
		return 1
	fi
}

stop_smdrd()
{
	## Stop daemon with killproc(8) and if this fails
	## set echo the echo return value.

	# Send SIGINT to smdrd for graceful close of connections
	SMDRD_RUNNING=`pgrep smdrd | wc -l`
	if [ $SMDRD_RUNNING -ne 0 ]
	then
		#Adding a while loop to kill all instances of smdrd (TCP and UDS)
		while [ $SMDRD_RUNNING -ge 0 ]
		do
			killproc -TERM $SMDRD_BIN
			SMDRD_RUNNING=`expr $SMDRD_RUNNING - 1`
		done
	fi
	return 0
			 
}
	

case "$1" in
    start)

	# load zapi driver only if nss is already loaded

	lsmod | grep nss >/dev/null 2>&1
	if [ $? -eq 0 ]
	then 
		echo -n "Loading backup kernel driver for NSS"
		#load the NSS driver for backup
		load_smszapi
		rc_status -v
	fi
	
	#start the smdrd daemon
	echo -n "Starting smdrd daemon"
	start_smdrd
	rc_status -v

	;;
    stop)


	echo -n "Shutting down smdr daemon"
	#stop the smdrd daemon
	stop_smdrd
	# Remember status and be verbose
	rc_status -v

	# unload zapi driver only if is already loaded	
	lsmod | grep smszapi >/dev/null 2>&1
	if [ $? -eq 0 ]
	then 
		echo -n "Unloading the NSS driver for backup"
		#unload the NSS driver for backup
		unload_smszapi
		rc_status -v
	fi
	;;
    try-restart | condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.

        if test "$1" = "condrestart"; then
              echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
                 $0 restart
        else
              rc_reset        # Not running is not a failure.
        fi

	# Remember status and be quiet
	rc_status
	;;
    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
	;;
    force-reload)
	## Signal the daemon to reload its config. Most daemons
	## do this on signal 1 (SIGHUP).
	## If it does not support it, restart.
	
	$0 stop; sleep 1  &&  $0 start
	rc_status -v
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signalling, do nothing (!)

	# If it supports signalling:
	echo -n "Reload service smdr"
	killproc -HUP $SMDRD_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for smdr daemon: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $SMDRD_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
