#!/bin/sh
# Portable System Interface Copyright (c) 1997-98 Tycho Softworks.
# $Id: config 1.1 Tue, 23 Jun 1998 11:38:33 -0400 dyfet $

BUILD=sdk
CONFIG=sdk/_config.h

CONFIG_CAP_PASSED_FILES='false'
CONFIG_CAP_UNIX_SOCKETS='true'
CONFIG_CAP_INET_SOCKETS='true'
CONFIG_CAP_MAPPED_FILES='false'
CONFIG_CAP_STREAM_FILES='false'
CONFIG_CAP_UNIX_STREAMS='false'
CONFIG_CAP_PTY_DEVICES='false'
CONFIG_CAP_UNIX_THREADS='false'

if test -f config.cache ; then
	. ./config.cache
fi

if test -f config.pkg ; then
	. ./config.pkg
fi

echo 'CONFIG_BUILD="'$BUILD'"' >config.cache

#
# Load bindings
#

if test -f config.pkg ; then
	. ./config.pkg
fi

if test -f config.make ; then
	echo -n >config.make
fi

if test -f $CONFIG ; then
	echo -n >$CONFIG
fi

if test -f $BUILD/bin/config.sub ; then
	. $BUILD/bin/config.sub
fi

BIND_OPTS='host'

if test -z "$CONFIG_HOST" ; then

	# Test CygWIN32

	if test ! -z "$CYGFS" ; then
		CONFIG_HOST=`echo $CYGFS/*cygwin32/*cygwin32`
		CONFIG_WIN32='true'
	fi

	# Normal UNIX hosts

	if test -d /usr/local/include/sys ; then
		CONFIG_HOST=/usr/local
	fi

	if test -d /usr/include/sys ; then
		CONFIG_HOST=/usr
	fi
fi

opt_host() {
	CONFIG_HOST="$1"
	return 0
}

if test ! -z "$BIND" ; then
	for bind in $BIND ; do
		if test -d $BUILD/$bind ; then
			. $BUILD/$bind/bind.conf
		else
			. $BUILD/bin/$bind.bind
		fi
	done
fi

# 
# Parse command line arguments.
#
err=''

if test ! -z "$CONFIG_FLAGS" ; then
	for arg in $CONFIG_FLAGS ; do
		for opt in $BIND_OPTS ; do
			case "$arg" in
			$opt=* | -$opt=* | --$opt=* )
				opt_$opt `echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'`
				break
				;;
			esac
		done	
	done
fi


if test ! -z "$*" ; then
	for arg in $* ; do
		err=$arg
		for opt in $BIND_OPTS ; do
			case "$arg" in
			$opt=* | -$opt=* | --$opt=* )
				opt_$opt `echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'`
				err=''
				break
				;;
			esac
		done	
		if test ! -z "$err" ; then
			break
		fi
	done
fi

CONFIG_FLAGS=''

if test ! -z "$err" ; then
	echo "config: $err: invalid option."
	echo 
	echo "Valid Options:" 
	echo -n >config.help
	if test ! -z "$BIND" ; then
		for use in $BIND ; do
			if test -f $BUILD/$use/help.conf ; then
				cat $BUILD/$use/help.conf >>config.help
			fi
			if test -f $BUILD/bin/$use.help ; then
				cat $BUILD/bin/$use.help >>config.help
			fi
		done
		sort <config.help
		rm -f config.help
	fi
	exit -1
fi

echo "#ifndef	__CONFIG_H__" >>$CONFIG
echo "#define	__CONFIG_H__" >>$CONFIG

if test ! -z "$CONFIG_WIN32" ; then
	echo #define	WIN32" >>$CONFIG
fi

if test ! -z "$BIND" ; then
	for bind in $BIND ; do
		if test -f $BUILD/$bind/make.conf ; then
			. $BUILD/$bind/make.conf
			if test -f $BUILD/$bind/Makefile.in ; then
				MAKEFILES="$MAKEFILES"' '$BUILD/$bind/Makefile
			fi
		else
			if test -f $BUILD/bin/$bind.make ; then
				. $BUILD/bin/$bind.make
			fi
		fi
	done
fi

SHLIBS=""
if test ! -z "$CHECK" ; then
	if test -z "$MAKELIBS" ; then
		MAKELIBS="std"
	fi
	for check in $CHECK ; do
		if test -d $inc/$check ; then
			continue
		fi
		if test -f $CONFIG_HOST'/lib/lib'$check'.a' ; then
			continue
		fi
		MAKELIBS=$MAKELIBS' '$check
		if test -z "$SHLIBS" ; then
			SHLIBS=$check
		else
			SHLIBS=$SHLIBS" "$check
		fi
	done
fi

if test ! -z "$MAKELIBS" ; then
	echo 'CONFIG_MAKELIBS="'$MAKELIBS'"' >>config.cache
	echo 'MAKELIBS='$SHLIBS >>config.make
fi
echo 'INCLUDE='$inc >>config.make

if test ! -z "$VERSION" ; then
	echo 'VERSION='$VERSION >>config.make
fi

if test ! -z "$PATHS" ; then
	for tpath in $PATHS ; do
		spath=$tpath':'
		dirs=`cat config.paths | grep $spath | sed s/$spath//`
		fpath=''
		if test ! -z "$dirs" ; then
			for dir in "$dirs" ; do
				if test -d $dir ; then
					fpath=`echo $dir`
				fi
			done
		fi
		if test ! -z "$fpath" ; then
			echo $tpath'='$fpath >>config.make
			echo '#define	_PATH_'$tpath'	"'$fpath'/"'>>$CONFIG
			echo 'PATH_'$tpath'="'$fpath'"' >>config.cache
		fi
	done
fi

if test ! -z "$MAKEFILES" ; then
	for makefile in $MAKEFILES ; do
		echo '# '`date` >$makefile
		if test -f config.install ; then
			echo "# Install destinations" >>$makefile
			echo " " >>$makefile
			cat config.install >>$makefile
		fi
		echo "# Build options" >>$makefile
		echo " " >>$makefile
		cat config.make >>$makefile
		cat $makefile.in >>$makefile
	done
fi


if test -z "$NAMESPACE" ; then
	echo "#define	__NAMESPACE(x)	x" >>$CONFIG
else
	echo "#ifdef	NAMESPACE" >>$CONFIG
	echo "#define	__NAMESPACE(x) _"$NAMESPACE"_##x##_" >>$CONFIG
	echo "#else" >>$CONFIG
	echo "#define	__NAMESPACE(x)	x" >>$CONFIG
	echo "#endif" >>$CONFIG
fi

echo "#endif" >>$CONFIG


if test -z "$CONFIG_CACHE" ; then
	CONFIG_CACHE='config.cache'
	CONFIG_FLAGS=''
else
	CONFIG_VERIFY=''
fi

echo "CONFIG_CACHE="$CONFIG_CACHE >>config.cache

if test -f conftest.c ; then
	rm -f conftest.c
fi

if test -f conftest.o ; then
	rm conftest.o
fi

if test ! -z "$CONFIG_VERIFY" ; then
	. $CONFIG_VERIFY
fi

