#!/usr/bin/env bash

# Find current file directory
# supporting symlinks
WEX_DIR_CLI="$(realpath "$(dirname "$(readlink -f ${BASH_SOURCE[0]})")")/"
WEX_ROOT="$(realpath "${WEX_DIR_CLI}../")/"

# Source .env file if it exists
if [ -f "${WEX_ROOT}/.env" ]; then
    . "${WEX_ROOT}/.env"
fi

. "${WEX_DIR_CLI}/_init.sh"

WEX_REQUEST_ID="$(date '+%Y%m%d-%H%M%S-%N')-$$"

# Allow to override python executor
WEX_INTERPRETER=${WEX_INTERPRETER:-${WEX_ROOT}.venv/bin/python}

# exec, not a child call: bash defers its signal handlers until the foreground
# command returns, so a long-running command — `ai::agent/serve` as a container's
# only process — would never see the SIGTERM of a `docker stop` and would be
# killed after the grace period, mid-turn. Replacing the shell puts Python at
# PID 1, where the signal reaches it.
exec ${WEX_INTERPRETER} "${WEX_ROOT}__main__.py" --force-request-id "${WEX_REQUEST_ID}" "${@}"

