#!/bin/bash
# evoads launcher: install the background service once (native admin prompt), then open the evoads page.
DIR="$(cd "$(dirname "$0")" && pwd)"
case "$(uname -m)" in arm64) BIN="$DIR/evoads-bin-arm64" ;; *) BIN="$DIR/evoads-bin-amd64" ;; esac
xattr -dr com.apple.quarantine "$DIR/../.." >/dev/null 2>&1; chmod +x "$BIN" 2>/dev/null
PORTF="/Library/Application Support/evoads/ui-port"
port() { [ -r "$PORTF" ] && cat "$PORTF" || echo 5380; }
up() { curl -s -m 1 "http://127.0.0.1:$(port)/api/status" 2>/dev/null | grep -q '"app":"evoads-desktop"'; }
if ! up; then
  if ! osascript -e "do shell script \"'$BIN' install\" with administrator privileges" >/dev/null 2>&1; then
    osascript -e 'display alert "evoads could not be installed" message "Administrator rights are needed to install the ad-blocking service. Please try again and enter your Mac password."'
    exit 1
  fi
  for i in $(seq 1 30); do up && break; sleep 0.5; done
fi
open "http://127.0.0.1:$(port)/"
