#!/bin/sh
#
# /etc/init.d/nis	Start NIS (formerly YP) daemons.
#
#

# Customize the variables in /etc/default/nis rather than here
NISSERVER=false
NISMASTER=
YPPWDDIR=/etc
YPCHANGEOK=chsh
YPSERVARGS=""
YPBINDARGS=""
YPPASSWDDARGS=""
YPXFRDARGS=""
YPPWDDIRARGS=""

[ -f /etc/default/nis ] && . /etc/default/nis

NET="/usr/sbin"
test -f ${NET}/ypbind -a -f /etc/defaultdomain || exit 0

#
#	If ypbind broadcasts for the default domain, we may not be bound to 
#	any server yet (note that you can set broadcast in yp.conf for the
#	default domain without ypbind being run with -broadcast)
#
bind_wait()
{
	[ "`ypwhich 2>/dev/null`" = "" ] && sleep 1

	if [ "`ypwhich 2>/dev/null`" = "" ]
	then
		bound=""
		echo -n "[binding to YP server "
		for i in 1 2 3 4 5 6 7 8 9 10
		do
			sleep 1
			echo -n "."
			if [ "`ypwhich 2>/dev/null`" != "" ]
			then
				echo -n " done] "
				bound="yes"
				break
			fi
		done
		[ "$bound" ] || echo -n " backgrounded] "
	fi
}

#
#	Do we want ypbind to be started? On a laptop without ethernet
#	maybe not just yet - /etc/network/if-up.d will take care of it.
#
want_ypbind()
{
	# Do we want to run as a NIS client anyway ?
	case "$NISCLIENT" in
		false|[nN]*)
			return 1
			;;
	esac

	# Executable present ?
	if ! [ -x ${NET}/ypbind ]
	then
		return 1
	fi

	# Started manually?
	if [ "$manual" != "" ]
	then
		return 0
	fi

	#
	#	For now, we do not use the /etc/network/if-{up,down}.d
	#	stuff yet. Not sure if it is useful for NIS or how
	#	it should work, exactly.
	#
	return 0

	#
	#	If the network is not up yet, do not start ypbind.
	#	We assume that /etc/network/ifup.d will start ypbind.
	#	It doesn't matter if it already did.
	#
	network=`route -n | sed -ne '/^224/d' -e '/^127/d' -e '/^[0-9]/p'`
	if [ "$network" = "" ]
	then
		return 1
	fi

	return 0
}

do_start ()
{
	oname=`domainname`
	nname=`cat /etc/defaultdomain`
	if [ "$oname" != "$nname" ]; then
		echo "Setting NIS domainname to: $nname"
		domainname "$nname"
	fi
	echo -n "Starting NIS services: "
	if [ "$NISSERVER" != "false" ]
	then
		echo -n "ypserv "
		start-stop-daemon --start --quiet \
			--pidfile /var/run/ypserv.pid --exec ${NET}/ypserv \
			-- ${YPSERVARGS}
	fi
	if [ "$NISSERVER" = master ]
	then
		E=""
		if [ "$YPCHANGEOK" != "" ]
		then
			OIFS="$IFS"; IFS="$IFS,"
			for i in $YPCHANGEOK
			do
				case "$i" in
					chsh|chfn)
						E="$E -e $i"
						;;
				esac
			done
			IFS="$OIFS"
		fi
		echo -n "yppasswdd "
		if [ "$YPPWDDIR" != "" ]; then
			YPPWDDIRARGS="-D ${YPPWDDIR}"
		fi
		start-stop-daemon --start --quiet \
			--exec ${NET}/rpc.yppasswdd -- $YPPWDDIRARGS $E $YPPASSWDDARGS
		echo -n "ypxfrd "
		start-stop-daemon --start --quiet \
			--exec ${NET}/rpc.ypxfrd -- $YPXFRDARGS
	fi
	if egrep -q '^(ypserver|domain)' /etc/yp.conf
	then
		broadcast=""
	else
		broadcast="-broadcast"
	fi
	if want_ypbind
	then
		echo -n "ypbind "
		start-stop-daemon -b --start --quiet \
			--exec ${NET}/ypbind -- $broadcast ${YPBINDARGS}
		bind_wait
	fi
	if [ "$NISSERVER" = "slave" -a "$NISMASTER" != "" ]
	then
		echo -n "ypinit "
		/usr/lib/yp/ypinit -s $NISMASTER
	fi
	echo
}

do_stop () {
	start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/ypbind.pid
	start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/ypserv.pid
	start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/yppasswdd.pid
	start-stop-daemon --stop --quiet --oknodo \
		--name rpc.ypxfrd
}

# Set 'manual' to indicate if we were started by hand.
case "$0" in
	*/S[0-9][0-9]*|*/K[0-9][0-9]*)
		manual=
		;;
	*)
		manual=1
		;;
esac

case "$1" in
  start)
	do_start
	;;
  stop)
	do_stop
	;;
  reload|force-reload)
	start-stop-daemon --stop --quiet --oknodo --signal 1 \
		--pidfile /var/run/ypserv.pid --exec ${NET}/ypserv
	;;
  restart)
	do_stop
	sleep 2
	do_start
	;;
  *)
	echo "Usage: /etc/init.d/nis {start|stop|reload|force-reload|restart}"
	exit 1
esac

exit 0

