#!/bin/bash
SERVER="relay.nct.support"
KEY="z+yC5J1KiP88dj7K0hoGncR5pemqL97wp3ctRRF1k+c="
VER="1.3.8"

# Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
    x86_64) PKG="rustdesk-${VER}-x86_64.deb" ;;
    aarch64) PKG="rustdesk-${VER}-aarch64.deb" ;;
    *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

URL="https://github.com/rustdesk/rustdesk/releases/download/${VER}/${PKG}"

echo "Downloading RustDesk ${VER}..."
wget -q -O /tmp/rustdesk.deb "$URL" || { echo "Download failed"; exit 1; }

echo "Installing RustDesk..."
sudo dpkg -i /tmp/rustdesk.deb 2>/dev/null
sudo apt-get install -f -y -q 2>/dev/null
rm -f /tmp/rustdesk.deb

# Configure RustDesk
for DIR in "$HOME/.config/rustdesk" "/root/.config/rustdesk"; do
    sudo mkdir -p "$DIR"
    sudo tee "$DIR/RustDesk.toml" > /dev/null <<EOF
rendezvous_server = '$SERVER'
nat_type = 1
serial = 0

[options]
custom-rendezvous-server = '$SERVER'
relay-server = '$SERVER'
api-server = 'https://$SERVER'
key = '$KEY'
EOF
done

# Enable and start service
sudo systemctl enable rustdesk
sudo systemctl restart rustdesk

sleep 3

# Get device ID
ID=$(rustdesk --get-id 2>/dev/null || echo "Open RustDesk to find your ID")
echo ""
echo "=================================="
echo "  NCT Remote Support Ready"
echo "  Your Device ID: $ID"
echo "  Call NCT Support: (980) 494-3560"
echo "=================================="
