#!/bin/bash
# =========================================
# DGITel Remote Support - Linux Launcher
# =========================================
# Doppelklicken (oder Terminal) → neueste RustDesk-Version wird geladen → verbindet mit DGITel-Server
# Keine Root-Rechte nötig

# ============================================================
# KONFIGURATION — nur das musst du anpassen:
# ============================================================
RUSTDESK_CFG="==Qfi0DO0VzZVJGbBF0bWNzbvBlSpVEOTZmWxV3dG9WarJURmNWOCllYDlWawcmdiojI5V2aiwiIiojIpBXYiwiI0VmbuwWZ0l2Zk5SZ09WblJnI6ISehxWZyJCLiQXZu5CblRXanRmLlR3btVmciojI0N3boJye"

# ============================================================
# AB HIER NICHTS MEHR ÄNDERN
# ============================================================

echo "DGITel Remote Support wird gestartet..."
echo "Lade neueste RustDesk-Version..."

# Neueste Version ermitteln
VERSION=$(curl -sI https://github.com/rustdesk/rustdesk/releases/latest | grep -i location | grep -oE 'tag/[^/]+' | cut -d'/' -f2 | tr -d '\r' | tr -d '[:space:]')

if [ -z "$VERSION" ]; then
    echo "FEHLER: Konnte Version nicht ermitteln. Prüfe Internetverbindung."
    echo "Alternativ: https://rustdesk.com/download"
    sleep 10
    exit 1
fi

echo "Version: $VERSION"

APPIMAGE_URL="https://github.com/rustdesk/rustdesk/releases/download/${VERSION}/rustdesk-${VERSION}-x86_64.AppImage"
APPIMAGE_PATH="/tmp/rustdesk-${VERSION}.AppImage"

# Nur downloaden wenn nicht schon vorhanden
if [ ! -f "$APPIMAGE_PATH" ]; then
    echo "Download läuft..."
    rm -f /tmp/rustdesk-*.AppImage 2>/dev/null
    curl -L -o "$APPIMAGE_PATH" "$APPIMAGE_URL" --progress-bar
    if [ $? -ne 0 ]; then
        echo "Download fehlgeschlagen. Prüfe deine Internetverbindung."
        sleep 10
        exit 1
    fi
    chmod +x "$APPIMAGE_PATH"
fi

echo "Starte RustDesk mit DGITel-Serverkonfiguration..."
"$APPIMAGE_PATH" --config "$RUSTDESK_CFG" &

echo ""
echo "RustDesk wurde gestartet. Gib die angezeigte ID an den DGITel-Support weiter."
echo "Fenster schließt in 10 Sekunden..."
sleep 10
