#!/bin/sh

prefix=/usr
exec_prefix=/usr

# First set up a default search path.
PATH="/usr/sbin:$PATH"
export PATH

# set up OpenSOAP log directory
OPENSOAP_DIR=${exec_prefix}
OPENSOAP_SYSCONFDIR=/etc/opensoap
OPENSOAP_LOCALSTATEDIR=/var/opensoap

export OPENSOAP_DIR

LOG_DIR=${OPENSOAP_LOCALSTATEDIR}/log
export LOG_DIR;

PID_DIR=${OPENSOAP_LOCALSTATEDIR}/run
export PID_DIR;

DATA_DIR=${OPENSOAP_LOCALSTATEDIR}/spool
export DATA_DIR;

TABLE_DIR=${OPENSOAP_LOCALSTATEDIR}/spool
export TABLE_DIR;

CONF_DIR=${OPENSOAP_SYSCONFDIR}
export CONF_DIR;

SOCKET_DIR=${OPENSOAP_LOCALSTATEDIR}/run
export SOCKET_DIR;

# Bash is not POSIX compliant without the following
test -n "$BASH_VERSION" && set -o posix

# iterpret escape sequence
test -n "$BASH_VERSION" && shopt -s xpg_echo

# A function to start a program.
daemon() {
	# Test syntax.
	case $1 in
	    '')    echo '$0: Usage: daemon [+/-nicelevel] {program}'
	           return 1;;
	    -*|+*) nicelevel=$1
	           shift;;
	     *)    nicelevel=0;;
	esac

        # Save basename.
        base=`basename $1`

        # See if it's already running.
	pid=`pidofproc $base`
	[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && return

	# echo basename of the program.
	echo "$base "

	# make sure it doesn't core dump anywhere; while this could mask
	# problems with the daemon, it also closes some security problems
	#ulimit -c 0

	# And start it up.
	nice -n $nicelevel "$@"
}

# A function to start a program as a backgrounded process.  Not sure if
# this is a stellar idea, so we're going to put it in as a function 
# and let the RPM creator decide.  

daemon_bg() {
        # Test syntax.
        case $1 in
            '')    echo '$0: Usage: daemon [+/-nicelevel] {program}'
                   return 1;;
            -*|+*) nicelevel=$1
                   shift;;
             *)    nicelevel=0;;
        esac

        # Save basename.
        base=`basename $1`

        # See if it's already running.
        pid=`pidofproc $base`
        [ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && return

        # echo basename of the program.
        echo  "$base "

        # make sure it doesn't core dump anywhere; while this could mask
        # problems with the daemon, it also closes some security problems
        #ulimit -c 0

        # And start it up.
        nice -n $nicelevel "$@" &
}                                              

# A function to stop a program.
killproc() {
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi
	
        # Find pid.
	pidfile="${PID_DIR}/$1.pid"
	if [ -f "$pidfile" ]; then
	    pid=`head -1 $pidfile`
	else
	    echo ": already stopped\c"
	    pid=""
	fi
	
        # Kill it.
        if [ "$pid" != "" ] ; then
		if [ "$notset" = 1 ] ; then
			# TERM first, then KILL if not dead
			kill -TERM $pid >/dev/null 2>/dev/null
			echo ": ok\c"
			#usleep 100000
		# use specified level only
		else
	                kill $killlevel $pid >/dev/null 2>/dev/null
		fi
	fi

        # Remove pid file if any.
        rm -f $pidfile
}


# A function to find the pid of a program.
pidofproc() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: pidofproc {program}"
		return 1
	fi

	# First try "${PID_DIR}/*.pid" files
	if [ -f ${PID_DIR}/$1.pid ] ; then
	        pid=`head -1 ${PID_DIR}/$1.pid`
	        if [ "$pid" != "" ] ; then
	                echo $pid
	                return 0
	        fi
	fi

	# Next try "pidof"
	pid=`pidof $1`
	if [ "$pid" != "" ] ; then
	        echo $pid
	        return 0
	fi

	# Finally try to extract it from ps
	ps auxw | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
			   { if ((prog == $11) || (("(" prog ")") == $11) ||
			   ((prog ":") == $11)) { print $2 ; exit 0 } }' $1
}
status() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: status {program}"
		return 1
	fi

	# First try "pidof"
	pid=`pidof $1`
	if [ "$pid" != "" ] ; then
	        echo "$1 (pid $pid) is running..."
	        return 0
        else
                pid=`ps auxww Op | awk '{ if ($12 ~ /^[^[]'"$1"'/) print $2}'`
                if [ "$pid" != "" ] ; then
                        echo "$1 (pid $pid) is running..."
                        return 0
                fi
	fi

	# Next try "${PID_DIR}/*.pid" files
	if [ -f ${PID_DIR}/$1.pid ] ; then
	        pid=`head -1 ${PID_DIR}/$1.pid`
	        if [ "$pid" != "" ] ; then
	                echo "$1 dead but pid file exists"
	                return 1
	        fi
	fi
	# See if /var/lock/subsys/$1 exists
	if [ -f /var/lock/subsys/$1 ]; then
		echo "$1 dead but subsys locked"
		return 2
	fi
	echo "$1 is stopped"
	return 3
}

# Run some action. Log its output.
action() {
  STRING=$1
  echo "$STRING "
  shift
  $*
  rc=$?
  echo
  return $rc
}

# check directories for OpenSOAP server
dircheck() {
    mkdir -p -m 777 ${DATA_DIR}/
    mkdir -p ${TABLE_DIR}
    mkdir -p ${SOCKET_DIR}
    mkdir -p ${CONF_DIR}/ssml/
    mkdir -p ${LOG_DIR}
    mkdir -p ${PID_DIR}
}

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

for ARG in $@ $ARGS
do
    case $ARG in
    start)
	# check directories for OpenSOAP server
	echo "=== Check Required Directories ==="
	echo "check directories for OpenSOAP server"
	dircheck;
	#
	# delete old SOAPMessage
	#
	echo "=== Delete Old Files ==="
	echo "delete old SOAPMessage"
	rm -f ${DATA_DIR}/*
	
	echo "delete old queue & spool files"
	#rm -f ${TABLE_DIR}/req_queue_table 
	#rm -f ${TABLE_DIR}/fwd_queue_table 
	#rm -f ${TABLE_DIR}/res_spool_table
	#rm -f ${TABLE_DIR}/ttl_table
	
	#----------------------------------------------------------
	# exec OpenSOAP Server Process
	#----------------------------------------------------------
	echo "=== Start OpenSOAP Server ==="
	# exec ServerConfigManager
	echo "start Server Config Attributes Manager"
	srvConfAttrMgr \
	    >${LOG_DIR}/srvConfAttrMgr 2>&1 &
	sleep 1;

	# exec SSMLAttributesManager
	echo "start SSML Attribute Manager"
	ssmlAttrMgr \
	    >${LOG_DIR}/ssmlAttrMgr 2>&1 &
	sleep 1;
	
	# exec SOAPMessageStoreFile ID Manager
	echo "start Stored File ID Magager"
	idManager \
	    >${LOG_DIR}/idManager 2>&1 &
	sleep 1;
	
	# exec MsgDriverCreator
	echo "start Message Driver Creator"
	msgDrvCreator \
	    >${LOG_DIR}/msgDrvCreator 2>&1 &
	sleep 1;
	
	#server/QueueMgr/src/
	echo "start Request Queue Manager"
	queueManager \
	    >${LOG_DIR}/queueManager_req 2>&1 &
	
	echo "start Forward Queue Manager"
	queueManager_fwd \
	    >${LOG_DIR}/queueManager_fwd 2>&1 &
	
	#server/SpoolMgr/src/
	echo "start Response Spool Manager"
	spoolManager \
	    >${LOG_DIR}/spoolManager 2>&1 &
	
	#server/TTLMgr/src
	echo "start TTL Manager"
	ttlManager \
	    >${LOG_DIR}/ttlManager 2>&1 &
        ;;
    stop)
	#----------------------------------------------------------
	# stop OpenSOAP Server Process
	#----------------------------------------------------------
	
	echo "=== Stop OpenSOAP Server ==="
	
	# stop Server Config Attributes Manager
	echo "stop Server Config Attributes Manager\t\c"
	killproc srvConfAttrMgr
	echo 
	
	# stop SSMLAttributesManager
	echo "stop SSML Attributes Manager\t\t\c"
	killproc ssmlAttrMgr
	echo 
	
	# stop SOAPMessageStoreFile ID Manager
	echo "stop Stored File ID Manager\t\t\c"
	killproc idManager
	echo 
	
	# stop MsgDriverCreator
	echo "stop Message Driver Creator\t\t\c"
	killproc msgDrvCreator
	echo 
	
	# stop Request Queue Manager
	echo "stop Request Queue Manager\t\t\c"
	killproc queueManager_req
	echo

	# stop Forward Queue Manager
	echo "stop Forward Queue Manager\t\t\c"
	killproc queueManager_fwd
	echo
	
	#SpoolMgr
	echo "stop Response Spool Manager\t\t\c"
	killproc spoolManager
	echo
	
	#TTLMgr
	echo "stop TTL Manager\t\t\t\c"
	killproc ttlManager
	echo

	#
	# delete old SOAPMessage
	#
	echo "=== Delete Old Files ==="
	echo "delete old SOAPMessage"
	rm -f ${DATA_DIR}/SOAPMessage/*
	
	echo "delete old queue & spool files"
	rm -f ${TABLE_DIR}/req_queue_table
	rm -f ${TABLE_DIR}/fwd_queue_table 
	rm -f ${TABLE_DIR}/res_spool_table
	rm -f ${TABLE_DIR}/ttl_table
	
        ;;
    restart)
        # restart OpenSOAP Server
        $0 stop
        $0 start
        ;;
    reload)
	#------------------------------------------------------------------
	# stop OpenSOAP Server Config Manager & Attributes Manager Process
	#------------------------------------------------------------------
	# stop Server Config Attributes Manager
	echo "stop Server Config Attributes Manager\t\c"
	killproc srvConfAttrMgr
	echo 
	
	# stop SSMLAttributesManager
	echo "stop SSML Attributes Manager\t\t\c"
	killproc ssmlAttrMgr
	echo 
	
	#---------------------------------------------------------------------
	# exec OpenSOAP Server Config Manager & SSML Attibute Manager Process
	#---------------------------------------------------------------------
		
	# exec ServerConfigManager
	echo "start srvConfAttrMgr"
	srvConfAttrMgr \
	    >${LOG_DIR}/srvConfAttrMgr 2>&1 &
	
	# exec SSMLAttributesManager
	echo "start ssmlAttrMgr"
	ssmlAttrMgr \
	    >${LOG_DIR}/ssmlAttrMgr 2>&1 &
        ;;
    *)
        echo "usage: $0 (start|stop|restart|reload|help)"
        cat <<EOF
start      - start  OpenSOAP server
stop       - stop   OpenSOAP server
restart    - restart OpenSOAP server
reload     - reload OpenSOAP server configuration files
help       - this screen

EOF
        ERROR=2
    ;;
    
    esac

done

exit $ERROR
