#! /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()
{
	test -f Makefile && make distclean > /dev/null
	rm -rf ../Makefile.am ../Makefile.in ../aclocal.m4 ../autom4te.cache \
	    ../config.hin ../config.hin~ ../configure gnu-support mklog
}

while :
	do case "$1" in
	-c)
		clean			# Clean and leave empty
		exit 0;;
	*)
		clean			# 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' 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
