#!/bin/sh
set -eu

# keep container around if $DEBUG is set
[ -n "${DEBUG:-}" ] || OPTS="--rm"

if type podman >/dev/null 2>&1; then
    RUNC=podman
else
    RUNC="sudo docker"
fi

$RUNC run --interactive ${OPTS:-} --volume `pwd`:/source:ro ${1:-docker.io/alpine} /bin/sh <<EOF

# install build dependencies
apk add --no-cache meson gcc musl-dev glib-dev eudev-dev make vala linux-headers xz usbutils ${EXTRA_PACKAGES:-}

# run build as user
su -s /bin/sh - guest << EOG
set -ex
cd /source
meson setup /tmp/dbg --buildtype debug
meson test -C /tmp/dbg -v --num-processes=1
DESTDIR=/tmp/inst meson install -C /tmp/dbg
echo "===== tests done; install tree: ===="
ls -lR /tmp/inst
EOG

EOF
