#! /bin/bash

IFS=$'\n'

case "$1" in
kernel-module-*)    ;; # Fedora kernel module package names start with
                      # kernel-module.
kernel*)           is_kernel_package=1 ;;
esac

is_tumbleweed=false

if test "$1" = "--tumbleweed"; then
    if test "$2" -gt 0; then
        is_tumbleweed=true
    fi
    shift 2
fi

modules=()
modreqs=""
modsexp=""
while read f ; do
	case "$f" in
		*/lib/modules/*.ko) modules[${#modules[*]}]="$f" ;;
		*/lib/modules/*.ko.[gx]z) modules[${#modules[*]}]="${f%.*}" ;;
		*/lib/modules/*.ko.zst) modules[${#modules[*]}]="${f%.*}" ;;
		*/lib/modules/*/modules.builtin) while read x; do modsexp="$modsexp|$(basename "$x" .ko | tr '-' '_')"; done < $f ;;
		*/usr/lib/modules-load.d/*.conf) while read x; do
			case "$x" in
				\#*|\;*);; #empty lines removed by grep
				*)modreqs="$modreqs $x.ko";;
			esac
		done < $f
	esac
done
while read x ; do modsexp="$modsexp|$(basename "$x" .ko | tr '-' '_')" ; done << EOF
	$(echo "${modules[@]}" | tr ' ' '\n')
EOF
echo $modreqs | tr ' -' '\n_' | grep -vE "^(|($modsexp).ko)\$" | while read x; do echo "(kmod($x) if kernel)" ; done

if $is_tumbleweed; then
	for module in "${modules[@]}"; do
		version=${module#*/lib/modules/}
		version=${version%%/*}
		echo "kernel-uname-r = $version"
	done
	exit 0
fi

if [ -n "$is_kernel_package" ] || ! test -e /sbin/modprobe || ! test -e /sbin/modinfo ; then
    cat > /dev/null
    exit 0
fi

trap 'rm -f "$tmp"' EXIT
tmp=$(mktemp)

all_provides() {
    for module in "${modules[@]}"; do
	if [ -f "$module" ]; then
	    nm "$module"
	else
	    if [ -f "$module".gz ]; then
		gzip -cd "$module".gz > "$tmp"
	    elif [ -f "$module".xz ]; then
		xz -cd "$module".xz > "$tmp"
	    elif [ -f "$module".zst ]; then
	    	zstd -cd "$module".zst > "$tmp"
	    else
		continue
	    fi
	    nm "$tmp"
	fi
    done \
    | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):\1\t\2:p' \
    | sort -t $'\t' -k2 -u
}

all_requires() {
    for module in "${modules[@]}"; do
	if [ ! -f "$module" ]; then
	    if [ -f "$module".gz ]; then
		module="$module".gz
	    elif [ -f "$module".xz ]; then
		module="$module".xz
	    elif [ -f "$module".zst ]; then
		module="$module".zst
	    else
		continue
	    fi
	fi
	set -- $(/sbin/modinfo -F vermagic "$module" | sed -e 's: .*::' -e q)
	/sbin/modprobe --dump-modversions "$module" \
	    | sed -r -e 's:^0x0*::' -e 's:$:\t'"$1"':'
    done \
    | sort -t $'\t' -k2 -u
}

if [ ${#modules[@]} -gt 0 ]; then
    symset_table=$(mktemp -t ${0##*/}.XXXXX)
    /usr/lib/rpm/symset-table | sort -t $'\t' -k 1,1 > $symset_table

    join -t $'\t' -j 1 -a 2 $symset_table <(
	# Filter out requirements that we fulfill ourself.
	join -t $'\t' -j 2 -v 1 \
	    <(all_requires "${modules[@]}") \
	    <(all_provides "${modules[@]}") \
	| awk '
	BEGIN { FS = "\t" ; OFS = "\t" }
	{ print $3 "/" $2 "/" $1 }
	' \
	| sort -t $'\t' -k 1,1 -u) \
    | sort -u \
    | awk '
    { FS = "\t" ; OFS = "\t" }
	    { split($1, arr, "/")
	      flavor = gensub(/.*-/, "", 1, arr[1]) }
    NF == 3 { print "kernel(" flavor ":" $2 ") = " $3
	      next }
	    { print "ksym(" flavor ":" arr[3] ") = " arr[2] }
    '
fi
