#!/bin/bash
URL="http://127.0.0.1:8787"
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"

# ask for admin rights and launch MacBlue as root (native password dialog)
TOKEN="$(/usr/bin/osascript -e "do shell script quoted form of \"$RES/run-root.sh\" with administrator privileges")" || exit 1
[ -n "$TOKEN" ] || exit 1

# wait for the server (first launch of the compiled binary can take a few
# seconds to initialize), then open the dashboard
for i in $(seq 1 50); do
  [ "$(/usr/bin/curl -fsS --max-time 1 "$URL/health" 2>/dev/null)" = '{"ok": true}' ] && break
  sleep 0.4
done
open "$URL/?access=$TOKEN"
