#!/bin/bash
set -eu

cd "$(dirname "$0")"
cd .. # git root

if ! command -v sudo; then
    # CI or Docker sometimes doesn't have it, so useful to have a dummy
    function sudo {
        "$@"
    }
fi

# --parallel-live to show outputs while it's running
tox_cmd='run-parallel --parallel-live'
if [ -n "${CI-}" ]; then
    # install OS specific stuff here
    case "$OSTYPE" in
    darwin*)
        # macos
        brew install fd
        ;;
    cygwin* | msys* | win*)
        # windows
        # ugh. parallel stuff seems super flaky under windows, some random failures, "file used by other process" and crap like that
        tox_cmd='run'
        ;;
    *)
        # must be linux?
        sudo apt update
        sudo apt install fd-find
        ;;
    esac
fi


PY_BIN="python3"
# some systems might have python pointing to python3
if ! command -v python3 &> /dev/null; then
    PY_BIN="python"
fi


# TODO hmm for some reason installing uv with pip and then running
# "$PY_BIN" -m uv tool fails with missing setuptools error??
# just uvx directly works, but it's not present in PATH...
"$PY_BIN" -m pip install --user pipx
"$PY_BIN" -m pipx run uv tool run --with=tox-uv tox $tox_cmd "$@"
