#! /bin/sh

t=/tmp/__configure.$$
trap 'rm -f $t; exit 0' 0 1 2 3 13 15

# Insulate against IFS from the user's env
IFS=' ''	''
'
export IFS

# Allow this script to be run from anywhere
cd "`dirname \"$0\"`"

# There's a cleanup function so we can easily clean out the directory.
clean()
{
	# Use the Makefile to remove object files if they exist.
	test -f Makefile && make distclean > /dev/null

	# Remove automatically generated files.
	rm -rf	Makefile \
		Makefile.am \
		Makefile.in \
		aclocal.m4 \
		auto-includes.chk \
		autom4te.cache \
		config.cache \
		config.hin \
		config.hin~ \
		config.log \
		config.status \
		configure \
		gnu-support \
		mklog
}

# We always clean things up, assume build_posix and the top-level directory
# are the build spots.
(cd .. && clean)
clean

while :
	do case "$1" in
	-c)				# Clean and leave empty
		exit 0;;
	*)				# Clean and then re-create
		break;;
	esac
done

# Build configure.ac
(
echo "# DO NOT EDIT"
echo "# This file is built automatically from build_posix/configure.ac.in."

sed -n '1,/BEGIN check existence/p' configure.ac.in

sed -e 's/#.*$//' -e '/^$/d' -e '/^\.$/d' Make.subdirs | \
while read dir cond ; do
	test -d ../$dir || continue
	echo 'AC_CONFIG_FILES(['$dir/Makefile'])'
done

sed -n '/END check existence/,$p' configure.ac.in
) > ../configure.ac

# Build Makefile.am
sh ./makemake

# From here on, work in the top of the tree
cd ..
autoreconf --install --warnings=all

# Make sure any missing files are writable
chmod 755 build_posix/gnu-support/*

# Cleanup
rm -rf autom4te.cache

exit 0
