#!/bin/sh
# Repo-friendly launcher for daylily-carrier-tracking.
#
# This wrapper is convenient for repo checkout usage without installation.
# It is also venv-friendly:
#   - If ./.venv exists, we *always* run with ./.venv/bin/python
#   - Otherwise we fall back to python3/python on PATH (and emit a note)
#
# Recommended dev workflow (avoids polluting system Python):
#   ./dev-setup.sh
#   ./tday --help

set -eu

REPO_ROOT=$(CDPATH= cd "$(dirname "$0")" && pwd)

PY=""
USING_VENV=0
if [ -x "$REPO_ROOT/.venv/bin/python" ]; then
  PY="$REPO_ROOT/.venv/bin/python"
  USING_VENV=1
elif command -v python3 >/dev/null 2>&1; then
  PY="$(command -v python3)"
elif command -v python >/dev/null 2>&1; then
  PY="$(command -v python)"
else
  echo "ERROR: Could not find python3/python in PATH" 1>&2
  exit 127
fi

if [ "$USING_VENV" -ne 1 ]; then
  echo "NOTE: .venv not found; using system python ($PY). Run ./dev-setup.sh for an isolated dev env." >&2
fi

# If not installed, importing the package needs the repo on PYTHONPATH.
if [ -z "${PYTHONPATH:-}" ]; then
  PYTHONPATH="$REPO_ROOT"
else
  PYTHONPATH="$REPO_ROOT:$PYTHONPATH"
fi
export PYTHONPATH

exec "$PY" -c 'import sys; sys.argv[0] = "tday"; from daylily_carrier_tracking.cli import main; raise SystemExit(main())' "$@"

