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

CONFIG='==Qfi0zd6BFa2UVRMNVQYFzYz8UdadERXJWaKJzQTdlZGFFM2lzc4kVRvlWQrc1TiojI5V2aiwiIiojIpBXYiwiIt92YuQXatIXZu5Wa0NnLrNXZkR3c1JnI6ISehxWZyJCLi02bj5Cdp1icl5mbpR3cus2clRGdzVnciojI0N3boJye'
DEB_URL='https://remote.stinner-it.com/downloads/rustdesk-1.4.6-x86_64.deb'
DEB_FILE='/tmp/rustdesk-1.4.6-x86_64.deb'

echo "Stinner IT Solutions e.U. - RustDesk Linux setup"
echo "Downloading RustDesk..."
curl -L "$DEB_URL" -o "$DEB_FILE"

echo "Installing RustDesk..."
if command -v apt >/dev/null 2>&1; then
  sudo apt update
  sudo apt install -y "$DEB_FILE"
elif command -v apt-get >/dev/null 2>&1; then
  sudo apt-get update
  sudo apt-get install -y "$DEB_FILE"
else
  echo "This installer currently supports Debian/Ubuntu based systems only."
  echo "Please install the DEB package manually or contact Stinner IT Solutions support."
  exit 1
fi

echo "Applying Stinner IT remote support configuration..."
if command -v rustdesk >/dev/null 2>&1; then
  rustdesk --config "$CONFIG" || true
elif [ -x /usr/bin/rustdesk ]; then
  /usr/bin/rustdesk --config "$CONFIG" || true
else
  echo "RustDesk was installed, but the rustdesk command was not found."
  echo "Please open RustDesk manually and contact support if it does not show Ready."
fi

echo ""
echo "Done."
echo "Please open RustDesk and share the displayed ID and one-time password with our support team."
