#!/bin/bash
#
# ypbind:       Starts the ypbind Daemon
#
# Version:      @(#) /etc/init.d/ypbind.init 1.3
#
# chkconfig: - 27 73
# description: This is a daemon which runs on NIS/YP clients and binds them \
#              to a NIS domain. It must be running for systems based on glibc \
#              to work as NIS clients, but it should not be enabled on systems \
#              which are not using NIS.
# processname: ypbind
# config: /etc/yp.conf

OTHER_YPBIND_OPTS=""

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

. /etc/sysconfig/network

# Check that networking is configured.
[ ${NETWORKING} = "no" ] && exit 0

start() {
	DOMAINNAME=`domainname`
	if [ "$DOMAINNAME" = "(none)" -o "$DOMAINNAME" = "" ]; then
	   if [ -n "$NISDOMAIN" ]; then
		action $"Setting NIS domain name $NISDOMAIN: " domainname $NISDOMAIN
	   else
	        exit 1
	   fi
	fi
	echo -n $"Binding to the NIS domain: "
	daemon ypbind $OTHER_YPBIND_OPTS
	RETVAL=$?
	echo
	if [ $RETVAL -ne 0 ]; then
	    return $RETVAL
	fi
	# the following fixes problems with the init scripts continuing
	# even when we are really not bound yet to a server, and then things
	# that need NIS fail.
	echo -n $"Listening for an NIS domain server."
	for (( times = 1; times < 20; times++ )); do
	    /usr/sbin/rpcinfo -p | grep ypbind > /dev/null 2>&1 && ypwhich > /dev/null 2>&1
	    RETVAL=$?
	    if [ $RETVAL -eq 0 ]; then
		break;
	    fi
	    sleep 1
	    echo -n "."
	done
	if [ $RETVAL -eq 0 ]; then
	    initlog -n ypbind -s "bound to NIS server `ypwhich 2> /dev/null`"
	    touch /var/lock/subsys/ypbind
	else
	    killproc ypbind
	    # if  we used brute force (like kill -9) we don't want those around
	    if [ x$(domainname) != x ] ; then
		rm -f /var/yp/binding/$(domainname)*
	    fi
	    failure "attempting to contact yp server"
	fi
	echo
	return $RETVAL
}

stop() {
	echo -n $"Shutting down NIS services: "
	killproc ypbind
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
	  rm -f /var/lock/subsys/ypbind
	  # if  we used brute force (like kill -9) we don't want those around
	  if [ x$(domainname) != x ] ; then
	    rm -f /var/yp/binding/$(domainname)*
	  fi
	fi
	echo
	return $RETVAL
}

restart() {
	stop
	start
}

RETVAL=0

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status ypbind
	;;
  restart|reload)
	restart
	;;
  condrestart)
        [ -f /var/lock/subsys/ypbind ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
esac

exit $?