#!/usr/bin/env bash
set -euo pipefail

script_url='https://support.obergodmar.ru/scripts/macos_support.sh'
command_file="${TMPDIR:-/tmp}/rustdesk-support.command"

cat >"$command_file" <<EOF
#!/usr/bin/env bash

script_url='$script_url'
script_path="\${TMPDIR:-/tmp}/rustdesk-support.sh"

echo "Downloading RustDesk support script..."
curl -fsSL "\$script_url" -o "\$script_path"
download_status=\$?

if [[ "\$download_status" -ne 0 ]]; then
  echo "Failed to download RustDesk support script."
  echo
  read -r -p "Press Enter to close..."
  exit "\$download_status"
fi

chmod +x "\$script_path"
bash "\$script_path"
exit_status=\$?

echo
read -r -p "Press Enter to close..."
exit "\$exit_status"
EOF

chmod 700 "$command_file"
open -a Terminal "$command_file"
