#!/bin/sh

# folder definitions  
basedir=/opt/Motorola/mcd5000/omcserver
bindir=/opt/Motorola/mcd5000/omcserver/bin

# Source function library.
. /etc/rc.d/init.d/functions

mode=$1    # start or stop


case "$mode" in
  'start')
    # Start daemon
     for pid in `ps -ef | grep $bindir`; do
     	if test -d /proc/$pid
        then
        	echo "ssrnomcd (pid $pid) allready running..."
                exit
        fi
     done
 
     if test -x $bindir/omcserver
	then
	    action $"Starting ssrnomcd :" /bin/true 
	    ulimit -n 20000
      	    sudo -u ssrn $bindir/omcserver 
    else
            action $"Starting ssrnomcd :" /bin/false
    fi
    ;;

  'stop')
	# Stop Daemon
        action $"Stopping ssrnomcd :" /bin/true
	for pid in `ps -ef | grep $bindir`; do
	    if test -d /proc/$pid
		then    #pid found, exiting...
		kill -9 $pid
		sleep 1
		break
	    fi
	done
	if test -d /proc/$pid #if no pid :-(
	    then
            action $"Stopping ssrnomcd :" /bin/false
	fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    $0 stop
    $0 start
		;;
		
  'status')
    # Print ssrnomcd status
        for pid in `ps -ef | grep $bindir`; do
            if test -d /proc/$pid
							then
                echo "ssrnomcd (pid $pid) is running..."
                break
            fi
        done
        if test -s $pid #if no pid :-(
            then
            echo "ssrnomcd is stopped"
        fi
    ;;
    

  *)
    # usage
    echo "Usage: $0 start|stop|restart|status"
    exit 1
    ;;
esac
