#!/bin/sh

# Crude attempt at a self-configuring script.  Yeah, sure, it should
# be autoconf-based, comprehensive, blah, blah, but I'll leave that
# for later.

# 1. Try to find the kernel headers.  The first method works for recent
# 2.4.xx kernels, and seems to be what Red Hat at least is pushing.  The
# second method should catch any sanely-configured older system...

TOPDIR=/lib/modules/`uname -r`/build
if [ ! -d $TOPDIR ] ; then
	TOPDIR=/usr/src/linux-`uname -r`
	if [ ! -d $TOPDIR ] ; then
		TOPDIR=/usr/src/linux
	fi
fi
if [ ! -d $TOPDIR -o ! -d $TOPDIR/include/linux ] ; then
	echo "You need to install the kernel source!"
	exit 1
fi

echo Kernel headers found at $TOPDIR/include/linux

# 2. Ask whether they want ECN or COS support

grep DCONFIG_ECN Config | grep '#' > /dev/null
if [ $? = 0 ] ; then
	# ECN off
	ECN='# -DCONFIG_ECN'
	echo -n 'Add explicit congestion notification (ECN) support [no]? '
else
	# ECN on
	ECN='-DCONFIG_ECN'
	echo -n 'Add explicit congestion notification (ECN) support [yes]? '
fi
read response junk
case $response in
	Y* | y*)
		ECN='-DCONFIG_ECN';;
	N* | n*)
		ECN='# -DCONFIG_ECN';;
esac

grep DCONFIG_COS Config | grep '#' > /dev/null
if [ $? = 0 ] ; then
	# COS off
	COS='# -DCONFIG_COS'
	echo -n 'Add class/type of service (COS) support [no]? '
else
	# COS on
	COS='-DCONFIG_COS'
	echo -n 'Add class/type of service (COS) support [yes]? '
fi
read response junk
case $response in
	Y* | y*)
		COS='-DCONFIG_COS';;
	N* | n*)
		COS='# -DCONFIG_COS';;
esac


# 3. Update the Config file appropriately
ex Config << below.Config
/TOPDIR/c
TOPDIR = $TOPDIR
.
/^ECN/c
ECN = $ECN
.
/^COS/c
COS = $COS
.
w
q
below.Config

# 4. Determine what widget set to use with xnistnet.

# Assume if someone went through the trouble of installing neXtaw,
# they want it...
ldconfig -p | grep neXtaw > /dev/null
if [ $? = 0 ] ; then
	OURXAWLIB=-lneXtaw
else
	ldconfig -p | grep Xaw3d > /dev/null
	if [ $? = 0 ] ; then
		OURXAWLIB=-lXaw3d
	else
		OURXAWLIB=-lXaw
	fi
fi

echo Using $OURXAWLIB for the widget set...

cd monitor

ex Imakefile << below.Imakefile
/OURXAWLIB/c
OURXAWLIB = $OURXAWLIB
.
w
q
below.Imakefile

# Rebuild the monitor makefiles appropriately:
echo ' '
echo ' '
xmkmf -a

exit 0
