#!/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>

# some initial definitions

CONFIG_MK=config.mk
KERNEL_BUILD=/lib/modules/`uname -r`/build
# If using 2.6 outputting to a seperate directory, make KERNEL_OUTPUT
# point to that directory
#KERNEL_OUTPUT=
ECHO="`which echo` -e"
WARNING_FLAG=0

# functions

# 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
}

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

configcheck() {
	eval "CONFIG_$1=n"
	if qgrep "^CONFIG_$1=y" "$KERNEL_OUTPUT/.config"
	then
		eval "CONFIG_$1=y"
	elif qgrep "^CONFIG_$1=m" "$KERNEL_OUTPUT/.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.
}

# ---------- main ----------

rm -f $CONFIG_MK

if [ -z "${KERNEL_OUTPUT}" ]
then
	KERNEL_OUTPUT=${KERNEL_BUILD}
fi

if [ ! -d ${KERNEL_OUTPUT} ]; then
  $ECHO
  $ECHO "WARNING: kernel build/output directory not found!"
  $ECHO "You probably need to install the kernel development/header file package! (which matches your currently running kernel)"
  $ECHO
  WARNING_FLAG=1
fi
  
$ECHO Kernel version file: $KERNEL_OUTPUT/include/linux/version.h
$ECHO Kernel configuration file: $KERNEL_OUTPUT/.config
$ECHO "Make damn sure these really match your currently running kernel!!"
$ECHO

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

[ -z $VERSION_CODE ] && VERSION_CODE=0

if [ -e "$KERNEL_OUTPUT/.config" ]
then
	$ECHO "Kernel configuration found, performing sanity checks"
	configcheck MODULES
	configcheck NET_RADIO
	configcheck NET_WIRELESS
	configcheck PCMCIA
	configcheck CARDBUS
	configcheck USB
	configcheck PCI
	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 USB cards:"
	printflag "USB support" CONFIG_USB
	$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 "WARNING: 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."
		WARNING_FLAG=1
	fi
	if is_unset CONFIG_USB
	then 
		$ECHO "WARNING: USB support (CONFIG_USB) is disabled."
		$ECHO "This is not a terribly good idea in case your ACX100 card is USB-based."
		WARNING_FLAG=1
	fi
else
	$ECHO "WARNING: Unable to find kernel configuration file ($KERNEL_OUTPUT/.config) -> unable to perform sanity checks"
	$ECHO "Make sure your kernel has the required options enabled, see README"
	WARNING_FLAG=1
fi

if [ $VERSION_CODE -ge `version 2 5 0` ]
then
	KERN26="yes"
else
	KERN26=
fi

write_str KERNEL_BUILD
write_str KERNEL_OUTPUT
write_str VERSION_CODE
write_str KERN26

if [ "$WARNING_FLAG" = "1" ]; then
  $ECHO
  $ECHO "*** Potential problems found above, waiting 5 seconds to let you know about it. ***"
  sleep 5
fi
