#!/bin/bash
#
# chkconfig: - 60 20
# description: The rusers protocol allows users on a network to identify \
#              who is logged in on other responding machines.
# processname: rpc.rusersd

### BEGIN INIT INFO
# Provides: rpc.rusersd
# Required-Start: $syslog $network $rpcbind
# Required-Stop:  $syslog $network $rpcbind
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start and stop rpc.rusersd
# Description: The rusers protocol allows users on a network to identify \
#              who is logged in on other responding machines.
### END INIT INFO

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

# Get config.
. /etc/sysconfig/network

# pidfile: /var/run/rpc.rusersd.pid
# lockfile: /var/lock/subsys/rpc.rusersd
RETVAL=0
LOCK="/var/lock/subsys/rpc.rusersd"
PIDFILE="/var/run/rpc.rusersd.pid"
RUSERS_RPC_NUM=100002

start() {
    if [ $UID -ne 0 ] ; then
        #user had insufficient privilege
        exit 4
    fi
    # Check that networking is up.
    if [ ${NETWORKING} = "no" ]
    then
	  exit 6 #considered not configured
    fi
	status rpcbind > /dev/null
	RETVAL=$?
	[ $RETVAL -ne 0 ] && /etc/rc.d/init.d/rpcbind start
	echo -n $"Starting rusers services: "
	daemon rpc.rusersd
	RETVAL=$?
	 # Check that rpc service has been registred successfully
        if [ $RETVAL -eq 0 ]
        then
                timeout=3
                SECONDS=0
                while [ $SECONDS -lt $timeout ]; do
                        rpcinfo -u localhost $RUSERS_RPC_NUM > /dev/null 2>&1
                        RETVAL=$?
                        if [ $RETVAL -eq 0 ]; then
                                break;
                        fi
                        sleep 1
                done
                if [ $RETVAL -ne 0 ]; then
                        logger -t rpc.rstatd "rusersd cannot bind to rpcbind."
                        failure
                fi
        else
                failure
        fi
	[ $RETVAL -eq 0 ] && touch $LOCK && touch $PIDFILE
	echo
	return $RETVAL
}

stop() {
    if [ $UID -ne 0 ] ; then
        #user had insufficient privilege
        exit 4
    fi
	echo -n $"Stopping rusers services: "
	killproc rpc.rusersd
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $LOCK rm -f $PIDFILE
	return $RETVAL
}

restart() {
	stop
	start
}	

# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
	status rpc.rusersd
    RETVAL=$?
	;;
  restart|force-reload)
  	restart
	;;
  reload)
    RETVAL=3
    ;;
  condrestart|try-restart)
  	[ -f $LOCK ] && restart || : 
	;;
  usage)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
    RETVAL=0
    ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
	RETVAL=2
	;;
esac

exit $RETVAL
