#!/bin/sh -eu

arch="$(dpkg --print-architecture)"
version=1.2.3
flavour=generic
installedname=$version-$flavour
tests_dir="$PWD/debian/tests"

# Create source package directory
mkdir -p "$AUTOPKGTEST_TMP/package/debian"
cd "$AUTOPKGTEST_TMP/package"

# Install k-w configuration and point to it
cp -R "$tests_dir/install-files-config" debian/installer
mv debian/installer/modules/any debian/installer/modules/$arch
echo >debian/installer/kernel-versions "$arch - $flavour - - -"
export KW_DEFCONFIG_DIR="$PWD/debian/installer"
export KW_CONFIG_DIR="$KW_DEFCONFIG_DIR"

# Create debian/control
echo >debian/control 'Source: linux'
kernel-wedge gen-control $version >>debian/control

export SOURCEDIR="$PWD/debian/build/install"
for moddir in /lib/modules/$installedname /usr/lib/modules/$installedname; do
    echo "I: Testing with modules in $moddir"
    for file in /boot/vmlinuz-$installedname \
		$moddir/modules.builtin \
		$moddir/modules.order \
		$moddir/kernel/drivers/dummy/dummy-core.ko.xz \
		$moddir/kernel/drivers/dummy/dummy-drv.ko.xz; do
	mkdir -p "$SOURCEDIR${file%/*}"
	touch "$SOURCEDIR$file"
    done
    DEPMOD="$tests_dir/dummy-depmod" kernel-wedge install-files $version
    cat >"$AUTOPKGTEST_TMP/expected-files" <<EOF
debian/dummy-modules-$installedname-di$moddir/kernel/drivers/dummy/dummy-core.ko.xz
debian/dummy-modules-$installedname-di$moddir/kernel/drivers/dummy/dummy-drv.ko.xz
debian/kernel-image-$installedname-di/boot/vmlinuz
debian/kernel-image-$installedname-di$moddir/modules.builtin
debian/kernel-image-$installedname-di$moddir/modules.order
EOF
    find debian/*-di -type f \
    | LC_COLLATE=C.UTF-8 sort \
    | diff -u "$AUTOPKGTEST_TMP/expected-files" /dev/stdin
    rm -r "$SOURCEDIR"
    rm -r debian/*-di*
done
