#!/bin/bash

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
	echo "Erreur !!!"
	echo "Doit etre execute avec sudo"
	exit 1
fi

bind9=`whereis bind9`
bind=`whereis bind`
bind9Ok=${bind9#*:}
bindOk=${bind#*:}
if [ "$bind9Ok" == "" ] && [ "$bindOk" == "" ]; then
        echo "Le programme Bind n'est peut etre pas installe."
        echo "Executer \"sudo apt-get install bind9\""
        exit 2
fi

adresse_ip=`hostname -i`
echo "Adresse IP : "$adresse_ip
echo "Est-ce correct ? [O/n]"
read reponse
if [ "$reponse" != "o" ] && [ "$reponse" != "O" ] && [ "$reponse" != "" ]; then
	echo "Veuillez verifier vos parametres"
	exit 2
fi

nom_de_domaine=`hostname -d`
echo "Nom de domaine : "$nom_de_domaine
echo "Est-ce correct ? [O/n]"
read reponse
if [ "$reponse" != "o" ] && [ "$reponse" != "O" ] && [ "$reponse" != "" ]; then
        echo "Veuillez verifier votre fichier /etc/hosts"
        exit 2
fi

host_name=`hostname`
echo "Nom d'hote : "$host_name
echo "Est-ce correct ? [O/n]"
read reponse
if [ "$reponse" != "o" ] && [ "$reponse" != "O" ] && [ "$reponse" != "" ]; then
        echo "Veuillez verifier votre fichier /etc/hosts"
        exit 2
fi

rep_bind="/etc/bind"
if [ ! -d $rep_bind ]; then
	echo "Le repertoire $rep_bind n'existe pas !"
	echo "Le programme Bind n'est peut etre pas installe."
	echo "Executer \"sudo apt-get install bind9\""
	exit 2
fi

named_conf="/etc/bind/named.conf"
if [ ! -f $named_conf ]; then
        echo "Le fichier $named_conf n'existe pas !"
        echo "Le programme Bind n'est peut etre pas installe."
        echo "Executer \"sudo apt-get install bind9\""
        exit 2
fi

old="$IFS"
IFS="."
tab=( $adresse_ip )
IFS="$old"

for (( i=0 ; i<${#tab[*]} ; i++ )) ; do
        echo $i" -> "${tab[$i]}
done

addr=${tab[2]}"."${tab[1]}"."${tab[0]}
addr_inv=${tab[0]}"."${tab[1]}"."${tab[2]}
file_conf=$rep_bind"/named.conf."$host_name
file_nom_de_domaine=$rep_bind"/zones/"$nom_de_domaine".hosts"
file_in_addr=$rep_bind"/zones/rev."$addr_inv".in-addr.arpa"

echo "Creation du fichier \"$file_conf\""
echo "" > $file_conf
echo "Ajout des informations dans le fichier \"$rep_bind"/named.conf."$host_name\""

echo "zone \"$nom_de_domaine\" {
	type master;
	file \"$file_nom_de_domaine\";
};" >> $file_conf

echo "" >> $file_conf

echo "zone \"$addr.in-addr.arpa\" {
	type master;
	file \"$file_in_addr\";
};" >> $file_conf

echo "Ajout du lien vers \"$file_conf\" dans le fichier \"$named_conf\""
echo "include \"$file_conf\";" >> $named_conf

rep_zones=$rep_bind"/zones"
echo "Creation du repertoire \"$rep_zones\""
mkdir $rep_zones

hostname_f=`hostname -f`
echo "Creation du fichier \"$file_nom_de_domaine\""
echo "" > $file_nom_de_domaine
echo "\$ttl 86400
@	IN	SOA	$hostname_f. mail.$nom_de_domaine. (
			2008061803
			21600
			3600
			604800
			86400 )

;ENREGISTREMENT \"A\" DNS <-> IP CLASSIQUES
@	IN	NS	mail.$nom_de_domaine.
	IN	MX	10 mail.$nom_de_domaine.
	IN	A	$adresse_ip
mail	IN	A	$adresse_ip
$host_name	IN	A	$adresse_ip

;ENREGISTREMENT MESSAGERIE
$nom_de_domaine.	IN	MX	10 $host_name" >> $file_nom_de_domaine


echo "Creation du fichier \"$file_in_addr\""
echo "" > $file_in_addr
echo "@	IN	SOA	$nom_de_domaine. admin.$nom_de_domaine. (
			2006081403;
			28800;
			604800;
			604800;
			86400);

	IN	NS	$hostname_f.
${tab[3]}	IN	PTR	$hostname_f." >> $file_in_addr

named_conf_options=$rep_bind"/named.conf.options"
echo "Creation du fichier \"$named_conf_options\""
if [ -f $named_conf_options ]; then
        mv $named_conf_options $named_conf_options".save"
fi
echo "" > $named_conf_options
echo "options {
	directory \"/var/cache/bind\";
	
	query-source address * port 53;

	forwarders {
		212.27.40.240;
		212.27.40.241;
	};
	
	auth-nxdomain no;	# conform to RFC1035
	listen-on-v6 { any; };
};" >> $named_conf_options

echo "Configuration terminee."
echo "Appuyer sur Entree pour continuer..."
read

echo "Redemarrage de Bind9"
/etc/init.d/bind9 restart
echo "Appuyer sur Entree pour continuer..."
read

echo "Verification de la configuration"
named-checkconf
echo "Appuyer sur Entree pour continuer..."
read

echo "Verification des enregistrements MX"
dig mx $nom_de_domaine
echo "Appuyer sur Entree pour continuer..."
read

echo "Verification des enregistrements A"
dig a "mail."$nom_de_domaine
echo "Appuyer sur Entree pour continuer..."
read

echo "Verification des enregistrements DNS"
nslookup $nom_de_domaine
echo "Appuyer sur Entree pour continuer..."
read

exit 0
