#!/bin/bash
# Pinkas launcher (the CFBundleExecutable of Pinkas.app).
# Starts the background server, or just opens the browser if it is already running, then exits.
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
APP_BUNDLE="$(cd "$RES/../.." && pwd)"

# Until the app is moved out of the folder it was unzipped in, macOS runs a downloaded app from a
# random read-only location ("App Translocation"). Ask the user to move it instead of failing.
case "$APP_BUNDLE" in
  */AppTranslocation/*)
    /usr/bin/osascript -e 'display dialog "כדי שפנקס יעבוד, גררו את Pinkas לתיקיית Applications (יישומים) ופתחו אותו משם." buttons {"אישור"} default button 1 with title "Pinkas"' >/dev/null 2>&1
    exit 0
    ;;
esac

# Files from a downloaded archive are quarantined, and macOS then blocks the bundled Node.js and
# Prisma engine. The user has already chosen to open the app, so clear the flag on our own files.
/usr/bin/xattr -dr com.apple.quarantine "$APP_BUNDLE" 2>/dev/null

"$RES/runtime/node" "$RES/app/bootstrap.mjs" >/dev/null 2>&1 &
disown
exit 0
