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

sysname=$1		# hostname of target system 
systype=$2		# type of target system
hostenv=$3		# type of host (local) system
SRC=`dirname $0`
BASE=.
SPATH=.
export SRC BASE SPATH

if [ ".$sysname" = ".-l" ] ; then
	echo "Standard target machine types:"
	for i in $SRC/Config/*.cf ; do
		printf "  %-15s" `basename $i .cf`
		head -1 $i
	done
	echo "Supported host environment types:"
	for i in $SRC/Config/*; do
		if [ -d $i ] ; then
			echo "  `basename $i`"
		fi
	done
	exit 0
fi

if [ -z "$sysname" ] ; then
	echo ""
	echo "usage: $0 <sysname> [<systype> [<hostenv>] ]"
	echo "	where:  sysname = hostname of target system"
	echo "	        systype = machine architecture type, or"
	echo "			  name of configuration file for target machine"
	echo "		hostenv = host environment type"
	echo "	\"$0 -l\" will list all standard machine types and host 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" -a -r $sysname.cf ] ; then
	conf=$sysname.cf
else
	conf=$systype.cf
fi

if [ ! -r "$conf" ] ; then
	if [ -z "$hostenv" ] ; then
		hostenv=`uname`
		if [ -z "$hostenv" ] ; then
			echo "error: hostenv must be specified (auto-detection failed)"
			exit 1
		else
			echo "Host environment detected: $hostenv"
		fi
	fi
	if [ ! -d $SRC/Config/$hostenv ] ; then
		echo "error: unknown hostenv: $hostenv"
		exit 1
	fi

	if [ -z "$systype" ] ; then
		systype=`$SRC/Config/$hostenv/guess_target`
		if [ -z "$systype" ] ; then
			echo "error: target systype must be specified (auto-detection failed)"
			exit 1
		fi
		echo "Target systype guessed: $systype"
	fi
	
	conf=$SRC/Config/$hostenv/$systype.cf
	if [ ! -r $conf ] ; then
		conf=$SRC/Config/$systype.cf
	fi
fi

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

echo "Reading configuration from $conf"


if [ -r $SRC/Config/$hostenv/default.cf ] ; then
	CONFIG_DEFAULTS="source $SRC/Config/$hostenv/default.cf"
else
	CONFIG_DEFAULTS="source $SRC/Config/default.cf"
fi

if [ "$conf" != "$sysname.cf" ] ; then
	echo "hostenv=$hostenv" > $sysname.cf
	echo "CONFIG_DEFAULTS=\"$CONFIG_DEFAULTS\"" > $sysname.cf
	cat $conf >> $sysname.cf
fi

# 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_ARCH" $SRC / .
$MAKE bin/all
$SRC/bin/dconfig "$CONF_ARCH" $SRC / . 

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

