#!/bin/sh
#
#  Kernel configuration 
#
set -e

sysname=$1		# hostname of target system 
systype=$2		# type of target system
SRC=`dirname $0`
BASE=.
SPATH=.
export SRC BASE SPATH

if [ ".$sysname" = ".-l" ] ; then
	for i in $SRC/Config/*.cf ; do
		printf "%-15s" `basename $i .cf`
		head -1 $i
	done
	exit 0
fi

if [ -z "$sysname" ] ; then
	echo ""
	echo "usage: $0 <sysname> [<systype>]"
	echo "	where:  sysname = hostname of target system"
	echo "	        systype = machine architecture type, or"
	echo "			  name of configuration file"
	echo "	\"$0 -l\" will list all standard machine types"
	echo
	echo "This will create the build area starting in the *current*"
	echo "directory, so make sure you are in an empty subdirectory"
	echo ""
	exit 1
fi

if [ -z "$systype" ] ; then
	if [ -f "$sysname.cf" ]; then
		systype=$sysname
	else
		echo "error: no local configuration file $sysname.cf"
		echo "(perhaps <systype> was omitted?)"
		exit 1
	fi
fi

conf=$systype.cf
if [ ! -f "$conf" ] ; then
	conf=$SRC/Config/$systype.cf
fi
if [ ! -f "$conf" ] ; then
	echo "error: no configuration file $systype.cf or $conf"
	exit 1
fi

if [ "$conf" != "$sysname.cf" ] ; then
	cp $conf $sysname.cf
fi

CONFIG_DEFAULTS="source $SRC/Config/default.cf"

# Export all parameters:
set -a
# Read configuration options:
source $conf
# Stop exporting all
set +a

CONF_SYS=$sysname
export CONF_SYS

if [ "x$MAKE" = "x" ] ; then
	MAKE=make
fi
if [ "x`$MAKE -v | grep GNU`" = "x" ] ; then
	echo "trying gmake instead of $MAKE..."
	MAKE=gmake
fi
if [ "x`$MAKE -v | grep GNU`" = "x" ] ; then
	echo "error: GNU make (gmake) needed... please install GNU make."
	echo "if GNU make is available, set the environment variable MAKE"
	echo "to point to it"
	exit 1
fi

$SRC/bin/dconfig "$CONF_MACH" $SRC / .
$MAKE bin/all
$SRC/bin/dconfig "$CONF_MACH" $SRC / . 

echo "Configuration complete."
echo "To prepare for kernel building, type:"
echo "    $MAKE setup"
echo "Then, to prepare for making bootdisks, type:"
echo "    $MAKE sys/boot/bootprep"

