#!/bin/sh
#
#   Boom builder for XNF 2.1c0
#   Generated by iMatix Boom from scripted.pwl
#   
#   Copyright (c) 1996-2009 iMatix Corporation
#   All rights reserved.
#   
#   This file is licensed under the BSD license as follows:
#   
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#   
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of iMatix Corporation nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#   
#   THIS SOFTWARE IS PROVIDED BY IMATIX CORPORATION "AS IS" AND ANY
#   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IMATIX CORPORATION BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
#   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#   Force environment variables to point to install location
#   Start by using final IBASE, then handle local -install path
#
boom_model_list () {
    set |
        awk 'BEGIN                      { FS="[=_]" }
             /^BOOM_MODEL_[A-Z0-9]*=1$/ { print $3  }'
}
boom_model_expand () {
    for MODEL in `boom_model_list`; do
        eval unset BOOM_MODEL_$MODEL
    done
    OLD_IFS=$IFS
    IFS=,
    for MODEL in $BOOM_MODEL; do
        MODEL=`echo $MODEL | tr \[a-z\] \[A-Z\]`
        eval BOOM_MODEL_$MODEL=1
        eval export BOOM_MODEL_$MODEL
    done
    IFS=$OLD_IFS
}
croak() {
    if [ -n "$*" ]; then
        echo "boom E: [XNF]: $* - stop" 1>&2
    else
        echo "boom E: [XNF]: stop" 1>&2
    fi
    exit 1
}
carp() {
    echo "boom I: [XNF]: $*" 1>&2
}
trap 'croak "Interrupted"' INT
trap 'croak "Terminated"' TERM
trap 'croak "Quit"' QUIT
self=$0
MY_VERSION="2.1c0"
#
#   Set default values for BOOM_MODEL
#
#   Default is release,mt on all platforms.
#
if [ -z "$BOOM_MODEL" ]; then
    BOOM_MODEL=release,mt
fi
boom_model_expand
if [ "$BOOM_MODEL_RELEASE" != "1" ]; then
    if [ "$BOOM_MODEL_DEBUG" != "1" ]; then
        BOOM_MODEL_RELEASE=1
        BOOM_MODEL="$BOOM_MODEL,release"
    fi
fi
if [ "$BOOM_MODEL_ST" != "1" ]; then
    if [ "$BOOM_MODEL_MT" != "1" ]; then
            BOOM_MODEL="$BOOM_MODEL,mt"
    fi
fi
if [ -z "$IBASE" ]; then
    carp "The IBASE variable is not set.  Please set it to the location where XNF should be installed."
    exit 1
fi
if [ "$1" = "-v" ]; then
    BOOM_TRACE=1
    export BOOM_TRACE
    shift
fi
if [ "$1" != "-install" ]; then
    #   Normal use, use IBASE
    PATH=.:$IBASE/bin:$PATH; export PATH
    PERLLIB=.:$IBASE/bin:$PERLLIB; export PERLLIB
    INCDIR_ALT=$IBASE/include; export INCDIR_ALT
    _LOCAL_BASE=
    _INSTALL_ROOT=$IBASE
else
    #   Building product, we use -install directory first, then IBASE
    CURDIR=`pwd`
    cd $2
    _LOCAL_BASE=`pwd`
    cd $CURDIR
    PATH=.:$_LOCAL_BASE/bin:$IBASE/bin:$PATH; export PATH
    PERLLIB=.:$_LOCAL_BASE/bin:$IBASE/bin:$PERLLIB; export PERLLIB
    INCDIR_ALT=$_LOCAL_BASE/include:$IBASE/include; export INCDIR_ALT
    shift
    shift
    _INSTALL_ROOT=$_LOCAL_BASE
fi

# -------------------------   BUILD SOURCE PACKAGES   -------------------------

a_build_source_packages() {
:
    rm -f _package.lst
    if [ -f "xnf/xnf.xnf" ]; then
        echo xnf/xnf.xnf>>_package.lst
    fi
    if [ -f "xnf/xnf_base.xnf" ]; then
        echo xnf/xnf_base.xnf>>_package.lst
    fi
    if [ -f "xnf/xnf_gen.gsl" ]; then
        echo xnf/xnf_gen.gsl>>_package.lst
    fi
    if [ -f "xnf/xnf_preproc.gsl" ]; then
        echo xnf/xnf_preproc.gsl>>_package.lst
    fi
    if [ -f "xnf/xnf_inherit.gsl" ]; then
        echo xnf/xnf_inherit.gsl>>_package.lst
    fi
    if [ -f "xnf/xnf_parse.gsl" ]; then
        echo xnf/xnf_parse.gsl>>_package.lst
    fi
    if [ -f "xnf/xnf_produce.gsl" ]; then
        echo xnf/xnf_produce.gsl>>_package.lst
    fi
    if [ -f "xnf/xnf_doc.txt" ]; then
        echo xnf/xnf_doc.txt>>_package.lst
    fi
    if [ -f "xnf/dfl.xnf" ]; then
        echo xnf/dfl.xnf>>_package.lst
    fi
    if [ -f "xnf/dfl_gen.gsl" ]; then
        echo xnf/dfl_gen.gsl>>_package.lst
    fi
    if [ -f "xnf/dfl_preproc.gsl" ]; then
        echo xnf/dfl_preproc.gsl>>_package.lst
    fi
    if [ -f "xnf/dfl_inherit.gsl" ]; then
        echo xnf/dfl_inherit.gsl>>_package.lst
    fi
    if [ -f "xnf/dfl_parse.gsl" ]; then
        echo xnf/dfl_parse.gsl>>_package.lst
    fi
    if [ -f "xnf/dfl_doc.txt" ]; then
        echo xnf/dfl_doc.txt>>_package.lst
    fi
    if [ -f "xnf/prelude.pdl" ]; then
        echo xnf/prelude.pdl>>_package.lst
    fi
    if [ -f "xnf/license.gpl" ]; then
        echo xnf/license.gpl>>_package.lst
    fi
    if [ -f "xnf/project.pdl" ]; then
        echo xnf/project.pdl>>_package.lst
    fi
    if [ -f "xnf/readme.txt" ]; then
        echo xnf/readme.txt>>_package.lst
    fi
    if [ -f "xnf/stamp_generate" ]; then
        echo xnf/stamp_generate>>_package.lst
    fi
    if [ -f "xnf/configure" ]; then
        echo xnf/configure>>_package.lst
    fi
    if [ -f "xnf/Makefile.unix" ]; then
        echo xnf/Makefile.unix>>_package.lst
    fi
    if [ -f "xnf/boomake" ]; then
        echo xnf/boomake>>_package.lst
    fi
    if [ -f "xnf/configure.bat" ]; then
        echo xnf/configure.bat>>_package.lst
    fi
    if [ -f "xnf/Makefile.win32" ]; then
        echo xnf/Makefile.win32>>_package.lst
    fi
    if [ -f "xnf/boomake.bat" ]; then
        echo xnf/boomake.bat>>_package.lst
    fi
    if [ -f "xnf/boomakew.bat" ]; then
        echo xnf/boomakew.bat>>_package.lst
    fi
    echo "Source package successfully generated">stamp_source
    echo "stamp_source">>_package.lst
    carp "Building XNF-$MY_VERSION-src.tar.gz..."
    zip  -rq _package.zip -@<_package.lst
    unzip -q _package.zip -d XNF-$MY_VERSION
    rm -f XNF-$MY_VERSION-src.tar.gz
    tar -cf  XNF-$MY_VERSION-src.tar XNF-$MY_VERSION
    gzip XNF-$MY_VERSION-src.tar
    rm -f XNF-$MY_VERSION-src.zip
    carp "Building XNF-$MY_VERSION-src.zip..."
    zip -lrmq XNF-$MY_VERSION-src.zip XNF-$MY_VERSION
    rm _package.zip
    rm _package.lst
    rm stamp_source
}

# ---------------------   CHECK ALL SOURCE FILES EXIST   ----------------------

a_check_all_source_files_exist() {
:
    if [ ! -f "xnf.xnf" ]; then
        croak "xnf.xnf is missing"
    fi
    if [ ! -f "xnf_base.xnf" ]; then
        croak "xnf_base.xnf is missing"
    fi
    if [ ! -f "xnf_gen.gsl" ]; then
        croak "xnf_gen.gsl is missing"
    fi
    if [ ! -f "xnf_preproc.gsl" ]; then
        croak "xnf_preproc.gsl is missing"
    fi
    if [ ! -f "xnf_inherit.gsl" ]; then
        croak "xnf_inherit.gsl is missing"
    fi
    if [ ! -f "xnf_parse.gsl" ]; then
        croak "xnf_parse.gsl is missing"
    fi
    if [ ! -f "xnf_produce.gsl" ]; then
        croak "xnf_produce.gsl is missing"
    fi
    if [ ! -f "xnf_doc.txt" ]; then
        croak "xnf_doc.txt is missing"
    fi
    if [ ! -f "dfl.xnf" ]; then
        croak "dfl.xnf is missing"
    fi
    if [ ! -f "dfl_gen.gsl" ]; then
        croak "dfl_gen.gsl is missing"
    fi
    if [ ! -f "dfl_preproc.gsl" ]; then
        croak "dfl_preproc.gsl is missing"
    fi
    if [ ! -f "dfl_inherit.gsl" ]; then
        croak "dfl_inherit.gsl is missing"
    fi
    if [ ! -f "dfl_parse.gsl" ]; then
        croak "dfl_parse.gsl is missing"
    fi
    if [ ! -f "dfl_doc.txt" ]; then
        croak "dfl_doc.txt is missing"
    fi
    if [ ! -f "project.pdl" ]; then
        croak "project.pdl is missing"
    fi
    if [ ! -f "readme.txt" ]; then
        croak "readme.txt is missing"
    fi
    if [ ! -f "stamp_generate" ]; then
        croak "stamp_generate is missing"
    fi
    if [ ! -f "configure" ]; then
        croak "configure is missing"
    fi
    if [ ! -f "Makefile.unix" ]; then
        croak "Makefile.unix is missing"
    fi
    if [ ! -f "boomake" ]; then
        croak "boomake is missing"
    fi
    if [ ! -f "configure.bat" ]; then
        croak "configure.bat is missing"
    fi
    if [ ! -f "Makefile.win32" ]; then
        croak "Makefile.win32 is missing"
    fi
    if [ ! -f "boomake.bat" ]; then
        croak "boomake.bat is missing"
    fi
    if [ ! -f "boomakew.bat" ]; then
        croak "boomakew.bat is missing"
    fi
}

# ------------------------   CHECK OPERATING CONTEXT   ------------------------

a_check_operating_context() {
:
    if [ -f ./stamp_stripped ]; then
        event=stripped
    elif [ -f ./stamp_source ]; then
        event=source
    else
        event=repository
    fi
}

# -----------------------   CHECK PROJECT FILES EXIST   -----------------------

a_check_project_files_exist() {
:
    if [ ! -f "xnf.xnf" ]; then
        croak "xnf.xnf is missing"
    fi
    if [ ! -f "xnf_base.xnf" ]; then
        croak "xnf_base.xnf is missing"
    fi
    if [ ! -f "xnf_gen.gsl" ]; then
        croak "xnf_gen.gsl is missing"
    fi
    if [ ! -f "xnf_preproc.gsl" ]; then
        croak "xnf_preproc.gsl is missing"
    fi
    if [ ! -f "xnf_inherit.gsl" ]; then
        croak "xnf_inherit.gsl is missing"
    fi
    if [ ! -f "xnf_parse.gsl" ]; then
        croak "xnf_parse.gsl is missing"
    fi
    if [ ! -f "xnf_produce.gsl" ]; then
        croak "xnf_produce.gsl is missing"
    fi
    if [ ! -f "dfl.xnf" ]; then
        croak "dfl.xnf is missing"
    fi
    if [ ! -f "project.pdl" ]; then
        croak "project.pdl is missing"
    fi
    if [ ! -f "readme.txt" ]; then
        croak "readme.txt is missing"
    fi
    if [ ! -f "boomake" ]; then
        croak "boomake is missing"
    fi
    if [ ! -f "boomake.bat" ]; then
        croak "boomake.bat is missing"
    fi
    if [ ! -f "boomakew.bat" ]; then
        croak "boomakew.bat is missing"
    fi
}

# -----------------------   CHECK SOURCE FILES EXIST   ------------------------

a_check_source_files_exist() {
:
    if [ ! -f "xnf.xnf" ]; then
        croak "xnf.xnf is missing"
    fi
    if [ ! -f "xnf_base.xnf" ]; then
        croak "xnf_base.xnf is missing"
    fi
    if [ ! -f "xnf_gen.gsl" ]; then
        croak "xnf_gen.gsl is missing"
    fi
    if [ ! -f "xnf_preproc.gsl" ]; then
        croak "xnf_preproc.gsl is missing"
    fi
    if [ ! -f "xnf_inherit.gsl" ]; then
        croak "xnf_inherit.gsl is missing"
    fi
    if [ ! -f "xnf_parse.gsl" ]; then
        croak "xnf_parse.gsl is missing"
    fi
    if [ ! -f "xnf_produce.gsl" ]; then
        croak "xnf_produce.gsl is missing"
    fi
    if [ ! -f "xnf_doc.txt" ]; then
        croak "xnf_doc.txt is missing"
    fi
    if [ ! -f "dfl.xnf" ]; then
        croak "dfl.xnf is missing"
    fi
    if [ ! -f "dfl_gen.gsl" ]; then
        croak "dfl_gen.gsl is missing"
    fi
    if [ ! -f "dfl_preproc.gsl" ]; then
        croak "dfl_preproc.gsl is missing"
    fi
    if [ ! -f "dfl_inherit.gsl" ]; then
        croak "dfl_inherit.gsl is missing"
    fi
    if [ ! -f "dfl_parse.gsl" ]; then
        croak "dfl_parse.gsl is missing"
    fi
    if [ ! -f "dfl_doc.txt" ]; then
        croak "dfl_doc.txt is missing"
    fi
    if [ ! -f "project.pdl" ]; then
        croak "project.pdl is missing"
    fi
    if [ ! -f "readme.txt" ]; then
        croak "readme.txt is missing"
    fi
    if [ ! -f "stamp_generate" ]; then
        croak "stamp_generate is missing"
    fi
    if [ ! -f "configure" ]; then
        croak "configure is missing"
    fi
    if [ ! -f "Makefile.unix" ]; then
        croak "Makefile.unix is missing"
    fi
    if [ ! -f "boomake" ]; then
        croak "boomake is missing"
    fi
    if [ ! -f "configure.bat" ]; then
        croak "configure.bat is missing"
    fi
    if [ ! -f "Makefile.win32" ]; then
        croak "Makefile.win32 is missing"
    fi
    if [ ! -f "boomake.bat" ]; then
        croak "boomake.bat is missing"
    fi
    if [ ! -f "boomakew.bat" ]; then
        croak "boomakew.bat is missing"
    fi
}

# -------------------------   INSTALL DELIVERABLES   --------------------------

a_install_deliverables() {
:
    carp "Installing XNF into $_INSTALL_ROOT..."
    if [ ! -d "$_INSTALL_ROOT" ]; then
        if mkdir -p $_INSTALL_ROOT; then
            :
        else
            croak "Could not create directory \"$_INSTALL_ROOT\""
        fi
    fi
    mkdir -p $_INSTALL_ROOT/bin/
    [ "$BOOM_TRACE" ] && echo "cp xnf_base.xnf $_INSTALL_ROOT/bin/"
    cp xnf_base.xnf $_INSTALL_ROOT/bin/ || croak "\"cp xnf_base.xnf $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_base.xnf"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_base.xnf || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_base.xnf\" failed"
    [ "$BOOM_TRACE" ] && echo "cp xnf_gen.gsl $_INSTALL_ROOT/bin/"
    cp xnf_gen.gsl $_INSTALL_ROOT/bin/ || croak "\"cp xnf_gen.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_gen.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_gen.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_gen.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp xnf_preproc.gsl $_INSTALL_ROOT/bin/"
    cp xnf_preproc.gsl $_INSTALL_ROOT/bin/ || croak "\"cp xnf_preproc.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_preproc.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_preproc.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_preproc.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp xnf_inherit.gsl $_INSTALL_ROOT/bin/"
    cp xnf_inherit.gsl $_INSTALL_ROOT/bin/ || croak "\"cp xnf_inherit.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_inherit.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_inherit.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_inherit.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp xnf_parse.gsl $_INSTALL_ROOT/bin/"
    cp xnf_parse.gsl $_INSTALL_ROOT/bin/ || croak "\"cp xnf_parse.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_parse.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_parse.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_parse.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp xnf_produce.gsl $_INSTALL_ROOT/bin/"
    cp xnf_produce.gsl $_INSTALL_ROOT/bin/ || croak "\"cp xnf_produce.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/xnf_produce.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/xnf_produce.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/xnf_produce.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp dfl_gen.gsl $_INSTALL_ROOT/bin/"
    cp dfl_gen.gsl $_INSTALL_ROOT/bin/ || croak "\"cp dfl_gen.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/dfl_gen.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/dfl_gen.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/dfl_gen.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp dfl_preproc.gsl $_INSTALL_ROOT/bin/"
    cp dfl_preproc.gsl $_INSTALL_ROOT/bin/ || croak "\"cp dfl_preproc.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/dfl_preproc.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/dfl_preproc.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/dfl_preproc.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp dfl_inherit.gsl $_INSTALL_ROOT/bin/"
    cp dfl_inherit.gsl $_INSTALL_ROOT/bin/ || croak "\"cp dfl_inherit.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/dfl_inherit.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/dfl_inherit.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/dfl_inherit.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp dfl_parse.gsl $_INSTALL_ROOT/bin/"
    cp dfl_parse.gsl $_INSTALL_ROOT/bin/ || croak "\"cp dfl_parse.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/dfl_parse.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/dfl_parse.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/dfl_parse.gsl\" failed"
    mkdir -p $_INSTALL_ROOT/doc/XNF/
    [ "$BOOM_TRACE" ] && echo "cp readme.txt $_INSTALL_ROOT/doc/XNF/"
    cp readme.txt $_INSTALL_ROOT/doc/XNF/ || croak "\"cp readme.txt $_INSTALL_ROOT/doc/XNF/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/doc/XNF/readme.txt"
    chmod 0644 $_INSTALL_ROOT/doc/XNF/readme.txt || croak "\"chmod 0644 $_INSTALL_ROOT/doc/XNF/readme.txt\" failed"
    if [ -f ./installer ]; then
    [ "$BOOM_TRACE" ] && echo "sh ./installer"
    sh ./installer || croak "\"sh ./installer\" failed"
    fi
}

# ----------------------   PRODUCE ALL GENERATED FILES   ----------------------

a_produce_all_generated_files() {
:
    carp "Generating files..."
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 xnf.xnf"
    gsl -q -quiet:1 xnf.xnf || croak "\"gsl -q -quiet:1 xnf.xnf\" failed"
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 dfl.xnf"
    gsl -q -quiet:1 dfl.xnf || croak "\"gsl -q -quiet:1 dfl.xnf\" failed"
    echo "Source files successfully generated">stamp_generate
}

# ------------------------   PRODUCE GENERATED FILES   ------------------------

a_produce_generated_files() {
:
    carp "Generating files..."
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 xnf.xnf"
    gsl -q -quiet:1 xnf.xnf || croak "\"gsl -q -quiet:1 xnf.xnf\" failed"
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 dfl.xnf"
    gsl -q -quiet:1 dfl.xnf || croak "\"gsl -q -quiet:1 dfl.xnf\" failed"
    echo "Source files successfully generated">stamp_generate
}

# -----------------------   REMOVE GENERATED SOURCES   ------------------------

a_remove_generated_sources() {
:
    rm -f xnf_doc.txt
    rm -f dfl_gen.gsl
    rm -f dfl_preproc.gsl
    rm -f dfl_inherit.gsl
    rm -f dfl_parse.gsl
    rm -f dfl_doc.txt
    rm -f stamp_generate
    rm -f configure
    rm -f Makefile.unix
    rm -f configure.bat
    rm -f Makefile.win32
    rm -f XNF_*.zip
    rm -f XNF_*.tar.gz
}

# ------------------------   REPORT HELP INFORMATION   ------------------------

a_report_help_information() {
:
    echo "The commands you can currently use on this project are:"
    echo ""
    eval "actions_$state"
}

# ------------------------   REPORT PROJECT VERSION   -------------------------

a_report_project_version() {
:
    echo "XNF-2.1c0"
}

# -------------------------   RUN REGRESSION TESTS   --------------------------

a_run_regression_tests() {
:
    #   Run local selftest script if present
    if [ -f ./selftest ]; then
    [ "$BOOM_TRACE" ] && echo "sh ./selftest"
    sh ./selftest || croak "\"sh ./selftest\" failed"
    fi
}

# ----------------------   USE SOURCE CONTROL REVISION   ----------------------

a_use_source_control_revision() {
:
    if [ -d .svn ]; then
        SVN_REVISION="`svn info | awk '/^Revision:/ { print $2 }'`"
    else
        SVN_REVISION="svn"
    fi
    MY_VERSION=${MY_VERSION}-${SVN_REVISION}
}

actions_initial() {
    return
}

actions_initial_check() {
    return
}

actions_repository() {
echo "build:"
    echo "  From the raw project sources, produces all generated files."
echo "compile:"
    echo "  Has no effect, provided for consistency with other workflows."
echo "clean:"
    echo "  Removes all generated source files from the project directory."
echo "regen:"
    echo "  Produces generated files required for building using the"
    echo "    current model."
echo "allgen:"
    echo "  Produces all generated files."
echo "distrib:"
    echo "  Creates a source distribution for the project."
echo "distsrc:"
    echo "  Equivalent to distrib, provided for compatibility."
echo "distrev:"
    echo "  Creates a source distribution for the project.  Builds distribution"
    echo "    packages based on source control version number. Note that binary"
    echo "    distributions can only be created at the product level."
actions_default
    return
}

actions_source() {
echo "build:"
    echo "  When working with the packaged sources, does nothing."
echo "clean:"
    echo "  Has no effect, provided for consistency with other workflows."
echo "regen:"
    echo "  Produces generated files required for building using the"
    echo "    current model."
echo "allgen:"
    echo "  Produces all generated files."
actions_default
    return
}

actions_stripped() {
echo "build:"
    echo "  When working with the packaged sources, does nothing."
echo "clean:"
    echo "  Has no effect, provided for consistency with other workflows."
actions_default
    return
}

actions_default() {
echo "install:"
    echo "  Installs deliverables into the final or temporary install tree."
echo "test:"
    echo "  Runs regression tests for the project."
echo "help:"
    echo "  Shows a list of the commands allowed on the project."
echo "version:"
    echo "  Show project version."
    return
}

#   State machine starts here
#
firsttime=true
while [ "$1" -o "$firsttime" ]; do
  firsttime=
  state=initial
  event=ok
  debug=
  export state event debug
  while [ -n "$state" ]; do
    if [ "$inherit_state" ]; then
        check_state=$inherit_state
        inherit_state=
    else
        check_state=$state
    fi
    case "$check_state" in
    initial)
        if [ -n "$debug" ]; then
            echo "State: initial"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        ok)
            if [ -n "$debug" ]; then
                echo "Event: ok"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check operating context"
            fi
            if [ -z "$exception" ]; then
                a_check_operating_context
            fi
            state=initial_check
            if [ -n "$exception" ]; then
                continue
            fi
        ;;
        *)
            echo "Unknown command - use one of:"
            echo ""
            eval "actions_$state"
            exit 1
        ;;
        esac
    ;;
    initial_check)
        if [ -n "$debug" ]; then
            echo "State: initial check"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        repository)
            if [ -n "$debug" ]; then
                echo "Event: repository"
            fi
            event=
            exception=
            state=repository
            if [ -n "$exception" ]; then
                continue
            fi
        ;;
        source)
            if [ -n "$debug" ]; then
                echo "Event: source"
            fi
            event=
            exception=
            state=source
            if [ -n "$exception" ]; then
                continue
            fi
        ;;
        stripped)
            if [ -n "$debug" ]; then
                echo "Event: stripped"
            fi
            event=
            exception=
            state=stripped
            if [ -n "$exception" ]; then
                continue
            fi
        ;;
        *)
            echo "Unknown command - use one of:"
            echo ""
            eval "actions_$state"
            exit 1
        ;;
        esac
    ;;
    repository)
        if [ -n "$debug" ]; then
            echo "State: repository"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        build)
            if [ -n "$debug" ]; then
                echo "Event: build"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: produce generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_generated_files
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        compile)
            if [ -n "$debug" ]; then
                echo "Event: compile"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        clean)
            if [ -n "$debug" ]; then
                echo "Event: clean"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: remove generated sources"
            fi
            if [ -z "$exception" ]; then
                a_remove_generated_sources
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        regen)
            if [ -n "$debug" ]; then
                echo "Event: regen"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: produce generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_generated_files
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        allgen)
            if [ -n "$debug" ]; then
                echo "Event: allgen"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: produce all generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_all_generated_files
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        distrib)
            if [ -n "$debug" ]; then
                echo "Event: distrib"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: produce all generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_all_generated_files
            fi
            if [ -n "$debug" ]; then
                echo "Action: check all source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_all_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: build source packages"
            fi
            if [ -z "$exception" ]; then
                a_build_source_packages
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        distsrc)
            if [ -n "$debug" ]; then
                echo "Event: distsrc"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: produce all generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_all_generated_files
            fi
            if [ -n "$debug" ]; then
                echo "Action: check all source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_all_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: build source packages"
            fi
            if [ -z "$exception" ]; then
                a_build_source_packages
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        distrev)
            if [ -n "$debug" ]; then
                echo "Event: distrev"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: produce all generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_all_generated_files
            fi
            if [ -n "$debug" ]; then
                echo "Action: check source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: use source control revision"
            fi
            if [ -z "$exception" ]; then
                a_use_source_control_revision
            fi
            if [ -n "$debug" ]; then
                echo "Action: build source packages"
            fi
            if [ -z "$exception" ]; then
                a_build_source_packages
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        *)
            inherit_state=default
        ;;
        esac
    ;;
    source)
        if [ -n "$debug" ]; then
            echo "State: source"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        build)
            if [ -n "$debug" ]; then
                echo "Event: build"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        clean)
            if [ -n "$debug" ]; then
                echo "Event: clean"
            fi
            event=
            exception=
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        regen)
            if [ -n "$debug" ]; then
                echo "Event: regen"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: produce generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_generated_files
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        allgen)
            if [ -n "$debug" ]; then
                echo "Event: allgen"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: produce all generated files"
            fi
            if [ -z "$exception" ]; then
                a_produce_all_generated_files
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        *)
            inherit_state=default
        ;;
        esac
    ;;
    stripped)
        if [ -n "$debug" ]; then
            echo "State: stripped"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        build)
            if [ -n "$debug" ]; then
                echo "Event: build"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check project files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_project_files_exist
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        clean)
            if [ -n "$debug" ]; then
                echo "Event: clean"
            fi
            event=
            exception=
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        *)
            inherit_state=default
        ;;
        esac
    ;;
    default)
        if [ -n "$debug" ]; then
            echo "State: default"
        fi
        case "$event" in
        "")
            if [ -n "$debug" ]; then
                echo "Get event from user"
            fi
            if [ "$1" ]; then
                event=$1
                shift
            else
                event=empty
            fi
            continue
        ;;
        install)
            if [ -n "$debug" ]; then
                echo "Event: install"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: install deliverables"
            fi
            if [ -z "$exception" ]; then
                a_install_deliverables
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        test)
            if [ -n "$debug" ]; then
                echo "Event: test"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: check source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: run regression tests"
            fi
            if [ -z "$exception" ]; then
                a_run_regression_tests
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        help)
            if [ -n "$debug" ]; then
                echo "Event: help"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: report help information"
            fi
            if [ -z "$exception" ]; then
                a_report_help_information
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        version)
            if [ -n "$debug" ]; then
                echo "Event: version"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: report project version"
            fi
            if [ -z "$exception" ]; then
                a_report_project_version
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        empty)
            if [ -n "$debug" ]; then
                echo "Event: empty"
            fi
            event=
            exception=
            if [ -n "$debug" ]; then
                echo "Action: report help information"
            fi
            if [ -z "$exception" ]; then
                a_report_help_information
            fi
            if [ -n "$1" -a -z "$exception" ]; then
                continue 2
            fi
            state=
        ;;
        *)
            echo "Unknown command - use one of:"
            echo ""
            eval "actions_$state"
            exit 1
        ;;
        esac
    ;;
    esac
  done
done
