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

dsrc=`cd $SRC; pwd`
dbld=`pwd`
if [ -n "$sysname" -a $dsrc = $dbld ] ; then
	echo "ERROR: build directory (current directory) must be different"
	echo "       from the source directory!"
	sysname=
fi

if [ -z "$sysname" ] ; then
	d=`$SRC/bin/relpath $SRC ..`
	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 "and then run config from the source directory by typing"
	echo "its relative path.  The relative path to the source directory"
	echo "is extracted from the command used to run config"
	echo "i.e.:   cd ..; mkdir target; cd target; ../$d""config target"
	echo "(replace 'target' with the name of your target machine)"
	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=". $SRC/Config/$hostenv/default.cf"
else
	CONFIG_DEFAULTS=". $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:
. $conf
# Stop exporting all
set +a

CONF_SYS=$sysname
export CONF_SYS

if [ "x$MAKE" = "x" ] ; then
	MAKE=make
fi
set +e
if [ "x`$MAKE -x -v 2>/dev/null | grep GNU`" = "x" ] ; then
	echo "trying gmake instead of $MAKE..."
	MAKE=gmake
fi
if [ "x`$MAKE -v 2>/dev/null | grep GNU`" = "x" ] ; then
	echo
	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
set -e
echo MAKE=$MAKE

SRC=`$SRC/bin/relpath $SRC .`

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

echo "Configuration complete."
echo
echo "To prepare the libraries, include files, etc, type:"
echo "    $MAKE setup"

