#!/bin/tcsh -f
# JLdL 18Dec08.
#
# Copyright (C) 2008 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# This script may be executed remotely by ssh from another system;
# _ it updates the IP number in the DNS zone map of a given client
# _ registered in the dynahostdns service.
#
# Get the remote zonename from the command line.
set rzone = "$1"
#
# Get the remote IP address from the command line.
set raddr = "$2"
#
# Define a variable with the TAB character.
set tab = "`echo '\t'`"
#
# Define a grep target to match a generic numerical IP address.
set iptrg = '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
#
# Define the zone file.
set zfile = /etc/bind/$rzone.zone
#
# Update the zone file.
echo Updating the $zfile file...
cat $zfile | \
sed -e "s|\(${tab}in${tab}a${tab}\)$iptrg\($tab\;TAG\.\)|\1$raddr\2|g" \
    -e "s|\(${tab}in${tab}wks${tab}\)$iptrg\(.*$tab\;TAG\.\)|\1$raddr\2|g" \
    >! $zfile.DYNAHOSTDNS
cp -f $zfile.DYNAHOSTDNS $zfile
#
# Update the primary DNS server.
echo Updating the primary DNS server...
#
# Handling the migration from bind to bind9;
# _ either one may exist, and bind9 requires
# _ a restart rather than a reload.
if ( -x /etc/init.d/bind ) then
    /etc/init.d/bind reload
else if ( -x /etc/init.d/bind9 ) then
    /etc/init.d/bind9 restart
endif
