#!/bin/sh
# $Id: bind-forwarders,v 1.3 2002/02/27 06:35:25 andrew Exp $
#
# by Andrew McMillan, Catalyst IT Ltd, (c) 2002 licensed
# for use under the GPL version 2
#
# usage:
# bind-forwarders <IP address>
#

FILENAME=/etc/bind/named.conf

if [ ! -e $FILENAME ] ; then
  logger -p user.warning -t whereami -i "Bind configuration named.conf missing!"
  cp $FILENAME.last $FILENAME
  if [ ! -e $FILENAME ] ; then
    logger -p user.error -t whereami -i "Really stuffed - can't copy from backup named.conf!"
    exit 1 
  fi
fi

logger -p user.info -t whereami -i "Setting forwarder to $1"
sed -e "s/\([ 	]*\)[0-9.]*\([;].*bind-forwarders\)/\1$1\2/" <$FILENAME >$FILENAME.tmp

#
if [ -e $FILENAME.tmp ] ; then
  chmod --reference=$FILENAME $FILENAME.tmp
  chown --reference=$FILENAME $FILENAME.tmp
  mv $FILENAME $FILENAME.last
  cp $FILENAME.tmp $FILENAME
else
  logger -p user.error -t whereami -i "Error handling named.conf changes!"
  exit 1 
fi

# Look for the named.conf file again.
if [ ! -e $FILENAME ] ; then
  logger -p user.error -t whereami -i "Wha..? named.conf gone missing!"
  exit 1
fi

# Test to see if bind is already running (just reload it) or needs to be started
BIND_RUNNING="`find /proc -type d -maxdepth 1 -name \`cat /var/run/named.pid\``"
if [ "$BIND_RUNNING" = "" ] ; then
  logger -p user.warning -t whereami -i "Nameserver not running - starting it."
  /etc/init.d/bind start
else
  /etc/init.d/bind reload
fi

if [ ! -e $FILENAME ] ; then
  logger -p user.error -t whereami -i "Wha..? named.conf gone missing after restarting bind!"
  exit 1
fi

