--- a/register-p.sh
+++ b/register-p.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 # Parameters
 # 1 = hostname
@@ -7,32 +7,28 @@
 #
 # Constants
 #
-SENDER=requests@domain.com
+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
 
-# Define locations
-TEMP=/etc/dhis/temp
-GENID=/etc/dhis/bin/genid
-GENKEYS=/etc/dhis/bin/genkeys
-GENPASS=/etc/dhis/bin/genpass
-DBDIR=/etc/dhis/db
-DBFILE=/etc/dhis/db/dhis.db
-
-ISADDR=123.123.123.123		; IP address of DHIS server
-MXADDR=122.222.222.222		; IP address of relay server
-DHISD_PID=/etc/dhis/pid/dhisd.pid
-
-RELAY=relay.domain.com
-NSUPDATE=/usr/sbin/nsupdate
-
-$GENPASS > $TEMP/pass.$$
-$GENID > $TEMP/id.$$
+$GENPASS > $temp_pass
+$GENID > $temp_id
 
 # DB File
 #
 echo "" >> $DBFILE
-echo `cat $TEMP/id.$$ | awk '{ print $2 }'`" {" >> $DBFILE
+echo `cat $temp_id | awk '{ print $2 }'`" {" >> $DBFILE
 echo "\thostname\t$1" >> $DBFILE
-cat $TEMP/pass.$$ >> $DBFILE
+cat $temp_pass >> $DBFILE
 echo "\tcontact\t\t$2" >> $DBFILE
 echo "\temail\t\t$3" >> $DBFILE
 echo "\tservice\t\tdns" >> $DBFILE
@@ -41,14 +37,12 @@
 
 # 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.$$
-echo "update add $1. 86600 in mx 10  $RELAY." >> $TEMP/nsupdate.$$
-echo "" >> $TEMP/nsupdate.$$
-$NSUPDATE $TEMP/nsupdate.$$
-rm -f $TEMP/nsupdate.$$
-
+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
 
-# Remove Files
-rm -f $TEMP/id.$$
-rm -f $TEMP/pass.$$
+exit 0
--- a/register-q.sh
+++ b/register-q.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 # Parameters
 # 1 = hostname
@@ -7,32 +7,29 @@
 #
 # Constants
 #
-SENDER=requests@domain.com
+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_keys=`mktemp -p $TEMP keys.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
 
-# Define locations
-TEMP=/etc/dhis/temp
-GENID=/etc/dhis/bin/genid
-GENKEYS=/etc/dhis/bin/genkeys
-GENPASS=/etc/dhis/bin/genpass
-DBDIR=/etc/dhis/db
-DBFILE=/etc/dhis/db/dhis.db
-
-ISADDR=123.123.123.123		; IP address of DHIS server
-MXADDR=122.222.222.222		; IP address of relay server
-DHISD_PID=/etc/dhis/pid/dhisd.pid
-
-RELAY=relay.domain.com
-NSUPDATE=/usr/sbin/nsupdate
-
-$GENKEYS > $TEMP/keys.$$
-$GENID > $TEMP/id.$$
+$GENKEYS > $temp_keys
+$GENID > $temp_id
 
 # DB File
 #
 echo "" >> $DBFILE
-echo `cat $TEMP/id.$$ | awk '{ print $2 }'`" {" >> $DBFILE
+echo `cat $temp_id | awk '{ print $2 }'`" {" >> $DBFILE
 echo "\thostname\t$1" >> $DBFILE
-cat $TEMP/keys.$$ >> $DBFILE
+cat $temp_keys >> $DBFILE
 echo "\tcontact\t\t$2" >> $DBFILE
 echo "\temail\t\t$3" >> $DBFILE
 echo "\tservice\t\tdns" >> $DBFILE
@@ -41,14 +38,12 @@
 
 # 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.$$
-echo "update add $1. 86600 in mx 10  $RELAY." >> $TEMP/nsupdate.$$
-echo "" >> $TEMP/nsupdate.$$
-$NSUPDATE $TEMP/nsupdate.$$
-rm -f $TEMP/nsupdate.$$
-
+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
 
-# Remove Files
-rm -f $TEMP/id.$$
-rm -f $TEMP/keys.$$
+exit 0
