#!/bin/sh
# DAP-Share.app: find Python 3, start the agent in the background and end, so that opening the app
# again finds the agent running and only opens its screen. The agent is stopped from its screen.
HERE="$(cd "$(dirname "$0")/../Resources" && pwd)"
PATH="$PATH:/usr/local/bin:/opt/homebrew/bin:/Library/Frameworks/Python.framework/Versions/Current/bin"
export PATH
PY="$(command -v python3)"
if [ -z "$PY" ]; then
  osascript -e 'display dialog "DAP-Share needs Python 3. Install it from python.org, then open DAP-Share again." buttons {"OK"} default button 1 with title "DAP-Share"' >/dev/null 2>&1
  open "https://www.python.org/downloads/macos/" >/dev/null 2>&1
  exit 1
fi
if [ "${DAP_SHARE_LOG:-}" = "-" ]; then
  exec "$PY" "$HERE/dap-share.pyz" start --relay relay.dapshare.app:8090 "$@"
fi
LOG="${DAP_SHARE_LOG:-$HOME/.dap-share/agent.log}"
mkdir -p "$(dirname "$LOG")"
nohup "$PY" "$HERE/dap-share.pyz" start --relay relay.dapshare.app:8090 "$@" >>"$LOG" 2>&1 </dev/null &
exit 0
