#!/bin/bash
set -euo pipefail
SERVER="remote.tropia.org"
KEY="IiCKFsD6zFc768BKuZVZquXDGpAGLo1Fkl6DUKcMi8s="
VERSION="1.4.9"
CONFIG="PASTE_EXPORTED_ENCRYPTED_CONFIG_STRING_HERE"
APP="/Applications/RustDesk.app"
if [[ "$SERVER" == __* || "$KEY" == __* ]]; then
  osascript -e 'display alert "Noch nicht konfiguriert" message "Bitte Support kontaktieren." as critical'
  exit 1
fi
if [[ ! -d "$APP" ]]; then
  url="https://github.com/rustdesk/rustdesk/releases/download/${VERSION}/rustdesk-${VERSION}-x86_64.dmg"
  [[ "$(uname -m)" == arm64 ]] && url="https://github.com/rustdesk/rustdesk/releases/download/${VERSION}/rustdesk-${VERSION}-aarch64.dmg"
  open "$url"
  osascript -e 'display alert "RustDesk installieren" message "Download öffnen, RustDesk nach Programme ziehen und dieses Startprogramm danach erneut öffnen."'
  exit 0
fi
open "$APP"
if [[ "$CONFIG" != __* && "$CONFIG" != PASTE_* ]]; then
  osascript -e 'display dialog "Zur sicheren Server-Konfiguration benötigt macOS einmalig Ihr Administratorpasswort." buttons {"Abbrechen", "Fortfahren"} default button "Fortfahren" with icon caution' >/dev/null || exit 0
  sudo "$APP/Contents/MacOS/RustDesk" --config "$CONFIG"
fi
open "$APP"
osascript -e 'display alert "Fernhilfe freigeben" message "Erlauben Sie Bildschirmaufnahme und Bedienungshilfen. Teilen Sie nur die angezeigte ID und bestätigen Sie jede Sitzung bewusst."'
printf 'ID-Server: %s\nKey: %s\n' "$SERVER" "$KEY"
open 'x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture' || true
