#!/bin/bash
modulefile="$1"
base=$(basename $modulefile | sed 's/\.ko$//')
basedir=$(dirname $(echo "$modulefile" | cut -d'/' -f6-))
basedirparent=$(echo "$basedir" | cut -d'/' -f1-2)
match=$(find $basedir -name Makefile -o -name Kbuild | xargs grep -E "[^a-z_-]$base.o([^a-z_-]|$)" | grep CONFIG)
if [ "$match" = "" ]; then
    match=$(find $basedirparent -name Makefile -o -name Kbuild | xargs grep -E "[^a-z_-]$base.o([^a-z_-]|$)" | grep CONFIG)
    if [ "$match" = "" ]; then
	match=$(find -name Makefile -o -name Kbuild | xargs grep -E "[^a-z_-]$base.o([^a-z_-]|$)" | grep CONFIG)
	if [ "$match" = "" ]; then
	    match=$(find -name Makefile -o -name Kbuild | xargs grep -E " $base/" | grep CONFIG)
	    if [ "$match" = "" ]; then
		case "$base" in
ocfs2_stackglue)
			match="CONFIG_OCFS2_FS"
			;;
speakup_*)
			match="CONFIG_SPEAKUP"
			;;
ib_cm|iw_cm)
			match="CONFIG_INFINIBAND"
			;;
		    *)
			echo UNKNOWN
			exit 1
			;;
		esac
	    fi
	fi
    fi
fi

echo "$match" | tr '()' '\n\n' | grep ^CONFIG | head -n1
