#! /bin/echo this-file-should-be-sourced,-not-executed.
## -*- Mode: shell-script -*-

## bootstrap -- bootstrapping script
##
##  This file is part of Complexity.
##  Complexity Copyright (c) 2011-2016 by Bruce Korb - all rights reserved
##
## Complexity 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.
##
## Complexity 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/>.

initialize()
{
    set -e
    PS4='>BS-$FUNCNAME> '
    if test "${xtrace+true}" = true
    then
        TRAPPED=false
        tcode='TRAPPED=true sfile=${BASH_SOURCE[0]} sline=${BASH_LINENO[0]}'
        trap "$tcode"' ; die "failed: ${_:-${FUNCNAME[0]}}"' 0
    fi
    builddir=$PWD
    rm -rf m4 build-aux 2>/dev/null
    mkdir m4 build-aux
    cd ${progdir}
    chmod a+x tests/*.test

    if test -d .git
    then
        git clean -f -x -d .

    elif test -f .sdir
    then
        cd $(<.sdir)

    else
        cd $(dirname "$0")/../complexity || \
            die "Cannot locate source directory"
    fi

    GNULIB_DIR=${GNULIB_DIR:-~gnu/proj/gnulib}
    v=$(bash ${GNULIB_DIR}/build-aux/git-version-gen \
            $progdir/.tarball-version)
    printf %s $v > $progdir/.tarball-version
    cd $progdir/src
    test -f opts.c -a -f opts.h -a opts.c -nt opts.def || \
        autogen opts.def

    if test -f char-types.h && test char-types.h -nt char-types.map
    then
        :
    else
        cm=`command -v char-mapper`
        if test -x "$cm"
        then $cm char-types.map
        else touch char-types.h
        fi
    fi

    cd ${progdir}
    cp $(autoopts-config pkgdatadir)/liboptschk.m4 m4/.
    sed $'1i\\\n'"New in $v - $(date +'%B %Y')"$'\\\n\n' NEWS > XX
    mv -f XX NEWS

    # Trick automake into "allowing" us to use texinfo rules:
    #
    printf '\\input texinfo\n@setfilename complexity.info\n' \
        > doc/complexity.texi

    gnulib_modules=$(
	echo                \
	    close           \
	    fdl             \
	    gendocs         \
	    gpl-3.0         \
	    malloc-posix    \
	    snprintf        \
	    stdbool
    )
    test -d ~gnu/proj/gnulib && \
        export GNULIB_SRCDIR=$(echo ~gnu/proj/gnulib)
    set +e
}

untrap_die() {
    case "$(trap -p 0)" in
    *TRAPPED=true*' die '* )
        trap '' 0
        ;;
    esac
    ln build-aux/* .
}

. ./"$0.std"

oribs=$(echo ~gnu/proj/libtool/bootstrap)
test -f "$oribs" && {
    cmp -s "$oribs" "$0" || {
        rm -f "$0"
        cp -fp "$oribs" "$0"
        exec bash $dasxh "$0" "$@"
    }
}

initialize
func_add_hook func_fini untrap_die
