#!/bin/sh
# SPDX-FileCopyrightText: Copyright © 2022 Andreas Beckmann <anbe@debian.org>
# SPDX-License-Identifier: GPL-2.0-or-later
set -eu

result=0
summary=
newline="
"

header_packages=
check_for_linux_headers() {
    # Act only on the first run.
    if [ -n "$header_packages" ]; then
        return
    fi

    # Which Linux header packages are installed?
    header_packages=$(dpkg-query -f '${Status} ${Package}\n' -W 'linux-headers-*' 2>/dev/null | sed -r -n 's/^install ok installed //p')
    if [ -n "$header_packages" ]; then
        echo "I: Using the following Linux header packages that were already installed:"
        for p in $header_packages ; do
            echo "I:   $p"
        done
        return
    fi

    # Which Linux header packages could be installed?
    # linux-doc is a dummy dependency to select the src:linux version to test.
    # Install only linux-headers-* matching the source version of linux-doc.
    wanted_source_version=$(dpkg-query -f '${source:Version}' -W linux-doc 2>/dev/null || true)
    candidates=$(apt-cache search --names-only '^linux-headers-' | awk '{print $1}' | grep -v -E -e '-common(-rt)?$')
    echo "I: No Linux header packages are installed."
    echo "I: Installing all available ones from src:linux $wanted_source_version:"
    for p in $candidates ; do
        if [ -z "$wanted_source_version" ]; then
            echo "I:   $p"
            header_packages="$header_packages $p"
            continue
        fi
        source_versions=$(apt-cache show $p | perl -ne 'if (/^$/) { print $s || $v, "\n"; $s=$v=""; } $s=$1 if /^Source: .* \((.*)\)$/; $v=$1 if /^Version: (.*)$/;')
        for sv in $source_versions ; do
            if [ "$sv" = "$wanted_source_version" ]; then
                echo "I:   install $p"
                header_packages="$header_packages $p"
                continue 2
            fi
        done
        echo "I:   skip    $p"
    done
    RC=0
    apt-get install --no-install-recommends -yq $header_packages </dev/null 2>&1 || RC=$?
    if [ "$RC" -ne 0 ]; then
        echo "E: Linux headers failed to install" >&2
        exit 1
    fi
}

for m in $@ ; do
    check_for_linux_headers
    module-assistant clean "$m"
    for k in /lib/modules/*/build
    do
        test -d "$k" || continue
        kconfig="$k/.config"
        k="${k%/build}"
        k="${k#/lib/modules/}"

        skip=
        if [ -n "${BUILD_EXCLUSIVE_CONFIG-}" ] && [ -e "$kconfig" ]; then
            for opt in $BUILD_EXCLUSIVE_CONFIG ; do
                case "$opt" in
                    !*) grep -q -E "^${opt#!}=[ym]" "$kconfig" && skip="${skip:+$skip }$opt" ;;
                    *)  grep -q -E "^${opt}=[ym]" "$kconfig" || skip="${skip:+$skip }$opt" ;;
                esac
            done
        fi
        if [ -n "$skip" ]; then
            echo "I: $m does not support the $k kernel ($skip)"
            summary="${summary}I: SKIP $m $k ($skip)${newline}"
            continue
        fi

        RC=0
        echo "I: Trying to build $m for $k"
        module-assistant build --text-mode --force --kvers-list "$k" "$m" || RC=$?
        if [ "$RC" = 0 ]; then
            summary="${summary}I: PASS $m $k${newline}"
            for deb in /usr/src/${m}*-${k}_*_*.deb ; do
                if [ -f "$deb" ]; then
                    echo
                    echo "$deb:"
                    dpkg --info "$deb"
                    echo
                    dpkg-deb --contents "$deb"
                    echo
                fi
            done
        else
            result=1
            summary="${summary}I: FAIL $m $k ($RC)${newline}"
        fi
    done
done

if [ -n "$summary" ]; then
    echo "I: Summary:"
    echo -n "$summary"
fi

exit $result

# vim: sw=4:ts=4:et
