#!/sbin/sh
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)inetsvc	1.1	08/05/06 SMI"
#

if [ -c /dev/rawip ]; then
        . /etc/init.d/inetsvc.pre_p2v $1
	exit $?
fi

case "$1" in
'start')
	;; # Fall through -- rest of script is the initialization code

'stop')
	/usr/bin/pkill -x -u 0 'in.named|inetd'
	exit 0
	;;

*)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac

if [ -z "$_INIT_PREV_LEVEL" ]; then
	set -- `/usr/bin/who -r`
	_INIT_PREV_LEVEL="$9"
fi

#
# wait_nis
# Wait up to 5 seconds for ypbind to obtain a binding.
#
wait_nis ()
{
	for i in 1 2 3 4 5; do
		server=`/usr/bin/ypwhich 2>/dev/null`
		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
	done
	return 1
}

domain=`/usr/bin/domainname 2>/dev/null`

[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
    echo "WARNING: Timed out waiting for NIS to come up" >& 2

#
# If this machine is configured to be an Internet Domain Name System (DNS)
# server, run the name daemon.
#
if [ -f /usr/sbin/in.named -a -f /etc/named.conf ]; then
	echo 'starting internet domain name server.'
	/usr/sbin/in.named &
fi

#
# Run inetd in "standalone" mode (-s flag) so that it doesn't have
# to submit to the will of SAF.  Why did we ever let them change inetd?
#
/usr/sbin/inetd -s &
