#!/bin/sh
set -eux

# go-faster apt
echo  'Acquire::Languages "none";' > /etc/apt/apt.conf.d/90nolanguages

# upgrade
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y eatmydata
eatmydata apt-get -y --purge dist-upgrade

# install build dependencies
eatmydata apt-get install --no-install-recommends -y git \
    python3-all python3-setuptools python3-build python3-venv \
    python3-dbus python3-gi gir1.2-glib-2.0 \
    pyflakes3 pycodestyle \
    dbus libnotify-bin upower network-manager bluez ofono ofono-scripts

# systemd's tools otherwise fail on "not been booted with systemd"
mkdir -p /run/systemd/system

# run build and test as user
useradd build
su -s /bin/sh - build << EOF || { [ -z "$DEBUG" ] || sleep infinity; exit 1; }
set -ex
cp -r $(pwd) /tmp/source
cd /tmp/source
python3 -m unittest -v
# massively parallel test to check for races
for i in \$(seq 100); do
    ( PYTHONPATH=. python3 tests/test_api.py TestTemplates || touch /tmp/fail ) &
done
wait
[ ! -e /tmp/fail ]

# check sdist; note https://bugs.debian.org/1009916
if dpkg --compare-versions \$(dpkg-query -f '\${Version}' --show python3-build) '>=' 0.7.0-3; then
    my_version=\$(git describe --abbrev=0)
    python3 -m build --sdist
    tar --wildcards -xOf dist/python-dbusmock-\${my_version}*.tar.gz '*/PKG-INFO' | grep "^Version: \${my_version}"
fi
EOF
