#!/bin/sh
# Common autopkgtest script for testing a dkms source package.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# Copyright: (C) 2014 Canonical Ltd.
set -eu

result=0

run_pkg() {
    pkg="$1"

    echo "I: Removing binary package $pkg, to get clean state"
    export DEBIAN_FRONTEND=noninteractive
    apt-get purge -yq $pkg </dev/null 2>&1 >/dev/null || true

    echo "I: Installing binary package $pkg"
    export DEBIAN_FRONTEND=noninteractive
    RC=0
    apt-get install -yq $pkg </dev/null 2>&1 || RC=$?
    if [ "$RC" -ne 0 ]; then
        echo "E: Package $pkg failed to install" >&2
        exit 1
    fi

    # Try and remove dkms to spot packages which miss a dkms dependency
    dpkg --remove dkms || true

    if ! dkms_conf=$(dpkg -L $pkg | grep '/usr/src' | grep '/dkms.conf$'); then
        echo "I: Package $pkg has no dkms.conf, skipping"
        return
    fi

    echo "I: Testing binary package $pkg"

    dkms_pkg=$(bash -c ". $dkms_conf > /dev/null; echo \$PACKAGE_NAME" 2>/dev/null)
    dkms_ver=$(bash -c ". $dkms_conf > /dev/null; echo \$PACKAGE_VERSION" 2>/dev/null)

    for k in /lib/modules/*/build
    do
        test -d "$k" || continue
        kver="${k%/build}"
        kver="${kver#/lib/modules/}"

        echo "I: Trying to build $dkms_pkg/$dkms_ver for $kver"
        res=0
        dkms build -m "$dkms_pkg" -v "$dkms_ver" -k "$kver" || res=$?

        if [ "$res" = 9 ]; then
            echo "I: $dkms_pkg/$dkms_ver is not supported on $kver (BUILD_EXCLUSIVE directive), skipping"
            continue
	fi

        if [ "$res" != 0 ]; then
            echo "E: $dkms_pkg/$dkms_ver failed to build for $kver" >&2
            makelog="/var/lib/dkms/$dkms_pkg/$dkms_ver/build/make.log"
            echo "========== $makelog ==========" >&2
            cat "$makelog" >&2 || true
            echo "====================" >&2
            result=1
            continue
        fi

        if ! dkms install -m "$dkms_pkg" -v "$dkms_ver" -k "$kver" ; then
            echo "E: $dkms_pkg/$dkms_ver failed to install for $kver" >&2
            result=1
            continue
        fi

        echo "I: Testing if $dkms_pkg modules are correctly installed"
        dkmsstatus="$(dkms status $dkms_pkg -k $kver)"
        echo "$dkmsstatus"
        if [ -z "$dkmsstatus" ]; then
            echo "E: dkms status output is empty!" >&2
            result=1
            continue
        fi

        if ! echo "$dkmsstatus" | grep -q "installed$"; then
            echo "E: not installed" >&2
            result=1
            continue
        fi

    done

    # collect build logs as artifacts
    if [ -d /var/lib/dkms ]; then
        (cd /var/lib/dkms; find -name "make.log" -print0 | xargs -r -0 tar cv) > "$AUTOPKGTEST_ARTIFACTS/$pkg-make-logs.tar"
    fi

    # skip modprobing for now; this fails too often (needs particular
    # hardware/firmware/etc)
    # for mod in $(awk -F '"' '/^BUILT_MODULE_NAME/ {print $2}' $dkms_conf); do
    #     echo "I: modprobe $mod"
    #     if ! modprobe $mod; then
    #         echo "E: Failed to modprobe module $mod" >&2
    #         exit 1
    #     else
    #         echo "I: $modname loaded"
    #     fi
    # done
}

# Do not (fail to) build the modules upon linux-header-* and *-dkms package
# installation, which can cause apt-get to fail. We will do this later with
# improved error reporting.
# (This only works if the *-dkms package is not yet installed.)
touch /etc/dkms/no-autoinstall

for pkg in $(grep-dctrl -FDepends -e '(^| )dkms' -o -FPackage -e '\-dkms' debian/control -sPackage -n); do
    # package might be arch: restriction or udeb etc.
    if ! apt-cache show $pkg >/dev/null 2>&1; then
        echo "I: Skipping unavailable package $pkg"
        continue
    fi
    run_pkg $pkg
done

exit $result
