#!/bin/bash

# Standardwerte setzen
SAAL=""
NUMMER=""

# Optionen 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

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

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

# Logik für die Nummer (Standard 01, sonst Validierung)
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 "Bereinige alte Installation..."
apt purge -y bind9
rm -fr /var/cache/bind/ /etc/bind/

echo "Installiere BIND9..."
apt install -y bind9

# Netzwerk-Informationen sammeln
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. Konfiguration: options & ACL für Views
cat <<HERE > /etc/bind/named.conf.options
acl "trusted" {
    $DMZ;
    $LAN;
    $SERVER;
    127.0.0.1;
};

options {
    directory "/var/cache/bind";
    forwarders { $DNSGW; };
    allow-query { any; };
    dnssec-validation no;
    listen-on-v6 { none; };
    listen-on { any; };
};
HERE

# 2. Konfiguration: local mit Views (Split-Horizon)
cat <<HERE > /etc/bind/named.conf.local
view "internal" {
    match-clients { "trusted"; };
    recursion yes;

    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"; };
};

view "public" {
    match-clients { any; };
    recursion no;

    zone "it$OKT.int" {
        type master;
        file "/var/cache/bind/it$OKT.int.public";
    };

    zone "$OKT.88.10.in-addr.arpa" {
        type master;
        file "/var/cache/bind/$OKT.88.10.in-addr.arpa";
    };
};
HERE

# 3. Forward Zone File - INTERN (Vollständig)
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

# 4. Forward Zone File - PUBLIC (Nur DMZ/10.88er Netz)
cat <<HERE > /var/cache/bind/it$OKT.int.public
\$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
HERE

# 5. Reverse Zone Files (identisch für beide Views nutzbar)
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 vor dem Neustart
echo "Prüfe Konfiguration..."
named-checkconf /etc/bind/named.conf
if [ $? -eq 0 ]; then
    echo "Syntax OK. Starte BIND9 neu..."
    systemctl restart named
    systemctl status named --no-pager
else
    echo "FEHLER in der BIND-Konfiguration!"
    exit 1
fi

cat<<HERE > /etc/resolv.conf
search it$OKT.int
nameserver 192.168.$SAAL.88
HERE
