#!/bin/bash
#
# Init file for DELL SOL Telnet Proxy daemon
#
# chkconfig: 2345 56 26
# description: DELL SOL Telnet Proxy daemon
#
# processname: solproxyd
# config: /etc/solproxy.cfg
# pidfile: 
### BEGIN INIT INFO
# Provides: solproxyd
# Required-Start: network
# Required-Stop:  network
# Default-Start:  3 5
# Default-Stop:
# Description:    Start the OSA solproxy daemon
### END INIT INFO

osmchkproc()
{
        # for length of its name > 15 Bytes, checkproc will use its basename
        # in /proc/<pid>/stat to check if it is running when user is not root.
        # so we change checkproc parameter.
        if [ "`whoami`" != "root" ]; then
                bsname=`basename $1`
                prg=`echo $bsname | cut -b -15`
                /sbin/checkproc $prg
        else
                /sbin/checkproc $1
        fi
        return $?
}

SOLPROXY=/usr/sbin/dsm_bmu_solproxy32d
[ ! -x $SOLPROXY ] && exit 5

# Source SuSE config files if exist
[ -a /etc/rc.config ] && . /etc/rc.config
[ -a /etc/rc.status ] && . /etc/rc.status

# pull in sysconfig settings
# [ -f /etc/sysconfig/solproxy ] && . /etc/sysconfig/solproxy

#VERSION=2.0.9.3

SOLCFG=/etc/solproxy.cfg
prog=`basename $SOLPROXY`
LOGDEV=/dev/null

# proc utils
CHKPROC=osmchkproc
STPROC="/sbin/startproc -s"
KLPROC=/sbin/killproc

# 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.

# first echo Copyright
#echo
#echo "OSA SOLProxy Version `rpm -q osa_solproxy | awk -F- '{print $2 "." $3}'`"
#echo "OSA SOLProxy Version $VERSION"
#echo "Copyright 2003, OSA Technologies Corp. (www.osatechnologies.com)"
#echo "All rights reserved"
#echo 

case "$1" in
    start)		
	echo -n "Starting OSA BMU solproxy daemon"
	tms=1
        while [ $tms -lt 100 ]
        do
                $CHKPROC $SOLPROXY
                if [ $? -ne 0 ]; then
                        $STPROC -f $SOLPROXY -cfg $SOLCFG -daemon -b >>$LOGDEV 2>&1
                        sleep 1
                else
                        break
                fi
                tms=`expr $tms + 1`
        done
        $CHKPROC $SOLPROXY || echo "Can't start $SOLPROXY, please report to OSA."
        #[ $? -ne 0 ] && echo "Can't start $SOLPROXY, please report to OSA."
        # Rmember status and be verbose
        rc_status -v
        ;;
    stop)
        # need root privilige to stop these processes.
        if [ "`whoami`" != "root" ]; then
                echo "You need root privilege to stop these services."
                exit 1
        fi
	
	echo -n "Shutting down OSA BMU solproxy daemon"
        $CHKPROC $SOLPROXY
        if [ $? -eq 0 ]; then
                $KLPROC -TERM $SOLPROXY
                sleep 1
        fi

        # Rmember status and be verbose
        rc_status -v
	;;
    restart|reload)
	echo "Restarting service $SOLPROXY"
	$0 stop
	sleep 1
	$0 start

        # Rmember status and be verbose
        rc_status 
	;;
    status)
        # NOTE: checkproc returns LSB compliant status values.
        $CHKPROC $SOLPROXY
        if [ $? -ne 0 ]; then
                echo -n "$SOLPROXY: "
                echo $rc_unused
        else
                echo -n "$SOLPROXY: "
                echo $rc_running
        fi
	;;
    *)
	echo $"Usage: $0 {start|stop|restart|reload|status}"
	;;
esac
exit 0
