#!/bin/bash

# Standardwerte setzen
SAAL=""
NUMMER=""

# Optionen mit getopts parsen
while getopts "s:n:" opt; do
  case $opt in
    s) SAAL="$OPTARG" ;;
    n) NUMMER="$OPTARG" ;;
    *) echo "Nutzung: $0 -s <SAAL> -n <NUMMER>" >&2; exit 1 ;;
  esac
done

# Gültigkeit prüfen
if [[ -z "$SAAL" ]]; then
  echo "Fehlender SAAL! Nutzung: $0 -s <SAAL> -n <NUMMER>"
  exit 1
fi

DATE=$(date "+%Y%m%d")

if [[ -z "$NUMMER" ]]; then 
    NUMMER="01"
else
    if [[ ! "$NUMMER" =~ ^[0-9]+$ ]]; then
        echo "Fehler: '$NUMMER' ist keine gültige Zahl."
        exit 1
    fi

    if (( NUMMER >= 1 && NUMMER <= 99 )); then
        NUMMER=$(printf "%02d" "$NUMMER")
    else
        echo "Fehler: Zahl $NUMMER liegt nicht zwischen 1 und 99."
        exit 1
    fi
fi

SN="${DATE}${NUMMER}"

echo "Aufräumen und Installation..."
apt purge -y bind9
rm -fr /var/cache/bind/ /etc/bind/
apt install -y bind9

# Netzwerk-Logik
IP=$(ip a s enp0s3 | grep "inet\>" | awk '{print $2}')
OKT=$(echo $IP | cut -f 3 -d .)
DNSGW="192.168.$SAAL.88"
DMZ="10.88.$OKT.0/24"
LAN="172.26.$OKT.0/24"
SERVER="10.$OKT.1.0/24"

# 1. Konfigurationsdateien schreiben
cat <<HERE > /etc/bind/named.conf.options
options {
  directory "/var/cache/bind";
  forwarders { $DNSGW; };
  allow-query { any; };
  allow-recursion { $DMZ; $LAN; $SERVER; 127.0.0.1; };
  allow-transfer { 127.0.0.1; };
  dnssec-validation no;
  listen-on-v6 { none; };
  listen-on { any; };
};
HERE

cat <<HERE > /etc/bind/named.conf.local
zone "it$OKT.int" {
    type master;
    file "/var/cache/bind/it$OKT.int";
};
zone "$OKT.88.10.in-addr.arpa" {
    type master;
    file "/var/cache/bind/$OKT.88.10.in-addr.arpa";
};
zone "$OKT.26.172.in-addr.arpa" {
    type master;
    file "/var/cache/bind/$OKT.26.172.in-addr.arpa";
};
zone "1.$OKT.10.in-addr.arpa" {
    type master;
    file "/var/cache/bind/1.$OKT.10.in-addr.arpa";
};
HERE

# 2. Zone Files erstellen
cat <<HERE > /var/cache/bind/it$OKT.int
\$TTL 1
@   IN SOA ns.it$OKT.int. technik.it$OKT.int. ( $SN 14400 3600 3600000 86400 )
    NS      ns.it$OKT.int.
fw      IN      A       10.88.$OKT.1
ns      IN      A       10.88.$OKT.21
sftp    IN      A       10.88.$OKT.3
ntp     IN      A       10.88.$OKT.4
dhcp    IN      A       172.26.$OKT.2
smb     IN      A       10.$OKT.1.2
ldap    IN      A       10.$OKT.1.3
HERE

cat <<HERE > /var/cache/bind/$OKT.88.10.in-addr.arpa
\$TTL 1
@   IN SOA ns.it$OKT.int. technik.it$OKT.int. ( $SN 14400 3600 3600000 86400 )
    NS      ns.it$OKT.int.
1       IN      PTR     fw.it$OKT.int.
21      IN      PTR     ns.it$OKT.int.
3       IN      PTR     sftp.it$OKT.int.
4       IN      PTR     ntp.it$OKT.int.
HERE

cat <<HERE > /var/cache/bind/$OKT.26.172.in-addr.arpa
\$TTL 1
@   IN SOA ns.it$OKT.int. technik.it$OKT.int. ( $SN 14400 3600 3600000 86400 )
    NS      ns.it$OKT.int.
2       IN      PTR     dhcp.it$OKT.int.
HERE

cat <<HERE > /var/cache/bind/1.$OKT.10.in-addr.arpa
\$TTL 1
@   IN SOA ns.it$OKT.int. technik.it$OKT.int. ( $SN 14400 3600 3600000 86400 )
    NS      ns.it$OKT.int.
2       IN      PTR     smb.it$OKT.int.
3       IN      PTR     ldap.it$OKT.int.
HERE

# --- SYNTAX PRÜFUNG ---
echo "Prüfe Konfiguration..."
named-checkconf /etc/bind/named.conf
if [ $? -ne 0 ]; then
    echo "Fehler in der named.conf!"
    exit 1
fi

echo "Prüfe Zone-Datei it$OKT.int..."
named-checkzone "it$OKT.int" "/var/cache/bind/it$OKT.int"
if [ $? -ne 0 ]; then
    echo "Fehler in der Zone-Datei!"
    exit 1
fi

echo "Alles OK. Restarting named..."
systemctl restart named

# DNS-Eintrag lokal setzen
cat <<HERE > /etc/resolv.conf
search it$OKT.int
nameserver 127.0.0.1
HERE

echo "Skript erfolgreich beendet."
