#!/bin/bash
# Author: Nico Schottelius ( nicos@pcsystems.de )
# Last Modified by: 
# Date: 20th of January 2000
# Last Changed: 20th of January
# Description: Skript which does mapping and reverse
# 

# My variables :-)
host=""
ip=""
choice="y"
class=""
domain="mignet.magrathea.de"


# Title
echo "## Neuer Nameserver-Eintrag ##"
read -p "Bitte Domain eingeben ($domain) : " domain

# Check if zero domain
if [ -z "$domain" ]; then domain="mignet.magrathea.de";fi

# Main loop for more than one host in a domain
while [ "$choice" = "y" -o "$choice" = "Y" ] 
do 

read -p "Hostname tippen: " host
read -p "IP klopfen: " ip
read -p "NetClass eingeben: (a,b,c): " class

echo -n "Uno momento..."


# The real program

# First mapping
if [ -e /var/named/$domain ]; 
  then 
  echo "$host		IN	A		$ip" >> /var/named/$domain
else
  cat << EOF >> /etc/named.conf
  
zone "$domain"  in {
	type master;
	file "$domain";
};
EOF
  cat /var/named/sample/domain > /var/named/$domain
  echo "$host		IN	A		$ip" >> /var/named/$domain
fi
echo -n "."

# Lookup the host and net id, to choose the right arpa file
# For A it is net=172 host=16.1.1
if [ $class = a -o $class = A ];
 then 
  # Get the arpa file
  net=`echo $ip | cut -d. -f1`
  
  # Get the hostid.
  ho=`echo $ip |cut -d. -f2-4`

  # The reverse Net
  rnet=$net

# For B it is net=172.16 host=1.1
elif [ $class = b -o $class = B ];
 then
  # Get the arpa file
  net=`echo $ip | cut -d. -f1-2`
  
  # Get the hostid.
  ho=`echo $ip | cut -d. -f3-4`

  # The reverse Net
  rnet1=`echo $ip | cut -d. -f1`
  rnet2=`echo $ip | cut -d. -f2`
  rnet="$rnet2.$rnet1"
  
# For C it is net=172.16.1 host=1
elif [ $class = c -o $class = C ];
 then
  # Get the arpa file
  net=`echo $ip | cut -d. -f1-3`
  
  # Get the hostid.
  ho=`echo $ip | cut -d. -f4`
  
  # The reverse Net
  rnet1=`echo $ip | cut -d. -f1`
  rnet2=`echo $ip | cut -d. -f2`
  rnet3=`echo $ip | cut -d. -f3`
  rnet="$rnet3.$rnet2.$rnet1"
fi

echo -n "."

if [ -e /var/named/reverse/$net ];
  then 
  echo "$ho		IN	PTR	$host.$domain." >> /var/named/reverse/$net
  
else
  cat << EOF >> /etc/named.conf
  
zone "${rnet}.in-addr.arpa" in {
	type master;
	file "reverse/$net";
};
EOF

  cat /var/named/sample/reverse > /var/named/reverse/$net
  echo "$ho		IN	PTR	$host.$domain." >> /var/named/reverse/$net
fi
echo -n "."
echo 'Finito :-)'
read -p "Noch einen Host in $domain eintragen ? " choice
done