#!/bin/sh
# Add new zone to Bind Master DNS on Debian GNU / Linux
# Written and copyrighted by Georgi Georgiev a.k.a. hip0
# To be working the script assumes /etc/bind/masters/domain_skele.com is existing with records
# for domain_skele.com
# http://www.pc-freak.net
# To use it run it and enter new domain which will enter DNS named.conf and it will create new master file in /etc/bind/masters/domain_skele.com
# Also be sure to add string '#automatically added by add_to_named_conf.sh' to /etc/bind/named.conf (nearby the end of all other domain master records e.g. - add the string after - zone "domain.com" { type master; ... 
# WARNING !!If you're using the script always create backup of /etc/bind9/ first !!
named_doc_root=/etc/bind;
named_conf_location=$named_doc_root/named.conf;
temp_file=/tmp/named.conf.temp;
user='bind';
group='bind';
arg1=$1;
add_after_string='#automatically added by add_to_named_conf.sh';

echo -n "Enter Domain: ";
read domain;
if [ ! $domain ]; then
echo "Missing domain name";
exit 1;
fi
# Create NS config backup
cp -rpf /etc/bind /etc/bind-bakh
echo "Adding $domain to $named_conf_location";
while IFS=$'\n' read -r line; do

if [ "$line" = "$add_after_string" ]; then
echo -e "# $domain auto added" >> $temp_file;
echo -e "zone \"$domain\" { type master; file \"/etc/bind/masters/$domain\"; };\n" >>$temp_file;

fi

echo "${line}" >>$temp_file;
done < $named_conf_location;
chown $user:$group $temp_file;
mv $temp_file $named_conf_location;
echo "Testing our new $named_conf_location";
/usr/sbin/named-checkconf $named_conf_location;
if [ -s $temp_file ]; then
rm -f $temp_file;
fi
sed -e "s#domain_skele.com#$domain#g" $named_doc_root/masters/domain_skele.com >> /tmp/$domain
mv /tmp/$domain $named_doc_root/masters/$domain
echo "Check manually if all is fine again with named-checkzone named_doc_root/masters/$domain";
echo "If all is okay to make new changes affective restart DNS with /etc/init.d/bind9 restart";

