#! /bin/sh
#  -*- Mode: Shell-script -*- 
# ----------------------------------------------------------------------
# bootstrap --- maintainer's bootstrap script
#
# Author:            Gary V. Vaughan <gary@oranda.demon.co.uk>
# Maintainer:        Gary V. Vaughan <gary@oranda.demon.co.uk>
# Created:           Thu Apr  9 00:40:23 1998
# Last Modified:     Mon Aug 30 12:29:52 1999				      
#            by:     Bruce Korb <korb@datadesign.com>			      
# ----------------------------------------------------------------------
# @(#) $Id: bootstrap,v 1.26 1999/08/31 00:16:09 bkorb Exp $
# ----------------------------------------------------------------------

# Commentary:
#
# This script is designed to find any directories which contain a
# configure.in in script, and to run the autotools programs from each
# of those directories to make sure they are in a state ready to
# 'configure; make; make install'
#
# Often this process involves more than `libtoolize; automake; autoconf',
# so supplementary actions can be placed in a bootstrap.local script
# in the same directory as this script, and anywhere in the source tree
# in bootstrap.dir files.  The bootstrap.local script will be sourced
# twice; first with BOOTSTRAP=pre before the main part is run, and then
# again with BOOTSTRAP=post after the main part has finished.  This makes
# it possible to set up any links or temporary files required for this
# script to work before it has executed, and then remove them when it
# has finished.  The bootstrap.dir files are also sourced, in a random
# order, as they are found in the tree just before the BOOTSTRAP=post
# phase.  This allows a developer to put any peculiar bootstrap actions
# required by individual directories where they can be seen (and not
# forgotten!).
#
# In an ideal world, running this bootstrap script (including any extra
# scripts it executes) should leave a freshly checked out CVS source tree
# in the same state as a freshly unrolled tarball.  In this way, one
# no longer has to maintain generated files under source control, they
# can be generated after checkout using this bootstrap procedure.

# Code:

# Rather than give up at the first failure, set this variable to
# non-zero, so that all the checks are performed before exiting.
DIE=0
 
# 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
 
# Source any local scripts which add to the bootstrap procedure.
# The bootstrap.local script should test the value of the BOOTSTRAP
# environment variable to see whether it should run the sections
# to be called before the main script, or afterwards.
BOOTSTRAP=pre
test -f $wd/bootstrap.local && . $wd/bootstrap.local $@

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

# See whether the configure template requires automake.
ACLOCAL=aclocal
AUTOMAKE=:

sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in \
  | egrep 'A(C|M)_INIT_AUTOMAKE' > /dev/null && AUTOMAKE=automake

# See whether the configure template requires libtool.
LIBTOOLIZE=:
sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in \
  | egrep 'A(C|M)_PROG_LIBTOOL' > /dev/null && LIBTOOLIZE=libtoolize

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 ()
{
  if test "x$1" != x: ; then 
    echo "$@"
    eval "$@"
  fi
}

# 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/gnu/autoconf/autoconf-2.13.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/gnu/autoconf/autoconf-2.13.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/gnu/libtool/libtool-1.3.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/gnu/automake/automake-1.4.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/gnu/automake/automake-1.4.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 $top/$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.cache
    doit rm -f config.cache

    # 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=.

    if sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in \
      | egrep 'A(C|M)_PROG_LIBTOOL' > /dev/null ; then
    doit $LIBTOOLIZE    --force
    fi
    
    if sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in \
      | egrep 'A(C|M)_INIT_AUTOMAKE' > /dev/null ; then
    doit $ACLOCAL       -I $auxdir
    fi

    doit $AUTOHEADER    -l $auxdir
        
    if sed 's,#.*$,,;s,dnl[ \t].*$,,' configure.in \
      | egrep 'A(C|M)_INIT_AUTOMAKE' > /dev/null ; then
    doit $AUTOMAKE      --gnu --add-missing
    fi

    doit $AUTOCONF      -l config/
  )
done

# Execute (NOTE: not source!) any bootstrap.dir scripts we find in
# the source tree.
find . -name bootstrap.dir |
egrep -v 'CVS/Base/'	   |
while read f
do
  (cd `echo $f|sed 's,/[^/]*$,,'`
   echo running bootstrap.dir in `pwd`
   sh ./bootstrap.dir recursive )
done

# Source any local scripts which add to the bootstrap procedure.
# The bootstrap.local script should test the value of the BOOTSTRAP
# environment variable to see whether it should run the sections
# to be called before the main script, or afterwards.
BOOTSTRAP=post
test -f $wd/bootstrap.local && . $wd/bootstrap.local $@

# bootstrap ends here
