#!/bin/bash
clear
echo "============================================================"
echo "  Installazione Assistenza Remota - Informatica Gelormini"
echo "============================================================"
echo ""

# Detect architecture and distro
ARCH=$(uname -m)
TMPDIR=$(mktemp -d)

# Detect package manager
if command -v apt-get &>/dev/null; then
    PKG_TYPE="deb"
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
    PKG_TYPE="rpm"
else
    PKG_TYPE="unknown"
fi

if [ "$ARCH" = "x86_64" ]; then
    if [ "$PKG_TYPE" = "deb" ]; then
        URL="https://github.com/rustdesk/rustdesk/releases/download/1.4.6/rustdesk-1.4.6-x86_64.deb"
        PKG="$TMPDIR/rustdesk.deb"
    elif [ "$PKG_TYPE" = "rpm" ]; then
        URL="https://github.com/rustdesk/rustdesk/releases/download/1.4.6/rustdesk-1.4.6-0.x86_64.rpm"
        PKG="$TMPDIR/rustdesk.rpm"
    fi
elif [ "$ARCH" = "aarch64" ]; then
    if [ "$PKG_TYPE" = "deb" ]; then
        URL="https://github.com/rustdesk/rustdesk/releases/download/1.4.6/rustdesk-1.4.6-aarch64.deb"
        PKG="$TMPDIR/rustdesk.deb"
    fi
fi

if [ -z "$URL" ]; then
    echo "ERRORE: Architettura $ARCH o distribuzione non supportata."
    echo "Scarica manualmente da: https://github.com/rustdesk/rustdesk/releases"
    exit 1
fi

echo "Sistema: $ARCH / $PKG_TYPE"
echo "Scaricamento RustDesk in corso..."
curl -L -o "$PKG" "$URL"

if [ ! -f "$PKG" ]; then
    echo "ERRORE: Download fallito. Controlla la connessione internet."
    exit 1
fi

echo "Download completato."
echo ""
echo "Installazione in corso (richiede password sudo)..."

if [ "$PKG_TYPE" = "deb" ]; then
    sudo apt-get install -y "$PKG"
elif [ "$PKG_TYPE" = "rpm" ]; then
    sudo dnf install -y "$PKG" 2>/dev/null || sudo yum install -y "$PKG"
fi

echo ""
echo "Configurazione server assistenza..."

# Configure RustDesk - scrivi in entrambe le posizioni
CFG_CONTENT="rendezvous_server = 'informaticagelormini.ddns.net'
nat_type = 1
serial = 0

[options]
custom-rendezvous-server = 'informaticagelormini.ddns.net'
relay-server = 'informaticagelormini.ddns.net'
key = 'PEQMCbxAg6WVxPDEd7YN6emBoeekWLtBi7kAeKhLopU='
enable-audio = 'Y'"

# 1. Config utente
CFG_DIR="$HOME/.config/rustdesk/config"
mkdir -p "$CFG_DIR"
echo "$CFG_CONTENT" > "$CFG_DIR/RustDesk2.toml"

# 2. Config root/service
sudo mkdir -p /root/.config/rustdesk/config 2>/dev/null
echo "$CFG_CONTENT" | sudo tee /root/.config/rustdesk/config/RustDesk2.toml >/dev/null 2>/dev/null

# 3. Config in /etc (systemd service)
sudo mkdir -p /etc/rustdesk 2>/dev/null
echo "$CFG_CONTENT" | sudo tee /etc/rustdesk/RustDesk2.toml >/dev/null 2>/dev/null

# Restart RustDesk service and app
sudo systemctl restart rustdesk 2>/dev/null
killall rustdesk 2>/dev/null
sleep 2
nohup rustdesk >/dev/null 2>&1 &

# Cleanup
rm -rf "$TMPDIR"

echo ""
echo "============================================================"
echo ""
echo "  INSTALLAZIONE COMPLETATA!"
echo ""
echo "  RustDesk e stato installato e configurato automaticamente."
echo "  Comunica il tuo ID al tecnico per ricevere assistenza."
echo ""
echo "  Informatica Gelormini - Tel: 339 288 5800"
echo ""
echo "============================================================"
echo ""
read -p "Premi INVIO per chiudere..."
