#!/bin/bash
# =================================================================================
# install.sh - Vollständiges Installationsskript für Bücherei 2.0
# Inklusive Sudo-Check, Yum-Support und separatem Requirements-Download.
# =================================================================================

# --- KONFIGURATION ---
REQ_URL="https://ddl.buch-archiv20-software.de/haubt/script/requirements.txt"
DOWNLOAD_URL="https://ddl.buch-archiv20-software.de/haubt/py-2.2.0/v.2.2.0.zip"
TEMP_ZIP="v.2.2.0.zip"
INSTALL_DIR="Bücherei_Projekt"

echo "--- Bücherei 2.0 Installationsskript gestartet ---"

# --- 0. SUDO-CHECK ---
if [ "$EUID" -ne 0 ]; then
    echo "💡 Hinweis: Das Skript benötigt eventuell Sudo-Rechte für Systempakete."
    echo "Falls Python3/Unzip/Curl schon installiert sind, ist das kein Problem."
fi

# --- 1. PAKETMANAGER ERKENNEN & SYSTEM-TOOLS ---

detect_and_install() {
    # Wir prüfen erst, ob die Tools schon da sind
    if command -v python3 &> /dev/null && command -v unzip &> /dev/null && command -v curl &> /dev/null; then
        echo "✅ Alle System-Tools (Python, Unzip, Curl) bereits vorhanden."
        return 0
    fi

    echo "-> Installiere fehlende System-Abhängigkeiten..."

    if command -v apt &> /dev/null; then
        echo "Detected: apt (Debian/Ubuntu)"
        sudo apt update && sudo apt install -y python3 python3-pip python3-venv unzip curl
    elif command -v dnf &> /dev/null; then
        echo "Detected: dnf (Fedora/RHEL/CentOS)"
        sudo dnf install -y python3 python3-pip python3-venv unzip curl
    elif command -v yum &> /dev/null; then
        echo "Detected: yum (Älteres RHEL/CentOS)"
        sudo yum install -y python3 python3-pip python3-venv unzip curl
    else
        echo "❌ Fehler: Kein bekannter Paketmanager (apt, dnf, yum) gefunden."
        echo "Bitte installieren Sie Python 3, pip, venv, unzip und curl manuell."
        exit 1
    fi
}

detect_and_install

# Projektordner erstellen
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR" || { echo "Fehler: Konnte Ordner nicht erstellen."; exit 1; }

# --- 2. REQUIREMENTS SEPARAT LADEN & UMGEBUNG EINRICHTEN ---
echo "--- 2. Lade Abhängigkeiten (requirements.txt) ---"

curl -L -o "requirements.txt" "$REQ_URL"
if [ $? -ne 0 ]; then
    echo "❌ Fehler beim Download der requirements.txt von: $REQ_URL"
    exit 1
fi

echo "--- 3. Erstelle virtuelle Umgebung (Venv) ---"
python3 -m venv venv
if [ ! -d "venv" ]; then
    echo "❌ Fehler: Virtuelle Umgebung konnte nicht erstellt werden."
    exit 1
fi

source venv/bin/activate

echo "📦 Installiere Python-Pakete in die Venv..."
pip install --upgrade pip
pip install -r requirements.txt

if [ $? -ne 0 ]; then
    echo "❌ Fehler bei der Installation der Python-Abhängigkeiten."
    exit 1
fi

# --- 4. HAUPTPROGRAMM LADEN & ENTPAKKEN ---
echo "--- 4. Lade Hauptprogramm-Archiv herunter ---"

curl -L -o "$TEMP_ZIP" "$DOWNLOAD_URL"
if [ $? -ne 0 ]; then
    echo "❌ Fehler beim Download der Programm-ZIP."
    exit 1
fi

echo "📦 Entpacke Programmdateien..."
# -o überschreibt, -q ist leise
unzip -o -q "$TEMP_ZIP" -d .

# Aufräumen
rm "$TEMP_ZIP"

# --- 5. FINALE ANWEISUNGEN ---
echo ""
echo "========================================================================"
echo "🎉 Installation erfolgreich abgeschlossen!"
echo "Verzeichnis: $(pwd)"
echo "========================================================================"
echo ""
echo "START-ANLEITUNG (Schritt für Schritt):"
echo "1. In den Ordner gehen:  cd $INSTALL_DIR"
echo "2. Venv aktivieren:      source venv/bin/activate"
echo "3. SETUP AUSFÜHREN:      python3 setup.py  <-- WICHTIG beim 1. Mal!"
echo "4. Programm starten:     python3 all.py"
echo ""
echo "Viel Erfolg mit Bücherei 2.0!"
echo "========================================================================"

exit 0
