#!/bin/sh
#
# B4K2.1 – OpenWrt Diagnose: Routingtabelle 200 (anonymisiert)
# Nur lesend – keine UCI‑, Routing‑ oder Firewalländerungen.
#

set -u

LOG_DIR="/root/diagnose-logs"        # Platzhalter
STAMP="$(date +%Y%m%d_%H%M%S)"
LOG_FILE="${LOG_DIR}/B4K2-1_OpenWrt_Tabelle200_Diagnose_${STAMP}.log"

mkdir -p "$LOG_DIR"

{
    echo "============================================================"
    echo "B4K2.1 – OpenWrt Diagnose Routingtabelle 200"
    echo "Zeitpunkt: $(date)"
    echo "Host: $(hostname)"
    echo "Logdatei: $LOG_FILE"
    echo "============================================================"
    echo

    echo "### 1. System"
    uname -a
    echo

    echo "### 2. IPv4‑Schnittstellen"
    ip -4 addr show 2>&1
    echo

    echo "### 3. Status der WAN‑Schnittstelle"
    if command -v ubus >/dev/null 2>&1; then
        ubus call network.interface.wan status 2>&1 || true
    else
        echo "HINWEIS: ubus nicht gefunden."
    fi
    echo

    echo "### 4. Aktive Policy‑Routing‑Regeln"
    ip rule show 2>&1
    echo

    echo "### 5. Aktive Routingtabelle 200"
    ip route show table 200 2>&1
    echo

    echo "### 6. Alle aktiven IPv4‑Routen mit Bezug zu Tabelle 200 oder HA‑Gateway"
    ip -4 route show table all 2>&1 \
        | grep -E 'table 200|192\.168\.xxx\.xxx|192\.168\.xxx\.0/24|default' \
        || true
    echo

    echo "### 7. Gespeicherte UCI‑Routenkonfiguration"
    uci show network 2>&1 \
        | grep -E '=route|\.interface=|\.target=|\.netmask=|\.gateway=|\.table=|\.metric=|\.disabled=' \
        || true
    echo

    echo "### 8. Noch nicht bestätigte UCI‑Änderungen"
    uci changes network 2>&1 || true
    echo

    echo "### 9. Relevante Abschnitte aus /etc/config/network"
    if [ -r /etc/config/network ]; then
        awk '
            /^config route/ {show=1}
            /^config / && $2 != "route" && show==1 {show=0}
            show==1 {print}
        ' /etc/config/network
    else
        echo "FEHLER: /etc/config/network ist nicht lesbar."
    fi
    echo

    echo "### 10. Netzwerkmeldungen aus dem Systemprotokoll"
    if command -v logread >/dev/null 2>&1; then
        logread 2>&1 \
            | grep -E 'netifd|route|wan|192\.168\.xxx\.xxx|table 200' \
            | tail -n 150 \
            || true
    else
        echo "HINWEIS: logread nicht gefunden."
    fi
    echo

    echo "### 11. Automatische Bewertung"
    ACTIVE_DEFAULT=0
    ACTIVE_LOCAL=0
    UCI_DEFAULT=0
    UCI_LOCAL=0
    PENDING=0

    ip route show table 200 2>/dev/null \
        | grep -q '^default via 192\.168\.xxx\.xxx ' \
        && ACTIVE_DEFAULT=1

    ip route show table 200 2>/dev/null \
        | grep -q '^192\.168\.xxx\.0/24 ' \
        && ACTIVE_LOCAL=1

    uci show network 2>/dev/null \
        | grep -q "\.gateway='192\.168\.xxx\.xxx'" \
        && UCI_DEFAULT=1

    uci show network 2>/dev/null \
        | grep -q "\.target='192\.168\.xxx\.0'" \
        && UCI_LOCAL=1

    [ -n "$(uci changes network 2>/dev/null)" ] && PENDING=1

    echo "Aktive Standardroute Tabelle 200: $ACTIVE_DEFAULT"
    echo "Aktive lokale Route Tabelle 200: $ACTIVE_LOCAL"
    echo "In UCI gespeichertes Gateway 192.168.xxx.xxx: $UCI_DEFAULT"
    echo "In UCI gespeicherte Route 192.168.xxx.0: $UCI_LOCAL"
    echo "Nicht bestätigte UCI‑Änderungen vorhanden: $PENDING"
    echo

    if [ "$ACTIVE_DEFAULT" -eq 1 ] && [ "$ACTIVE_LOCAL" -eq 1 ]; then
        echo "ERGEBNIS: Tabelle 200 ist zur Laufzeit vollständig aktiv."
    elif [ "$UCI_DEFAULT" -eq 1 ] || [ "$UCI_LOCAL" -eq 1 ]; then
        echo "ERGEBNIS: Routen sind zumindest in UCI vorhanden, aber zur Laufzeit unvollständig oder nicht aktiv."
    else
        echo "ERGEBNIS: Die erwarteten Routen sind weder vollständig aktiv noch eindeutig in UCI vorhanden."
    fi
    echo

    echo "============================================================"
    echo "Diagnose abgeschlossen."
    echo "Es wurden keine UCI‑, Routing‑ oder Firewallregeln verändert."
    echo "============================================================"
} 2>&1 | tee "$LOG_FILE"

echo
echo "LOGDATEI=$LOG_FILE"