#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# The GPU queue's CLI (docs/gpu-queue.md): `scripts/gpu submit ...`, `scripts/gpu list`.
# Runs under the first interpreter with tomllib and tarfile extraction filters (3.11.4+); python3 alone may be older.
# Which kubeconfig it uses is decided in ONE place, scripts/gpu_queue_kube.py: GPU_QUEUE_KUBECONFIG_FILE, else
# ~/.kube/gpu-queue.kubeconfig when it exists, else the current KUBECONFIG; a kubeconfig that points only at the
# MicroK8s API (no dev GPU pool) is refused by name. An agent shell does not source ~/.profile, so do not rely on
# exports there.
for py in python3.13 python3.12 python3.11 python3; do
  if command -v "$py" >/dev/null 2>&1 && "$py" -c 'import tarfile, tomllib; tarfile.data_filter' 2>/dev/null; then
    dir="$(dirname "$0")"
    kube="$("$py" "$dir/gpu_queue_kube.py")" || exit $?
    [ -n "$kube" ] && export KUBECONFIG="$kube"
    exec "$py" "$dir/gpu_submit.py" "$@"
  fi
done
echo "scripts/gpu: no python3 with tomllib and tarfile extraction filters (3.11.4+) on PATH" >&2
exit 70
