#!/bin/sh
set -eu
umask 077
if [ "${1:-}" = --home ]; then
    [ "$#" -ge 3 ] || { echo 'usage: trackme-broker --home DIRECTORY COMMAND' >&2; exit 2; }
    TRACKME_BROKER_HOME=$2
elif [ -z "${TRACKME_BROKER_HOME:-}" ]; then
    TRACKME_BROKER_LAUNCHER_HOME=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd -P)
    export TRACKME_BROKER_LAUNCHER_HOME
fi
root=${TRACKME_BROKER_HOME:-${TRACKME_BROKER_LAUNCHER_HOME:-/opt/trackme-broker}}
# lib/current and etc/python switch together. Run the venv python regardless of PATH.
release=$(CDPATH='' cd -- "$root/lib/current" && pwd -P)
if [ -f "$release/.host-python" ]; then
    IFS= read -r python < "$release/.host-python"
else
    python=$release/bin/python
fi
"$python" -I -c 'import sys; sys.exit(0 if sys.implementation.name == "cpython" and (3,11) <= sys.version_info[:2] <= (3,14) else "trackme-broker needs CPython 3.11 to 3.14 (found %s)" % sys.version.split()[0])'
exec "$release/bin/python" -IBu -m trackme_broker_standalone "$@"
