#!/bin/sh

# Parameters
# 1 = hostname
# 2 = contact
# 3 = email
#
# Constants
#
if [ -e /etc/default/dhis-tools-dns ] ; then
	. /etc/default/dhis-tools-dns
else
	echo "Cannot read default definitions from /etc/default/dhis-tools-dns. Aborting" >&2
	exit 1
fi

temp_pass=`mktemp -p $TEMP pass.XXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1;  }
temp_id=`mktemp -p $TEMP id.XXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1;  }
temp_nsupdate=`mktemp -p $TEMP nsupdate.XXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1;  }
# Remove Files on exit
trap "/bin/rm -f -- \"$temp_id\" \"$temp_nsupdate\" \"$temp_keys\"" 0 1 2 3 13 15

$GENPASS > $temp_pass
$GENID > $temp_id

# DB File
#
echo "" >> $DBFILE
echo `cat $temp_id | awk '{ print $2 }'`" {" >> $DBFILE
printf "\thostname\t$1\n" >> $DBFILE
cat $temp_pass >> $DBFILE
printf "\tcontact\t\t$2\n" >> $DBFILE
printf "\temail\t\t$3\n" >> $DBFILE
printf "\tservice\t\tdns\n" >> $DBFILE
echo "}" >> $DBFILE
kill -HUP `cat $DHISD_PID` 

# Update DNS
#
echo "update add $1. 60 in a 192.168.255.0" > $temp_nsupdate
echo "update add $1. 86600 in mx 0  $1." >> $temp_nsupdate
if [ -n "$RELAY" ]; then
	echo "update add $1. 86600 in mx 10  $RELAY." >> $temp_nsupdate
fi
echo "" >> $temp_nsupdate
$NSUPDATE $temp_nsupdate

exit 0
