#!/usr/bin/env bash
set -e
DOMAIN="'"$DOMAIN"'"
KEY="'"$REAL_KEY"'"

# Write config
CONF="$HOME/.config/rustdesk/RustDesk2.toml"
mkdir -p "$(dirname "$CONF")"
cat >"$CONF" <<TOML
[network]
id_server = "$DOMAIN"
relay_server = "$DOMAIN"

[options]
key = "$KEY"
TOML

# Restart if running
pkill -x rustdesk 2>/dev/null || true

# Try Debian/Ubuntu install
if command -v apt >/dev/null 2>&1; then
  TMP="$(mktemp)"
  curl -L "https://$DOMAIN/downloads/$LIN_DEB" -o "$TMP"
  sudo apt install -y "$TMP" || sudo dpkg -i "$TMP"
  rm -f "$TMP"
  echo "Installed RustDesk .deb and applied config."
else
  echo "Applied config. Please install RustDesk for your distro:"
  echo "  - Download: https://$DOMAIN/downloads/"
  echo "  - Or from your distro repos/appimage/flatpak."
fi
echo "Launch RustDesk and connect via $DOMAIN (key already set)."
