#!/usr/bin/env bash
# Every job starts here, so the systemd template never hardcodes an interpreter.
#
# This exists because fitness-data-sync runs under its own virtualenv
# (~/.fitness-sync/venv) for the Strava and WHOOP client libraries. A shared
# service template pointing at /usr/bin/python3 could never run it, and adding
# a second template for "jobs with dependencies" is exactly the split
# architecture we are avoiding. One wrapper, one template, one way in.
set -euo pipefail

JOB="${1:?usage: run-job <name> [args...]}"
shift

cd "$HOME/automation"

# Prefer the repo's own virtualenv when it exists, so jobs with third-party
# dependencies and jobs without are launched identically.
if [ -x ".venv/bin/python" ]; then
    PYTHON=".venv/bin/python"
else
    # `python3` from PATH, never /usr/bin/python3 hardcoded: on macOS that is
    # an old system Python that cannot parse modern type hints, so the jobs
    # ran on the box and crashed on the Mac.
    PYTHON="$(command -v python3)"
fi

exec "$PYTHON" -m "jobs.$JOB" "$@"
