#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")/../python"

# One abi3 wheel per platform covers every CPython from 3.10 up, so the matrix
# is over targets rather than interpreter versions.
MAC_TARGETS=(aarch64-apple-darwin x86_64-apple-darwin)
LINUX_TARGETS=(x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu)

TOOLS="${TMPDIR:-/tmp}/rosless-wheel-tools"
if [ ! -x "$TOOLS/bin/maturin" ]; then
    python3 -m venv "$TOOLS"
    "$TOOLS/bin/pip" install --quiet --upgrade maturin ziglang twine
fi
export PATH="$TOOLS/bin:$PATH"

rustup target add "${MAC_TARGETS[@]}" "${LINUX_TARGETS[@]}"

for target in "${MAC_TARGETS[@]}"; do
    maturin build --release --target "$target"
done

# Zig supplies the cross-linker and lets us pin an old glibc, which is what
# makes the result manylinux rather than "whatever this machine happens to be".
for target in "${LINUX_TARGETS[@]}"; do
    maturin build --release --zig --target "$target"
done

maturin sdist

twine check ../target/wheels/*

echo ""
echo "Done. Upload with: twine upload target/wheels/*"
ls -lh ../target/wheels
