#! /bin/sh
# Copyright (C) 1998, Greg Stark <gsstark@mit.edu> distributed under the GPL

set -e

cat <<EOF
Zephyr needs a list of zephyr servers to work. 

Both the clients (via zhm) and the server depend
on finding this list in /etc/zephyr/server.list.

You can rerun this configuration at any time by running 
/usr/sbin/zephyrconfig

EOF

if test -s /etc/zephyr/server.list && test -n "`cat /etc/zephyr/server.list`" ; then
	echo "The current list of zephyr servers is:"
	cat /etc/zephyr/server.list
	echo -n "Is this correct? [Yn] "
	read good
	case "$good" in 
		"" | [yY]*)
			echo "Done"
			exit 0 
			;;
	esac			
fi

# poor man's hesiod
if [ -x /bin/dnsdomainname -a -x /usr/bin/dig -a -x /bin/sed -a -x /usr/bin/tr ] ; then
    SVC=zephyr
    REALM="$(dnsdomainname | tr '[a-z]' '[A-Z')"
    case "$REALM" in *MIT.EDU) REALM=ATHENA.MIT.EDU ;; esac
	# what other paths does this file show up at? and test hesiod.conf too
	if test -r /etc/athena/krb.conf ; then REALM="$(head -1 /etc/athena/krb.conf)" ; fi
    A="${SVC}.sloc.ns.${REALM}"
    servers=$(dig -t any "$A" 2>/dev/null | sed -n "/^$A/s/^.*\"\\(.*\\)\".*$/\\1/p")

    if [ -n "$servers" ] ; then
		echo "These seem to be your site's servers:"
		for i in $servers ; do echo "$i" ; done
		echo -n "Is this correct? [Yn] "
		read good
		case "$good" in 
			"" | [yY]*)
				echo -n > /etc/zephyr/server.list
				for i in $servers ; do echo "$i" >> /etc/zephyr/server.list ; done
				echo "Done"
				exit 0 
				;;
		esac
	fi
fi

echo -n > /etc/zephyr/server.list
echo "Please enter the names of the servers one per line, end with an blank line"
read s
while [ -n "$s" ] ; do
	echo "$s" >> /etc/zephyr/server.list
	read s
done

echo "Done"
