#!/bin/sh

# Run a Python script, setting up PYTHONPATH for access to test/common and the
# python libraries in bots/.  Checks out the bots first, if necessary.

# This is intended to be used from the interpreter line of executable Python
# scripts, referring to it with a relative path.  The interpreter line should
# look something like so:

  #!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../test/common/pywrap", sys.argv)

# with the `/../test/common/` part determined by the location of the script
# relative to this script.

set -eu

realpath="$(realpath "$0")"
top_srcdir="${realpath%/*}/../.."

# Check out the bots if required
test -d "${top_srcdir}/bots" || "${top_srcdir}/test/common/make-bots"

# Prepend the path
PYTHONPATH="${top_srcdir}/test/common:${top_srcdir}/bots:${top_srcdir}/bots/machine${PYTHONPATH:+:${PYTHONPATH}}"
export PYTHONPATH

# Run the script
# -B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
# -P     : don't prepend a potentially unsafe path to sys.path -- but not available in RHEL 8/9 yet, use once we can
exec python3 -B "$@"
