#!/bin/sh

# this script might require a bash compatible shell, but right now it
# also runs in ksh and ash, so let's set it to /bin/sh

# Added checks to see if the kernel configuration file exists and if so,
# check if all necessary options are enabled. By Vesa-Pekka Palmu <depili@sci.fi>

CONFIG_MK=config.mk
KERNEL_BUILD=/lib/modules/`uname -r`/build
ECHO="`which echo` -e"

# What kernel are we compiling for?

version() {
	expr $1 \* 65536 + $2 \* 256 + $3
}

write_bool() {
	value=`eval $ECHO '$'$1`
	if test "$value" = "y"
	then
		$ECHO "$1=y" >> $CONFIG_MK
	else
		$ECHO "# $1 is not defined" >> $CONFIG_MK
	fi
}

write_str() {
	value=`eval $ECHO '$'$1`
	$ECHO $1=$value >> $CONFIG_MK
}

rm -f $CONFIG_MK

VERSION_CODE=`grep LINUX_VERSION_CODE $KERNEL_BUILD/include/linux/version.h | \
	sed -e 's/[^0-9]//g'`

qgrep() {
	grep $* >/dev/null 2>&1
}

configcheck() {
	eval "CONFIG_$1=n"
	if qgrep "^CONFIG_$1=y" "$KERNEL_BUILD/.config"
	then
		eval "CONFIG_$1=y"
	elif qgrep "^CONFIG_$1=m" "$KERNEL_BUILD/.config"
	then
		eval "CONFIG_$1=y"
	fi
}

printflag() {
	value=`eval echo '$'$2`
	$ECHO "    $1 is \c"
	if test "$value" = "y"
	then
		$ECHO "enabled."
	else
		$ECHO "DISABLED."
	fi
}

is_unset() {
	value=`eval echo '$'$1`
	if test "$value" = "n"
	then
		return 0
	else
		return 1
	fi
}

fail() {
	$ECHO "Kernel configuration lacks needed options, please correct! ABORTING."
	exit 1
}

wireless_stats() {
	if [ ! -f $KERNEL_BUILD/include/linux/wireless.h ]; then
		$ECHO "$KERNEL_BUILD/include/linux/wireless.h header file doesn't exist!" 
		fail
	fi
	WIRELESS_VERSION=`grep define.*WIRELESS_EXT $KERNEL_BUILD/include/linux/wireless.h|tr '\t' ' '|cut -f3 -d' '`
	echo Wireless header file is WIRELESS_EXT version $WIRELESS_VERSION.
}

$ECHO Kernel version file: $KERNEL_BUILD/include/linux/version.h
$ECHO Kernel configuration file: $KERNEL_BUILD/.config
$ECHO "Make damn sure these really match your currently running kernel!!"
$ECHO

if [ -e "$KERNEL_BUILD/.config" ]
then
	$ECHO "Kernel configuration found, performing sanity checks"
	configcheck PCMCIA
	configcheck CARDBUS
	configcheck NET_WIRELESS
	configcheck NET_RADIO
	configcheck PCI
	configcheck MODULES
	configcheck SMP
	$ECHO "All of the following items are required by the driver:"
	printflag "Loadable modules support" CONFIG_MODULES
	printflag "Wireless LAN (non-hamradio) support" CONFIG_NET_RADIO
	printflag "Wireless extensions support" CONFIG_NET_WIRELESS
	$ECHO "The following is needed for PCMCIA/CardBus cards:"
	printflag "PCMCIA support" CONFIG_PCMCIA
	printflag "CardBus support" CONFIG_CARDBUS
	$ECHO "The following is needed for PCI card support:"
	printflag "PCI support" CONFIG_PCI
	if is_unset CONFIG_MODULES
	then
		fail
	elif is_unset CONFIG_NET_RADIO
	then
		fail
	elif is_unset CONFIG_NET_WIRELESS
	then
		fail	
	else
		$ECHO "Kernel configuration satisfies the minimum requirements, continuing."
		wireless_stats
	fi
	if is_unset CONFIG_SMP
	then 
		$ECHO "Symmetric multiprocessing support (CONFIG_SMP) is disabled."
	else
		$ECHO "Symmetric multiprocessing support (CONFIG_SMP) is enabled. If you"
		$ECHO "really have more than one cpu, be warned that this driver is not yet SMP safe."
	fi
else
	$ECHO "Unable to find kernel configuration -> unable to perform sanity checks"
	$ECHO "Make sure your kernel has the required options enabled, see README"
fi

if [ $VERSION_CODE -ge `version 2 5 0` ]
then
        $ECHO "Kernel 2.5/2.6 requires in-tree compilation of modules"
        $ECHO "Read the file README for more information"
	exit 2
fi

write_str KERNEL_BUILD
write_str VERSION_CODE
