#!/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"
APP=/Applications/RustDesk.app
BIN="$APP/Contents/MacOS/RustDesk"

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
  arm64) ASSET="rustdesk-$VER-aarch64.dmg" ;;
  x86_64) ASSET="rustdesk-$VER-x86_64.dmg" ;;
  *) die "unsupported architecture: $(uname -m)" ;;
esac

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

TMP="$(mktemp -d)"; MNT="$TMP/mnt"; mkdir -p "$MNT"
cleanup(){ hdiutil detach "$MNT" >/dev/null 2>&1 || true; rm -rf "$TMP"; }
trap cleanup EXIT

echo "==> downloading $ASSET"
curl -fsSL -o "$TMP/$ASSET" "$BASE/$ASSET" || die "download failed"
echo "==> verifying sha256"
printf '%s  %s\n' "$SUM" "$TMP/$ASSET" | shasum -a 256 -c - >/dev/null \
  || die "CHECKSUM MISMATCH -- do not install this file"

echo "==> installing"
hdiutil attach -nobrowse -quiet -mountpoint "$MNT" "$TMP/$ASSET" || die "could not mount the dmg"
rm -rf "$APP"
cp -R "$MNT/RustDesk.app" /Applications/ || die "could not copy RustDesk.app"
hdiutil detach "$MNT" >/dev/null 2>&1 || true
xattr -dr com.apple.quarantine "$APP" 2>/dev/null || true
[ -x "$BIN" ] || die "RustDesk.app is not where expected after install"

echo "==> applying server config"
"$BIN" --config "$CFG" >/dev/null 2>&1 || true
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != root ]; then
  su - "$SUDO_USER" -c "'$BIN' --config '$CFG'" >/dev/null 2>&1 || true
fi

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 /var/root /Users -name RustDesk2.toml 2>/dev/null)
[ "$ok" = 1 ] || die "the server config did NOT apply -- not reporting success"

echo
echo "Done. RustDesk is installed and pointed at $HOST."
echo "Open RustDesk from Applications and read out your ID."
