#!/bin/bash
set -euo pipefail

echo "============================================"
echo "  PANAMATECH - SOPORTE REMOTO PARA macOS"
echo "============================================"
echo ""
echo "1) Este script descarga el instalador oficial de RustDesk para macOS."
echo "2) Abre el DMG para que completes la instalacion."
echo "3) Luego contacta a soporte para finalizar la configuracion guiada."
echo ""

TMP_DIR="${TMPDIR:-/tmp}"
DMG_FILE="${TMP_DIR}/rustdesk-macos.dmg"

DOWNLOAD_URL="$(curl -fsSL https://api.github.com/repos/rustdesk/rustdesk/releases/latest \
  | grep browser_download_url \
  | grep -E 'universal\.dmg|x86_64\.dmg|aarch64\.dmg' \
  | head -n1 \
  | cut -d '"' -f4)"

if [ -z "${DOWNLOAD_URL}" ]; then
  echo "No se pudo encontrar el instalador de macOS."
  exit 1
fi

echo "Descargando RustDesk..."
curl -fL "${DOWNLOAD_URL}" -o "${DMG_FILE}"

echo "Abriendo instalador..."
open "${DMG_FILE}"

echo ""
echo "Instalador abierto. Si necesitas ayuda, comparte este link con soporte:"
echo "https://panamatech.com.pa/"
