#!/usr/bin/env bash
# Install RustDesk already pointed at ctrl.bpsystems.ca. No key entry required.
# Official signed release 1.5.0, verified by sha256. Run with sudo.
set -euo pipefail

HOST='ctrl.bpsystems.ca'; KEY='av9g3v3uXO2v9mLuhQJg2H9okVSAlYcHUvRYN5MWxHE='; VER='1.5.0'; CFG='host=ctrl.bpsystems.ca,key=av9g3v3uXO2v9mLuhQJg2H9okVSAlYcHUvRYN5MWxHE='
BASE="https://github.com/rustdesk/rustdesk/releases/download/$VER"

die(){ echo "ERROR: $*" >&2; exit 1; }
[ "$(id -u)" = 0 ] || die "run this with sudo: sudo bash $0"

sha_for(){
  case "$1" in
    rustdesk-1.5.0-x86_64.deb) echo 4e2b9701d25a7ef373f3d09bda584a37d6902f97a6ab15f2e86d4a5783e2a0ec;;
    rustdesk-1.5.0-aarch64.deb) echo 1f2fc9be8321c10347aa33cb13198c6a956aec272c5e58ac81f4c0ef04037df3;;
    rustdesk-1.5.0-0.x86_64.rpm) echo 96e4246d62a992630dacac3472f356ad517a5ef6d0cd8ea02b5648bc1cabff26;;
    rustdesk-1.5.0-0.aarch64.rpm) echo 1b436234e51ca59bfd3beba6ba81096be13a22a7e2f8b94bd45218b8c77e5e13;;
    rustdesk-1.5.0-x86_64.dmg) echo 1ca3cbfbe7f2bd28b50c93fcd50d4a0b671dc76d18be27cea99d3a65767eb526;;
    rustdesk-1.5.0-aarch64.dmg) echo 3929b0a4321e7d0f561a317059798be882d65decc59681eb77061f8efe56fbf4;;
    rustdesk-1.5.0-x86_64.msi) echo 624fe792ab1d76b35134dab84bff97a6bcd14043ffd090a2c6a2800f77580495;;
    rustdesk-1.5.0-aarch64.msi) echo ec4dc1afa5ae3d6a8ecceab1a681bfd17ed56400056c8fa1c182decf43eab1f7;;
    *) echo "";;
  esac
}

case "$(uname -m)" in
  x86_64|amd64) ARCH=x86_64 ;;
  aarch64|arm64) ARCH=aarch64 ;;
  *) die "unsupported architecture: $(uname -m)" ;;
esac

if   command -v apt-get >/dev/null 2>&1; then ASSET="rustdesk-$VER-$ARCH.deb";   INSTALL="apt-get install -y"
elif command -v dnf     >/dev/null 2>&1; then ASSET="rustdesk-$VER-0.$ARCH.rpm"; INSTALL="dnf install -y"
elif command -v zypper  >/dev/null 2>&1; then ASSET="rustdesk-$VER-0.$ARCH.rpm"; INSTALL="zypper --non-interactive install --allow-unsigned-rpm"
elif command -v yum     >/dev/null 2>&1; then ASSET="rustdesk-$VER-0.$ARCH.rpm"; INSTALL="yum install -y"
else die "no supported package manager found (apt/dnf/zypper/yum)"
fi

SUM="$(sha_for "$ASSET")"
[ -n "$SUM" ] || die "no pinned checksum for $ASSET -- regenerate this installer"

TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
echo "==> downloading $ASSET"
curl -fsSL -o "$TMP/$ASSET" "$BASE/$ASSET" || die "download failed"
echo "==> verifying sha256"
printf '%s  %s\n' "$SUM" "$TMP/$ASSET" | sha256sum -c - >/dev/null \
  || die "CHECKSUM MISMATCH -- do not install this file"

echo "==> installing"
$INSTALL "$TMP/$ASSET" >/dev/null 2>&1 || $INSTALL "$TMP/$ASSET" || die "package install failed"
command -v rustdesk >/dev/null 2>&1 || die "rustdesk is not on PATH after install"

# Apply to root (the service) AND to the desktop user (the GUI reads its own
# config), or the GUI would still come up unconfigured.
echo "==> applying server config"
rustdesk --config "$CFG" >/dev/null 2>&1 || true
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != root ]; then
  su - "$SUDO_USER" -c "rustdesk --config '$CFG'" >/dev/null 2>&1 || true
fi

# Verify by FINDING the config file rather than assuming its path, and require
# both values: a file that exists proves nothing about what is in it.
echo "==> verifying"
ok=0
while IFS= read -r f; do
  if grep -qF "$HOST" "$f" 2>/dev/null && grep -qF "$KEY" "$f" 2>/dev/null; then
    ok=1; echo "    configured: $f"
  fi
done < <(find /root /home -name RustDesk2.toml 2>/dev/null)
[ "$ok" = 1 ] || die "the server config did NOT apply -- not reporting success"

systemctl restart rustdesk >/dev/null 2>&1 || true
echo
echo "Done. RustDesk is installed and pointed at $HOST."
echo "Open RustDesk and read out your ID; you will be given a one-time code when someone connects."
