#!/bin/sh

#
# sample rgpsp start / stop script
#
# on Redhat systems you go to the /etc/rc.d/rcX.d (where X is the
# desired runlevel) and add a symlink to this file called SNNrgpsp
# (NN = number, means the order of execution) to start rpgps
# automatically
#

pidofproc() {
        # 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 } }' $1
}

killproc() {
        notset=0
        # check for second arg to be kill level
        notset=1
        killlevel="-9"

        # Save basename.
        base=`basename $1`

        # Find pid.
        pid=`pidofproc $base`

        # Kill it.
        if [ "$pid" != "" ] ; then
                echo -n "$base "
                # TERM first, then KILL if not dead
                kill -TERM $pid
        fi
}

RGPSP="no"

if [ -x /usr/sbin/linux_rgpsp ] ; then
    RGPSP="/usr/sbin/linux_rgpsp"
fi

case $1 in
    start)
	echo -n "Starting rgpsp on port 24374..."
	$RGPSP
	echo "done"
	;;
    stop)
	echo -n "Stopping rgpsp..."
	killproc linux_rgpsp
	echo "done"
	;;
    restart|force-reload)
	$0 stop
	$0 start
	;;
    status)
	echo "rgpsp: no status info available."
	;;
    *)
	echo "usage: $0 {start|stop|restart|status|force-reload}"
	exit 1
	;;
esac

exit 0


