#!/bin/bash

#
# source functions.sh for einfo, eerror and ewarn
. /sbin/functions.sh

setup() {
	echo
	echo
	einfo "tinydns Setup"
	echo
	echo ">>> More information on this package can be found at"
	echo ">>> http://cr.yp.to/djbdns/tinydns.html"
	echo
	echo "If you have previously setup tinydns, those directories will"
	echo "not be overwritten.  To redo setup, delete your"
	echo "tinydns dir tree first."
	echo
	echo '(press enter to begin setup, or press control-C to abort)'
	echo
	read

	echo
	einfo "Install location"
	echo
	echo "Where do you want tinydns installed?"
	echo "Ex. /var would install dnscache in /var/tinydns."
	echo "!!No trailing slash!!"
	echo
	read -p "[/var]> " mypath
	echo

	if [ "$mypath" == "" ]
	then
		mypath="/var"
	fi

	if [ ! -e ${mypath} ]
	then
		echo ">>> Creating ${mypath}..."
		mkdir $mypath
	fi

	# check for existance of users dnscache and dnslog:
	echo
	echo
	einfo "Checking for tinydns and dnslog user accts ..."
	echo
	/usr/bin/grep nofiles /etc/group &> /dev/null
	if [ $? -ne 0 ]
	then
		echo ">>> Adding group nofiles ..."
		/usr/sbin/groupadd nofiles &> /dev/null
	fi

	/usr/bin/grep tinydns /etc/passwd &> /dev/null
	if [ $? -ne 0 ]
	then
		echo ">>> Adding user dnscache ..."
		/usr/sbin/useradd -d /dev/null -s /bin/false -g nofiles \
			dnscache &> /dev/null
	fi

	/usr/bin/grep dnslog /etc/passwd &> /dev/null
	if [ $? -ne 0 ]
	then
		echo ">>> Adding user dnslog ..."
		/usr/sbin/useradd -d /dev/null -s /bin/false -g nofiles \
			dnslog &> /dev/null
	fi


	# grab interfaces
	addrs=`ifconfig -a | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "`

	echo "Specify an address to which tinydns should bind."
	echo "NOTICE: tinydns must be able to bind to port 53 on "
	echo "choosen ip address! udp by tinydns - tcp by axfrdns"
	echo "Usually this is NOT 127.0.0.1"
	echo "Currently running IP addresses:"
	echo
	echo $addrs
	echo

	while [ "$myip" = "" ]
	do
		read -p "IP to bind nameserver to>" myip
	done
	echo

	if [ ! -e ${mypath}/tinydns ]
	then
		einfo "Setting up tinydns..."
		/usr/bin/tinydns-conf tinydns dnslog \
			${mypath}/tinydns $myip
	else
		ewarn "*** tinydns directory currently exists, nothing done."
	fi

	#add afxrdns
	if [ ! -e ${mypath}/axfrdns ]
	then
		einfo "Setting up axfrdns..."
		/usr/bin/axfrdns-conf tinydns dnslog \
			${mypath}/axfrdns ${mypath}/tinydns $myip
	else
		ewarn "*** axfrdns directory currently exists, nothing done."
	fi

	#grant access to axfrdns

	echo
	echo
	einfo "Start service"
	echo
	echo "tinydns is ready for startup."
	echo "Do you want dnscache to be started and"
	echo "supervised by daemontools now?"

	echo
	echo "This requires daemontools to supervise"
	echo "/service !!"
	echo
	echo '(press control-C to abort)'
	read

	# Don't make symbolic links to / !
	# use ../ instead as it gives trouble in chrooted environments
	# By Kalin KOZHUHAROV <kalin@ThinRope.net>
	local fixedroot_path=`echo ${mypath} | sed -e 's#^/#../#'`
	cd /service
	ln -sf ${fixedroot_path}/tinydns .
	ln -sf ${fixedroot_path}/axfrdns .

	echo
	echo
	einfo "Installation successfull"
	echo

}

# check for root user!
if [ `id -u` -ne 0 ]
then
        eerror "${0}: must be root."
	exit 1
fi
		

# run setup
setup
