#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Build paulikit and run the non-slow suite inside a distribution
# container, for the jobs in extended.yml. Run as root inside the
# container, with the checkout mounted read-only at /src:
#
#   ext-container fedora | debian | alpine | debian-system
#
# fedora, debian, alpine
#     Distribution compiler, Python and oneTBB; NumPy and SciPy from
#     pip wheels in a virtual environment. If no wheel exists for the
#     platform (alpine falls back to the musllinux wheels, then to
#     the distribution's own packages), the distribution's NumPy and
#     SciPy are used instead.
# debian-system
#     Everything from the distribution, no wheels at all: for
#     platforms such as s390x where SciPy publishes none.
#
# EXPECT (BUILT, FALLBACK or any) and EXPECT_BYTEORDER are passed to
# native-outcome.
set -eu

distro=${1:?usage: ext-container DISTRO}

install_fedora() {
    dnf -y -q install gcc gcc-c++ tbb-devel python3 python3-devel \
        python3-pip
}

install_debian() {
    export DEBIAN_FRONTEND=noninteractive
    apt-get update -q
    apt-get install -y -q gcc g++ libtbb-dev python3 python3-dev \
        python3-venv python3-pip
}

install_debian_system() {
    install_debian
    apt-get install -y -q python3-numpy python3-scipy python3-pytest \
        python3-mesonpy cython3 meson ninja-build
}

install_alpine() {
    apk add --no-cache gcc g++ musl-dev linux-headers python3 \
        python3-dev py3-pip samurai
    # oneTBB is optional for the build; report if it is missing.
    apk add --no-cache onetbb-dev \
        || echo "note: no onetbb-dev package; building without oneTBB"
}

system_site=
case "$distro" in
    fedora) install_fedora ;;
    debian) install_debian ;;
    alpine) install_alpine ;;
    debian-system)
        install_debian_system
        system_site=--system-site-packages
        ;;
    *) echo "unknown distro: $distro" >&2; exit 2 ;;
esac

cp -a /src /work
cd /work

# shellcheck disable=SC2086  # system_site is a flag or empty
python3 -m venv $system_site /venv
PATH=/venv/bin:$PATH
export PATH
python -m pip install --upgrade pip

if [ "$distro" != debian-system ]; then
    if python -m pip install --only-binary=:all: numpy scipy pytest; then
        echo "numpy and scipy: installed from wheels"
    elif [ "$distro" = alpine ]; then
        echo "numpy and scipy: no wheel; using the distribution's"
        apk add --no-cache py3-numpy py3-scipy py3-pytest
        rm -rf /venv
        python3 -m venv --system-site-packages /venv
    else
        echo "error: no numpy/scipy wheels for this platform" >&2
        exit 1
    fi
    python -m pip install meson-python cython
    command -v ninja >/dev/null || python -m pip install ninja
else
    echo "numpy and scipy: from the distribution"
fi

# Build isolation is off for the reason given in ci.yml: the editable
# rebuild hook records NumPy's include path.
python -m pip install -e ".[test]" --no-build-isolation

rc=0
python -m pytest -v -m "not slow" || rc=$?
python .github/scripts/native-outcome || rc=$?
exit "$rc"
