#!/bin/sh
set -eu
set -x

DIR=${0%/*}
SCRIPT=${0##*/}

case ${PHASE-0} in
0)
	podman run --rm \
	       --cap-add sys_chroot \
	       --entrypoint /bin/sh \
	       -e PHASE=1 \
	       --mount type=bind,src="$DIR/..",dst=/repo,ro \
	       docker.io/library/archlinux -c /repo/_tools/"$SCRIPT"
	;;
1)
	pacman -Syy --noconfirm \
	       base base-devel git go linux-headers nginx opendoas perl

	echo 'permit nopass build' >/etc/doas.conf
	useradd -m build

	cp -r /repo /home/build/repo
	chown -R build:build /home/build/repo

	su -c "env PHASE=2 ~/repo/_tools/$SCRIPT" - build
	;;
2)
	# https://bbs.archlinux.org/viewtopic.php?id=120256
	set +ux; . /etc/profile; set -ux

	cd ~/repo
	autoreconf -i

	cd _tools
	./setup

	# gcc has false positive
	export CFLAGS=-Wno-error=use-after-free

	./check-system
	./check-libressl
	./check-libressl-libtls
	./check-openssl-1.1.1
	./check-openssl-1.1.1-libtls
	./check-openssl-3.0
	./check-openssl-3.0-libtls
	./check-openssl-3.5
	./check-openssl-3.5-libtls
	;;
esac
