#
# SCCS_ID - "@(#)setup_named        2.10      11:54:58 - 89/06/26 "
#
# @(#) Copyright 1989. The Wollongong Group, Inc.  All Rights Reserved.
PATH=$PATH:.;export PATH

#
# Install name server support
# Only handle resolver and caching, master needs admin support
#

UNAME=$1
NDBOOT=/etc/named.boot
NDHOSTS=/usr/local/domain/named.hosts
NDREV=/usr/local/domain/named.rev
NDLOCAL=/usr/local/domain/named.local
RESOLVER=/etc/resolv.conf
RCFILE=/etc/rc2.d/S86wintcp

# Prompt for yes or no answer - returns non-zero for no
askyn() {
	while	echo "$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		     [yY]) return 0 		                  ;;
	             [nN]) return 1		                  ;;
		        *) echo "\nPlease answer y, n, or q" >&2  ;;
		esac
	done
}

# input - String to display
# output - value in "$ansvalue"
askipvalue() {
	isitok=
	while [ "$isitok" = ""  ]
	do
		echo "Please enter $2 > \c"
		read ansvalue
		if [ "$ansvalue" = "" ]
		then
			if [ "$1" = "must" ]
			then
			    echo "\tYou must give an answer to the question"
			else
			    isitok="ok"
			fi
		else
			ansvalue=`inet_addr $ansvalue`
			code=$?
			if [ $code != 0 ]
			then
			    echo "\tThat is not a valid IP address, \c"
			    echo "please try again"
			else
			    echo "\t$2 = $ansvalue"
			    askyn "\tIs this OK?" && {
				# answered yes
				isitok="ok"
			    }
			fi
		fi
	done
}

# input - String to display
# output - value in "$ansvalue"
askvalue() {
	isitok=
	while [ "$isitok" = ""  ]
	do
		echo "Please enter $2 > \c"
		read ansvalue
		if [ "$ansvalue" = "" ]
		then
			if [ "$1" = "must" ]
			then
				echo "\tYou give an answer"
			else
				isitok=ok
			fi
		else
			echo "\t$2 = $ansvalue"
			askyn "\tIs this OK?" && {
				# answered yes
				isitok=ok
			}
		fi
	done
}

# add host to resolver file
addresolver() {

ed -s $RESOLVER <<! > /dev/null
a
$1	$2
.
w
q
!

}

# main()
#
# Resolver setup
#
echo "This host is being set up as a resolver in the domain $2"

found=`grep NONAMESERVER /etc/profile`
if [ "${found}" != "" ]
then

echo "removing environment variable NONAMESERVER in /etc/profile"
ed -s /etc/profile <<DONE > /dev/null 2>&1
/NONAMESERV/d
w
q
DONE

fi

echo "removing environment variable NONAMESERVER in $RCFILE"
ed -s $RCFILE <<DONE > /dev/null 2>&1
/NONAMESERV/s/^/#/
w
q
DONE

echo "domain		$2"	> $RESOLVER
echo "You will need the IP addresses of two Name Servers for this domain\n"
askipvalue "must" "the IP address of the first Name Server" && {
	addresolver "nameserver" $ansvalue
}
askipvalue "" "the IP address for the backup Name Server" && {
	if [ "$ansvalue" != "" ]
	then
		addresolver "nameserver" $ansvalue
	fi
}
echo "\nResolver setup complete"

# end

