#! /bin/bash
#  -*- Mode: Shell-script -*-
#
# bootstrap --- maintainer's bootstrap script
#
##  This file is part of AutoGen.
##  AutoGen Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
##
##  AutoGen is free software: you can redistribute it and/or modify it
##  under the terms of the GNU General Public License as published by the
##  Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  AutoGen is distributed in the hope that it will be useful, but
##  WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##  See the GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License along
##  with this program.  If not, see <http://www.gnu.org/licenses/>.

# This script is designed to find any directories which contain a
# configure.ac 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.

PS4='>bsl-${FUNCNAME}> '
top_srcdir=$(
    cd $(dirname $0)/.. >/dev/null
    pwd )
top_builddir=${top_srcdir}

. config/bootstrap.shlib
PS4='>bs-${FUNCNAME}> '

config_tools()
{
    top_srcdir=$(pwd)
    top_builddir=${top_srcdir}
    srcdir=${top_srcdir}
    config_file=configure.ac
    conf_dir=${top_srcdir}/config
    test -f "${srcdir}/${config_file}" || die "${config_file} not in ${srcdir}"

    #  This missing function is used in many places
    #
    export config_file srcdir top_srcdir top_builddir

    #  Check for AutoGen version 5.
    #
    AGexe=$(command -v autogen)
    test -x "$AGexe" || \
        die "AutoGen requires autogen to bootstrap"

    v="$($AGexe --version || :)"
    v=$(echo "$v" | sed 's/.* //;1q')
    case "$v" in
    5.* ) : ;;
    * ) die "OUT OF DATE AutoGen: $v" ;;
    esac

    GDexe=$(command -v getdefs)
    CLexe=$(command -v columns)
    test -x "${GDexe}" -a -x "${CLexe}" || \
        die "autogen support programs are missing"
    export AGexe GDexe CLexe

    # ------------------------------------------------------------------
    # Make sure all of the maintainer tools required for bootstrap are
    # available on the host machine.
    # ------------------------------------------------------------------
    #
    tools="autoconf autoheader aclocal automake libtoolize"
    for f in $tools
    do
        tool=$(command -v ${f}) > /dev/null || die "No $f found"
        eval ${f}_reqver=$(
            set -- $(${tool} --version | sed 1q)
            eval echo \${$#})
        eval $(echo $f | tr '[a-z]' '[A-Z]')=${tool}
    done

    char_mapper=$(command -v char-mapper) 2>/dev/null
    test -x "${char_mapper}" || {
        char_mapper=$(
            cd add-on/char-mapper >&2
            make char-mapper >&2 || die "cannot make char-mapper"
            echo ${PWD}/char-mapper)
    }
    export char_mapper

    echo bootstrapping in ${PWD}
    set +e
}

# 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.
#
pre_local()
{
    BOOTSTRAP=pre
    export BOOTSTRAP

    local PS4='>pbs-${FUNCNAME}> '
    . ${conf_dir}/bootstrap.local ${1+"$@"} || \
        die FAILED: ${conf_dir}/bootstrap.local

    cd autoopts
    PS4='>aobs-${FUNCNAME}> '
    . ./bootstrap.dir aoconf
}

post_local()
{
    for f in $(find . -name bootstrap.dir)
    do  (
        ${setx}
        cd $(dirname $f)
        srcdir=`pwd`
        echo running POST-bootstrap.dir in ${srcdir}
        PS4='>Abs-${FUNCNAME}> '
        . ./bootstrap.dir recursive || :
        )
    done

    local BOOTSTRAP=post
    local PS4='>Abs-${FUNCNAME}> '
    . ${conf_dir}/bootstrap.local ${1+"$@"}
}

filter_autotool_chaff() {
    set +x
    pat='^configure.ac:.*warning'
    while IFS='' read line
    do
        [[ $line =~ $pat ]] && break
        echo "$line"
    done
    pat='running POST-bootstrap.dir in '
    while IFS='' read line
    do
        [[ $line =~ $pat ]] && break
        echo "$line"
    done > autotool-chaff.txt
    local chaffct=$(wc -l < autotool-chaff.txt)
    if (( chaffct <= 4 )) || grep -i -w error autotool-chaff.txt >/dev/null
    then cat autotool-chaff.txt
    else
        echo "<< $chaffct lines of autotool chaff deleted >>"
    fi
    echo "$line"
    cat
}

run_autotools()
{
    # remove any stale config.cache
    doit rm -f config.cache

    test -n "$auxdir" || auxdir=${srcdir}
    test -d $auxdir   || auxdir=.

    doit $LIBTOOLIZE    --force
    doit $ACLOCAL       -I config
    doit $AUTOHEADER
    doit $AUTOMAKE      --gnu --add-missing
    doit $AUTOCONF
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#    M A I N
#
config_tools
( pre_local )
run_autotools 2>&1 | filter_autotool_chaff 1>&2
post_local

trap '' 0
( exit 0 ) ; exit

# Local Variables:
# mode: shell-script
# sh-indentation: 2
# indent-tabs-mode: nil
# End:

# bootstrap ends here
