#! /bin/sh
#  -*- Mode: Shell-script -*- 
# ----------------------------------------------------------------------
# bootstrap --- maintainer's bootstrap script
#
# Author:         Gary V. Vaughan <gvaughan@oranda.demon.co.uk>
# Maintainer:     Gary V. Vaughan <gvaughan@oranda.demon.co.uk>
# Created:        Thu Apr  9 00:40:23 1998
# Last Modified:     Fri Mar 19 12:47:18 1999
#            by:     Bruce Korb <korb@datadesign.com>
# ----------------------------------------------------------------------
# $Id: bootstrap,v 1.6 1999/03/19 20:58:50 bkorb Exp $
# ----------------------------------------------------------------------

# Code:

# Figure out the absolute path to the working directory.
wd=`echo $0|sed 's,/[^/]*$,,'`
test -z "$wd" && wd=.
 
# Search for a configure.in
for srcdir in $wd $wd/.. .. .; do
    test -f $srcdir/configure.in && break
done

# ----------------------------------------------------------------------
# Make sure all of the maintainer tools required for bootstrap are
# available on the host machine.
# ----------------------------------------------------------------------
#
# Assume autoconf will be present until we can prove otherwise.
AUTOCONF=autoconf
AUTOHEADER=autoheader

# Or automake.
ACLOCAL=aclocal
AUTOMAKE=:
sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in | \
	fgrep -q AM_INIT_AUTOMAKE && AUTOMAKE=automake

# See whether the configure template requires libtool.
LIBTOOLIZE=:
sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in | \
	fgrep -q AM_PROG_LIBTOOL && LIBTOOLIZE=libtoolize

DIE=0

missing()
{
    cat >&2 <<-	_EOF_
	**ERROR**:  You must have \`$1' installed to bootstrap autogen.
	Download the appropriate package for your distribution,
	or get the source at:
	    $2
	(or any later version found there)
	_EOF_
	DIE=1
}

doit ()
{
    echo "$@"
    eval "$@"
}

# We must be using autoconf (or we wouldn't need this script!).
$AUTOCONF --version >/dev/null 2>&1
if test $? -ne 0; then
	missing $AUTOCONF ftp://ftp.gnu.org/pub/gnu/autoconf-2.12.tar.gz

	# No point testing autoheader
	#
	AUTOHEADER=:
fi

# If autoconf failed we don't bother testing for autoheader.
$AUTOHEADER --version >/dev/null 2>&1
if test $? -ne 0; then
	missing $AUTOHEADER ftp://ftp.gnu.org/pub/gnu/autoconf-2.12.tar.gz
fi

# Check for the presence of libtool.
$LIBTOOLIZE --version >/dev/null 2>&1
if test $? -ne 0; then
	missing $LIBTOOLIZE ftp://ftp.gnu.org/pub/gnu/libtool-1.2.tar.gz
fi

# Check for the presence of automake.
$AUTOMAKE --version >/dev/null 2>&1
if test $? -ne 0; then
	missing $AUTOMAKE ftp://ftp.gnu.org/pub/gnu/automake-1.3.tar.gz

	# No point testing aclocal
	#
	ACLOCAL=:
fi

# If automake failed we don't bother testing for aclocal.
$ACLOCAL --version >/dev/null 2>&1
if test $? -ne 0; then
	missing $ACLOCAL ftp://ftp.gnu.org/pub/gnu/automake-1.3.tar.gz
fi

# If any of the above tests failed, abort at this point.
test "$DIE" -ne 0 && exit $DIE

# Still here?  Run through all of the required autogeneration tools in
# each directory which contains a configure.in file...
for i in `find $srcdir -name configure.in -print|grep -v CVS/`
do
    # Find the top directory with respect to the current configure.in.
    top=`echo $i|sed 's,/[^/]*$,,'`
    case $i in
        /* | [A-Za-z]:\\*) ;;
        *) top=`(cd $top && pwd)` ;;
    esac

    # A brief check to make sure we are not running in the wrong
    # directory.
    initfile=`fgrep AC_INIT $top/configure.in | sed 's,^.*(,,;s,).*$,,'`
    if test -z "$initfile" || test ! -f $initfile; then
        cat <<- EOF
		**Error**: Directory \`$top\' does not look like a valid
		top-level directory.
		EOF
        exit 1
    fi

    # Execute this in a sub-shell so we don't lose our start location.
    (
        cd $top
        echo bootstrapping in `pwd`

        # remove any stale config stuff
        doit rm -f config.cache config.status

        # Use auxdir if set in configure.in.
        auxdir=${auxdir-`fgrep CONFIG_AUX_DIR configure.in | \
            sed 's,^.*(,,;s,).*$,,'`}
        test -n "$auxdir" || auxdir=$srcdir
        test -d $auxdir || auxdir=.

        doit $LIBTOOLIZE    --force
        doit $ACLOCAL       -I $auxdir

	# automake wants this stuff now, but we generate it later!
	test -f doc/autogen.texi \
	    || echo '@setfilename fnord' > doc/autogen.texi
	test -f config.h.in \
	    || cp config/acconfig.h config.h.in
	    
        doit $AUTOMAKE      --gnu --add-missing

	# remove the fake files so we don't forget to generate them later.
	test "`sed 2q doc/autogen.texi`" != "@setfilename fnord" \
	    || rm -f doc/autogen.texi
	diff config/acconfig.h config.h.in >/dev/null 2>&1 \
	    || rm -f config.h.in

        doit $AUTOCONF
        doit $AUTOHEADER    -l $auxdir
    )
done

# bootstrap ends here
