#!/usr/bin/env bash
set -e

echo "Benchmark Computers Remote Support"
echo

DEB_URL="https://help.benchmarkcomputers.ca/downloads/rustdesk.deb"
DEB_FILE="/tmp/rustdesk-benchmark.deb"
CONFIG_DIR="$HOME/.config/rustdesk"
CONFIG_FILE="$CONFIG_DIR/RustDesk2.toml"

if command -v rustdesk >/dev/null 2>&1; then
  echo "RustDesk is already installed."
else
  echo "Downloading RustDesk..."
  curl -L "$DEB_URL" -o "$DEB_FILE"

  echo "Installing RustDesk..."
  sudo apt install -y "$DEB_FILE"
fi

echo "Applying Benchmark server settings..."
mkdir -p "$CONFIG_DIR"

cat > "$CONFIG_FILE" <<'CONFIGEOF'
rendezvous_server = 'help.benchmarkcomputers.ca'
nat_type = 1
serial = 0
unlock_pin = ''
trusted_devices = ''

[options]
custom-rendezvous-server = 'help.benchmarkcomputers.ca'
relay-server = 'help.benchmarkcomputers.ca'
api-server = ''
key = '49NCMnBbTKeELwoj6jugQn6D6Qbx2bLaciO0D5R1wKg='
CONFIGEOF

echo "Starting RustDesk..."
rustdesk >/dev/null 2>&1 &

echo
echo "Please provide the ID shown on screen to Benchmark Computers."
