#!/bin/bash
# Element Desktop Setup Script for Linux
# Ausführung: chmod +x element-setup-linux.sh && ./element-setup-linux.sh

echo "========================================"
echo "Element Desktop Konfiguration Setup"
echo "========================================"
echo ""

# Erkenne Linux Distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    OS=$(uname -s)
fi

# Config-Verzeichnis
CONFIG_DIR="$HOME/.config/Element"
CONFIG_FILE="$CONFIG_DIR/config.json"

# Erstelle Verzeichnis falls nicht vorhanden
if [ ! -d "$CONFIG_DIR" ]; then
    echo "Erstelle Element Verzeichnis..."
    mkdir -p "$CONFIG_DIR"
fi

# Erstelle config.json
echo "Erstelle config.json..."
cat > "$CONFIG_FILE" << 'EOF'
{
  "configUrl": "https://axion1337.chat/config.json",
  "brand": "aXion1337.Chat",
  "default_theme": "aXion1337 Dark",
  "show_labs_settings": true,
  "features": {
    "feature_qr_code_login": true
  },
  "setting_defaults": {
    "custom_themes": []
  }
}
EOF

echo "✅ Config erstellt: $CONFIG_FILE"
echo ""

# Versuche Element zu starten/installieren
echo "Überprüfe Element Desktop Installation..."

# Methode 1: Element ist bereits installiert
if command -v element &> /dev/null; then
    echo "Element gefunden. Starte Element..."
    element &
    sleep 2
    exit 0
fi

# Methode 2: Apt (Debian/Ubuntu)
if command -v apt &> /dev/null; then
    echo ""
    echo "Installiere Element über apt..."
    sudo apt update
    sudo apt install -y element-desktop
    echo "Starte Element..."
    element &
    exit 0
fi

# Methode 3: Pacman (Arch Linux)
if command -v pacman &> /dev/null; then
    echo ""
    echo "Installiere Element über pacman..."
    sudo pacman -S --noconfirm element-web
    echo "Starte Element..."
    element &
    exit 0
fi

# Methode 4: DNF (Fedora/RHEL)
if command -v dnf &> /dev/null; then
    echo ""
    echo "Installiere Element über dnf..."
    sudo dnf install -y element-desktop
    echo "Starte Element..."
    element &
    exit 0
fi

# Methode 5: Zypper (openSUSE)
if command -v zypper &> /dev/null; then
    echo ""
    echo "Installiere Element über zypper..."
    sudo zypper install -y element-desktop
    echo "Starte Element..."
    element &
    exit 0
fi

# Fallback
echo ""
echo "⚠️  Element Desktop konnte nicht automatisch installiert werden."
echo ""
echo "Bitte installiere Element Desktop manuell:"
echo "  Ubuntu/Debian:  sudo apt install element-desktop"
echo "  Fedora/RHEL:    sudo dnf install element-desktop"
echo "  Arch:           sudo pacman -S element-web"
echo "  openSUSE:       sudo zypper install element-desktop"
echo ""
echo "Oder: https://element.io/download"
echo ""
echo "✅ Deine config.json wurde erstellt unter:"
echo "$CONFIG_FILE"
echo ""
