#!/bin/bash
#
# Java 21 JDK Installationsscript
# Für Debian/Ubuntu und andere Linux-Distributionen
#

set -e

# Farben
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}========================================"
echo " Java 21 JDK Installation"
echo -e "========================================${NC}"
echo ""

# Prüfen ob Java 21 bereits installiert ist
if command -v javac &> /dev/null; then
    JAVA_VERSION=$(javac -version 2>&1 | awk '{print $2}' | cut -d'.' -f1)
    if [ "$JAVA_VERSION" == "21" ]; then
        echo -e "${GREEN}Java 21 ist bereits installiert!${NC}"
        javac -version
        java -version
        exit 0
    else
        echo -e "${YELLOW}Java $JAVA_VERSION gefunden, aber Java 21 wird benötigt${NC}"
    fi
fi

# Betriebssystem erkennen
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
    OS_LIKE=$ID_LIKE
else
    OS=$(uname -s)
fi

echo -e "Erkanntes OS: ${GREEN}$OS${NC}"
echo ""

install_debian_ubuntu() {
    echo -e "${YELLOW}Installiere Java 21 via apt...${NC}"
    echo ""

    # Prüfen ob openjdk-21-jdk verfügbar ist
    if apt-cache show openjdk-21-jdk &> /dev/null; then
        echo "OpenJDK 21 ist in den Repositories verfügbar"
        sudo apt update
        sudo apt install -y openjdk-21-jdk
    else
        echo "OpenJDK 21 nicht in Standard-Repos, verwende Adoptium/Temurin..."

        # Adoptium Repository hinzufügen
        sudo apt install -y wget apt-transport-https gnupg

        wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo gpg --dearmor -o /usr/share/keyrings/adoptium.gpg

        echo "deb [signed-by=/usr/share/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/adoptium.list

        sudo apt update
        sudo apt install -y temurin-21-jdk
    fi
}

install_fedora_rhel() {
    echo -e "${YELLOW}Installiere Java 21 via dnf/yum...${NC}"
    echo ""

    if command -v dnf &> /dev/null; then
        sudo dnf install -y java-21-openjdk-devel
    else
        sudo yum install -y java-21-openjdk-devel
    fi
}

install_arch() {
    echo -e "${YELLOW}Installiere Java 21 via pacman...${NC}"
    echo ""
    sudo pacman -S --noconfirm jdk21-openjdk
}

install_opensuse() {
    echo -e "${YELLOW}Installiere Java 21 via zypper...${NC}"
    echo ""
    sudo zypper install -y java-21-openjdk-devel
}

install_manual() {
    echo -e "${YELLOW}Manuelle Installation von Eclipse Temurin JDK 21...${NC}"
    echo ""

    ARCH=$(uname -m)
    case $ARCH in
        x86_64)
            JDK_ARCH="x64"
            ;;
        aarch64)
            JDK_ARCH="aarch64"
            ;;
        *)
            echo -e "${RED}Nicht unterstützte Architektur: $ARCH${NC}"
            exit 1
            ;;
    esac

    JDK_URL="https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.2%2B13/OpenJDK21U-jdk_${JDK_ARCH}_linux_hotspot_21.0.2_13.tar.gz"
    INSTALL_DIR="/opt/java"

    echo "Lade JDK herunter..."
    wget -q --show-progress -O /tmp/openjdk21.tar.gz "$JDK_URL"

    echo "Entpacke nach $INSTALL_DIR..."
    sudo mkdir -p "$INSTALL_DIR"
    sudo tar -xzf /tmp/openjdk21.tar.gz -C "$INSTALL_DIR"
    rm /tmp/openjdk21.tar.gz

    JDK_DIR=$(ls -d $INSTALL_DIR/jdk-21* | head -1)

    echo "Erstelle Symlinks..."
    sudo ln -sf "$JDK_DIR/bin/java" /usr/local/bin/java
    sudo ln -sf "$JDK_DIR/bin/javac" /usr/local/bin/javac
    sudo ln -sf "$JDK_DIR/bin/jar" /usr/local/bin/jar

    # JAVA_HOME setzen
    echo ""
    echo -e "${YELLOW}Füge JAVA_HOME zu ~/.bashrc hinzu...${NC}"
    echo "" >> ~/.bashrc
    echo "# Java 21" >> ~/.bashrc
    echo "export JAVA_HOME=$JDK_DIR" >> ~/.bashrc
    echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc

    export JAVA_HOME="$JDK_DIR"
    export PATH="$JAVA_HOME/bin:$PATH"
}

# Installation basierend auf OS
case $OS in
    debian|ubuntu|linuxmint|pop)
        install_debian_ubuntu
        ;;
    fedora|rhel|centos|rocky|almalinux)
        install_fedora_rhel
        ;;
    arch|manjaro|endeavouros)
        install_arch
        ;;
    opensuse*|sles)
        install_opensuse
        ;;
    *)
        # Prüfe ob debian-basiert
        if [[ "$OS_LIKE" == *"debian"* ]]; then
            install_debian_ubuntu
        elif [[ "$OS_LIKE" == *"rhel"* ]] || [[ "$OS_LIKE" == *"fedora"* ]]; then
            install_fedora_rhel
        elif [[ "$OS_LIKE" == *"arch"* ]]; then
            install_arch
        else
            echo -e "${YELLOW}Unbekanntes OS, versuche manuelle Installation...${NC}"
            install_manual
        fi
        ;;
esac

echo ""
echo -e "${BLUE}========================================"
echo " Installation abgeschlossen!"
echo -e "========================================${NC}"
echo ""

# Verifizieren
if command -v javac &> /dev/null; then
    echo -e "${GREEN}Java Version:${NC}"
    java -version
    echo ""
    echo -e "${GREEN}Java Compiler:${NC}"
    javac -version
    echo ""
    echo -e "${GREEN}Du kannst jetzt das Modul bauen:${NC}"
    echo "  cd v8-9-10 && ./build.sh"
else
    echo -e "${RED}Installation fehlgeschlagen. Bitte manuell installieren.${NC}"
    echo ""
    echo "Manuelle Installation:"
    echo "  1. Lade JDK 21 von https://adoptium.net herunter"
    echo "  2. Entpacke nach /opt/java/"
    echo "  3. Füge zu PATH hinzu: export PATH=/opt/java/jdk-21/bin:\$PATH"
    exit 1
fi
