#!/bin/sh
# RemoteMyDesktop Linux installer.
# Usage: curl -fsSL https://remotemydesktop.com/install.sh | sh
# Downloads the 0.2.15 beta package that matches your distribution, over HTTPS, checks its SHA-256
# against the published SHA256SUMS (the same hashes shown on the download page), and installs it.
#
# Everything runs from main(), called on the last line: a download cut short cannot run a
# half-received script.
set -eu

VER="0.2.15"
BASE="https://remotemydesktop.com/downloads"

fetch() { # fetch <url> <out>
  if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2"
  elif command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1"
  else echo "Need curl or wget to download." >&2; exit 1; fi
}

# verify <dir> <file>: the file's SHA-256 must be the one SHA256SUMS lists for its name.
verify() {
  dir="$1"; name="$2"
  fetch "$BASE/SHA256SUMS" "$dir/SHA256SUMS"
  expected="$(awk -v n="$name" '$2 == n || $2 == "*" n { print $1 }' "$dir/SHA256SUMS" | head -n 1)"
  if [ -z "$expected" ]; then
    echo "SHA256SUMS lists no hash for $name; not installing." >&2; exit 1
  fi
  if command -v sha256sum >/dev/null 2>&1; then actual="$(sha256sum "$dir/$name" | awk '{ print $1 }')"
  elif command -v shasum >/dev/null 2>&1; then actual="$(shasum -a 256 "$dir/$name" | awk '{ print $1 }')"
  elif command -v openssl >/dev/null 2>&1; then actual="$(openssl dgst -sha256 "$dir/$name" | awk '{ print $NF }')"
  else echo "Need sha256sum, shasum or openssl to verify the download." >&2; exit 1; fi
  if [ "$(printf '%s' "$actual" | tr 'A-F' 'a-f')" != "$(printf '%s' "$expected" | tr 'A-F' 'a-f')" ]; then
    echo "$name does not match its published SHA-256; not installing." >&2
    echo "  expected $expected" >&2
    echo "  got      $actual" >&2
    exit 1
  fi
  echo "SHA-256 verified."
}

main() {
  arch="$(uname -m 2>/dev/null || echo unknown)"
  case "$arch" in
    x86_64|amd64) : ;;
    *) echo "RemoteMyDesktop $VER is x86_64 only (your CPU: $arch)." >&2; exit 1 ;;
  esac

  sudo_cmd=""
  if [ "$(id -u)" -ne 0 ]; then
    if command -v sudo >/dev/null 2>&1; then sudo_cmd="sudo"
    else echo "Please run this as root, or install sudo first." >&2; exit 1; fi
  fi

  tmp="$(mktemp -d)"
  chmod 755 "$tmp"  # let the _apt sandbox user read the downloaded .deb
  trap 'rm -rf "$tmp"' EXIT INT TERM

  if command -v apt-get >/dev/null 2>&1; then
    f="RemoteMyDesktop_${VER}_amd64.deb"
    echo "Downloading $f ..."; fetch "$BASE/$f" "$tmp/$f"; verify "$tmp" "$f"
    echo "Installing with apt ..."; $sudo_cmd apt-get install -y "$tmp/$f"
  elif command -v dnf >/dev/null 2>&1; then
    f="RemoteMyDesktop-${VER}-1.x86_64.rpm"
    echo "Downloading $f ..."; fetch "$BASE/$f" "$tmp/$f"; verify "$tmp" "$f"
    # The beta packages carry no GPG signature yet; the SHA-256 check above stands in for it.
    echo "Installing with dnf ..."; $sudo_cmd dnf install -y --nogpgcheck "$tmp/$f"
  elif command -v zypper >/dev/null 2>&1; then
    f="RemoteMyDesktop-${VER}-1.x86_64.rpm"
    echo "Downloading $f ..."; fetch "$BASE/$f" "$tmp/$f"; verify "$tmp" "$f"
    echo "Installing with zypper ..."; $sudo_cmd zypper --non-interactive install --allow-unsigned-rpm "$tmp/$f"
  elif command -v yum >/dev/null 2>&1; then
    f="RemoteMyDesktop-${VER}-1.x86_64.rpm"
    echo "Downloading $f ..."; fetch "$BASE/$f" "$tmp/$f"; verify "$tmp" "$f"
    echo "Installing with yum ..."; $sudo_cmd yum install -y --nogpgcheck "$tmp/$f"
  else
    f="RemoteMyDesktop_${VER}_amd64.AppImage"
    echo "No apt/dnf/zypper/yum found. Installing the AppImage to /usr/local/bin ..."
    fetch "$BASE/$f" "$tmp/$f"; verify "$tmp" "$f"
    $sudo_cmd install -m 0755 "$tmp/$f" /usr/local/bin/remotemydesktop
    echo "Installed to /usr/local/bin/remotemydesktop (needs FUSE to run)."
  fi

  echo "RemoteMyDesktop $VER installed. Launch it from your applications menu, or run: remotemydesktop"
}

main "$@"
