#!/bin/sh
#
# ypbind:       Starts the upbind Daemon
#
# chkconfig:	345 42 63
#
# 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


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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/ypbind ] && . /etc/sysconfig/ypbind

# Check that networking is up.
if is_no "${NETWORKING}"; then
	msg_Network_Down ypbinf
	exit 1
fi

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/ypbind ]; then
		show "Binding to the NIS domain"

		daemon ypbind
		# 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.
		pid=`pidofproc ypbind`
		if [ -n "$pid" ]; then
			show "Listening for a NIS domain server"
			times=0
			until ypwhich >/dev/null 2>&1 || [ "$times" = "10" ]
			do
				echo -n "." ;
				sleep 1
				times=$(expr $times + 1)
			done
			ypwhich
			if [ $? -ne 0 ] ; then
				fail
			fi
		fi
	else
		msg_Already_Running ypbind
		exit 1
	fi
	touch /var/lock/subsys/ypbind
	;;
  stop)
	msg_stopping "NIS client"
	killproc ypbind
	rm -f /var/lock/subsys/ypbind
	# if  we used brute force (like kill -9) we don't want those around
	rm -f /var/yp/binding/$(domainname)*
	;;
  status)
	status ypbind
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
	msg_Usage "$0 {start|stop|status|restart|reload}"
	exit 1
esac

exit 0

