#!/bin/sh
set -eu

script_name="${0##*/}"
test="${script_name%%-*}"

if test -n "${AUTOPKGTEST_TMP-}"; then
	TESTDIR="${AUTOPKGTEST_TMP}"
else
	TESTDIR="$(mktemp -d -t dracut-test.XXXXXXXXXX)"
fi

ARCH=$(dpkg --print-architecture)
# shellcheck disable=SC2012
KVERSION=$(cd /lib/modules; ls -1 | tail -1)
export ARCH
export KVERSION
export DRACUT=dracut
export PKGLIBDIR=/usr/lib/dracut

if test "$ARCH" = "ppc64el"; then
	# KVM fails on the Ubuntu ppc64el autopkgtest runners:
	# $ /usr/bin/qemu-system-ppc64el -enable-kvm -cpu host [...]
	# ioctl(KVM_CREATE_VM) failed: 22 Invalid argument
	# PPC KVM module is not loaded. Try modprobe kvm_hv.
	# qemu-system-ppc64el: failed to initialize kvm: Invalid argument
	# $ modprobe kvm_hv
	# modprobe: ERROR: could not insert 'kvm_hv': No such device
	export NO_KVM=1
fi

# Workaround masked systemd-udevd bug: https://github.com/dracut-ng/dracut-ng/issues/1318
rm -f /etc/systemd/system/systemd-udevd.service

# Prapare out-of-tree test directory
cp -r dracut.conf.d test "$TESTDIR"

make -C "$TESTDIR/test" V=1 check TESTS="$test"
