#!/bin/sh
#
#   Boom builder for OPF 1.0
#   Generated by iMatix Boom from standard.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: [OPF]: $* - stop" 1>&2
    else
        echo "boom E: [OPF]: stop" 1>&2
    fi
    exit 1
}
carp() {
    echo "boom I: [OPF]: $*" 1>&2
}
trap 'croak "Interrupted"' INT
trap 'croak "Terminated"' TERM
trap 'croak "Quit"' QUIT
self=$0
MY_VERSION="1.0"
#
#   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 OPF 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 BINARY FILES   ---------------------------

a_build_binary_files() {
:
    carp "Building OPF..."
    #   To prevent compatibility warnings...
    INCDIR=
    LIBDIR=
    export LIBDIR INCDIR
    if [ "$BOOM_TRACE" ]; then
        MY_COPTS=-v
    else
        MY_COPTS=-q
    fi
    if [ ! -z "$_LOCAL_BASE" ]; then
        MY_COPTS="$MY_COPTS -li $_LOCAL_BASE/include -ll $_LOCAL_BASE/lib"
    fi
    [ "$BOOM_TRACE" ] && echo "c $MY_COPTS example_test"
    c $MY_COPTS example_test || croak "\"c $MY_COPTS example_test\" failed"
    [ "$BOOM_TRACE" ] && echo "c $MY_COPTS example"
    c $MY_COPTS example || croak "\"c $MY_COPTS example\" failed"
    [ "$BOOM_TRACE" ] && echo "c $MY_COPTS -r libopf example"
    c $MY_COPTS -r libopf example || croak "\"c $MY_COPTS -r libopf example\" failed"
    [ "$BOOM_TRACE" ] && echo "c $MY_COPTS -L example_test"
    c $MY_COPTS -L example_test || croak "\"c $MY_COPTS -L example_test\" failed"
}

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

a_build_source_packages() {
:
    rm -f _package.lst
    if [ -f "opf/version.h" ]; then
        echo opf/version.h>>_package.lst
    fi
    if [ -f "opf/opf_gen.gsl" ]; then
        echo opf/opf_gen.gsl>>_package.lst
    fi
    if [ -f "opf/opf_gen_class.gsl" ]; then
        echo opf/opf_gen_class.gsl>>_package.lst
    fi
    if [ -f "opf/opf_gen_docs.gsl" ]; then
        echo opf/opf_gen_docs.gsl>>_package.lst
    fi
    if [ -f "opf/opf_classes.pdl" ]; then
        echo opf/opf_classes.pdl>>_package.lst
    fi
    if [ -f "opf/example.opf" ]; then
        echo opf/example.opf>>_package.lst
    fi
    if [ -f "opf/prelude.pdl" ]; then
        echo opf/prelude.pdl>>_package.lst
    fi
    if [ -f "opf/license.gpl" ]; then
        echo opf/license.gpl>>_package.lst
    fi
    if [ -f "opf/project.pdl" ]; then
        echo opf/project.pdl>>_package.lst
    fi
    if [ -f "opf/readme.txt" ]; then
        echo opf/readme.txt>>_package.lst
    fi
    if [ -f "opf/example.icl" ]; then
        echo opf/example.icl>>_package.lst
    fi
    if [ -f "opf/example_test.c" ]; then
        echo opf/example_test.c>>_package.lst
    fi
    if [ -f "opf/example.h" ]; then
        echo opf/example.h>>_package.lst
    fi
    if [ -f "opf/example.c" ]; then
        echo opf/example.c>>_package.lst
    fi
    if [ -f "opf/example.txt" ]; then
        echo opf/example.txt>>_package.lst
    fi
    if [ -f "opf/stamp_generate" ]; then
        echo opf/stamp_generate>>_package.lst
    fi
    if [ -f "opf/configure" ]; then
        echo opf/configure>>_package.lst
    fi
    if [ -f "opf/Makefile.unix" ]; then
        echo opf/Makefile.unix>>_package.lst
    fi
    if [ -f "opf/boomake" ]; then
        echo opf/boomake>>_package.lst
    fi
    if [ -f "opf/configure.bat" ]; then
        echo opf/configure.bat>>_package.lst
    fi
    if [ -f "opf/Makefile.win32" ]; then
        echo opf/Makefile.win32>>_package.lst
    fi
    if [ -f "opf/boomake.bat" ]; then
        echo opf/boomake.bat>>_package.lst
    fi
    if [ -f "opf/boomakew.bat" ]; then
        echo opf/boomakew.bat>>_package.lst
    fi
    echo "Source package successfully generated">stamp_source
    echo "stamp_source">>_package.lst
    carp "Building OPF-$MY_VERSION-src.tar.gz..."
    zip  -rq _package.zip -@<_package.lst
    unzip -q _package.zip -d OPF-$MY_VERSION
    rm -f OPF-$MY_VERSION-src.tar.gz
    tar -cf  OPF-$MY_VERSION-src.tar OPF-$MY_VERSION
    gzip OPF-$MY_VERSION-src.tar
    rm -f OPF-$MY_VERSION-src.zip
    carp "Building OPF-$MY_VERSION-src.zip..."
    zip -lrmq OPF-$MY_VERSION-src.zip OPF-$MY_VERSION
    rm _package.zip
    rm _package.lst
    rm stamp_source
}

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

a_check_all_source_files_exist() {
:
    if [ ! -f "version.h" ]; then
        croak "version.h is missing"
    fi
    if [ ! -f "opf_gen.gsl" ]; then
        croak "opf_gen.gsl is missing"
    fi
    if [ ! -f "opf_gen_class.gsl" ]; then
        croak "opf_gen_class.gsl is missing"
    fi
    if [ ! -f "opf_gen_docs.gsl" ]; then
        croak "opf_gen_docs.gsl is missing"
    fi
    if [ ! -f "opf_classes.pdl" ]; then
        croak "opf_classes.pdl is missing"
    fi
    if [ ! -f "example.opf" ]; then
        croak "example.opf 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 "example.icl" ]; then
        croak "example.icl is missing"
    fi
    if [ ! -f "example_test.c" ]; then
        croak "example_test.c is missing"
    fi
    if [ ! -f "example.h" ]; then
        croak "example.h is missing"
    fi
    if [ ! -f "example.c" ]; then
        croak "example.c is missing"
    fi
    if [ ! -f "example.txt" ]; then
        croak "example.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 BINARY FILES EXIST   ------------------------

a_check_binary_files_exist() {
:
    if [ ! -f "opf_gen.gsl" ]; then
        croak "opf_gen.gsl is missing"
    fi
    if [ ! -f "opf_gen_class.gsl" ]; then
        croak "opf_gen_class.gsl is missing"
    fi
    if [ ! -f "opf_gen_docs.gsl" ]; then
        croak "opf_gen_docs.gsl is missing"
    fi
    if [ ! -f "opf_classes.pdl" ]; then
        croak "opf_classes.pdl is missing"
    fi
    if [ ! -f "libopf.a" ]; then
        croak "libopf.a is missing"
    fi
    if [ ! -f "readme.txt" ]; then
        croak "readme.txt is missing"
    fi
    if [ ! -f "example.h" ]; then
        croak "example.h is missing"
    fi
    if [ ! -f "example.txt" ]; then
        croak "example.txt 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 "version.h" ]; then
        croak "version.h is missing"
    fi
    if [ ! -f "opf_gen.gsl" ]; then
        croak "opf_gen.gsl is missing"
    fi
    if [ ! -f "opf_gen_class.gsl" ]; then
        croak "opf_gen_class.gsl is missing"
    fi
    if [ ! -f "opf_gen_docs.gsl" ]; then
        croak "opf_gen_docs.gsl is missing"
    fi
    if [ ! -f "opf_classes.pdl" ]; then
        croak "opf_classes.pdl is missing"
    fi
    if [ ! -f "example.opf" ]; then
        croak "example.opf 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 "version.h" ]; then
        croak "version.h is missing"
    fi
    if [ ! -f "opf_gen.gsl" ]; then
        croak "opf_gen.gsl is missing"
    fi
    if [ ! -f "opf_gen_class.gsl" ]; then
        croak "opf_gen_class.gsl is missing"
    fi
    if [ ! -f "opf_gen_docs.gsl" ]; then
        croak "opf_gen_docs.gsl is missing"
    fi
    if [ ! -f "opf_classes.pdl" ]; then
        croak "opf_classes.pdl is missing"
    fi
    if [ ! -f "example.opf" ]; then
        croak "example.opf 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 "example.icl" ]; then
        croak "example.icl is missing"
    fi
    if [ ! -f "example_test.c" ]; then
        croak "example_test.c is missing"
    fi
    if [ ! -f "example.h" ]; then
        croak "example.h is missing"
    fi
    if [ ! -f "example.c" ]; then
        croak "example.c is missing"
    fi
    if [ ! -f "example.txt" ]; then
        croak "example.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 OPF 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 opf_gen.gsl $_INSTALL_ROOT/bin/"
    cp opf_gen.gsl $_INSTALL_ROOT/bin/ || croak "\"cp opf_gen.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/opf_gen.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/opf_gen.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/opf_gen.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp opf_gen_class.gsl $_INSTALL_ROOT/bin/"
    cp opf_gen_class.gsl $_INSTALL_ROOT/bin/ || croak "\"cp opf_gen_class.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/opf_gen_class.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/opf_gen_class.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/opf_gen_class.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp opf_gen_docs.gsl $_INSTALL_ROOT/bin/"
    cp opf_gen_docs.gsl $_INSTALL_ROOT/bin/ || croak "\"cp opf_gen_docs.gsl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/opf_gen_docs.gsl"
    chmod 0644 $_INSTALL_ROOT/bin/opf_gen_docs.gsl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/opf_gen_docs.gsl\" failed"
    [ "$BOOM_TRACE" ] && echo "cp opf_classes.pdl $_INSTALL_ROOT/bin/"
    cp opf_classes.pdl $_INSTALL_ROOT/bin/ || croak "\"cp opf_classes.pdl $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/opf_classes.pdl"
    chmod 0644 $_INSTALL_ROOT/bin/opf_classes.pdl || croak "\"chmod 0644 $_INSTALL_ROOT/bin/opf_classes.pdl\" failed"
    mkdir -p $_INSTALL_ROOT/lib/
    [ "$BOOM_TRACE" ] && echo "cp libopf.a $_INSTALL_ROOT/lib/"
    cp libopf.a $_INSTALL_ROOT/lib/ || croak "\"cp libopf.a $_INSTALL_ROOT/lib/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/lib/libopf.a"
    chmod 0644 $_INSTALL_ROOT/lib/libopf.a || croak "\"chmod 0644 $_INSTALL_ROOT/lib/libopf.a\" failed"
    mkdir -p $_INSTALL_ROOT/bin/
    [ "$BOOM_TRACE" ] && echo "cp example.opf $_INSTALL_ROOT/bin/"
    cp example.opf $_INSTALL_ROOT/bin/ || croak "\"cp example.opf $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/example.opf"
    chmod 0644 $_INSTALL_ROOT/bin/example.opf || croak "\"chmod 0644 $_INSTALL_ROOT/bin/example.opf\" failed"
    mkdir -p $_INSTALL_ROOT/doc/OPF/
    [ "$BOOM_TRACE" ] && echo "cp readme.txt $_INSTALL_ROOT/doc/OPF/"
    cp readme.txt $_INSTALL_ROOT/doc/OPF/ || croak "\"cp readme.txt $_INSTALL_ROOT/doc/OPF/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/doc/OPF/readme.txt"
    chmod 0644 $_INSTALL_ROOT/doc/OPF/readme.txt || croak "\"chmod 0644 $_INSTALL_ROOT/doc/OPF/readme.txt\" failed"
    mkdir -p $_INSTALL_ROOT/include/
    [ "$BOOM_TRACE" ] && echo "cp example.h $_INSTALL_ROOT/include/"
    cp example.h $_INSTALL_ROOT/include/ || croak "\"cp example.h $_INSTALL_ROOT/include/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/include/example.h"
    chmod 0644 $_INSTALL_ROOT/include/example.h || croak "\"chmod 0644 $_INSTALL_ROOT/include/example.h\" failed"
    mkdir -p $_INSTALL_ROOT/bin/
    [ "$BOOM_TRACE" ] && echo "cp example.txt $_INSTALL_ROOT/bin/"
    cp example.txt $_INSTALL_ROOT/bin/ || croak "\"cp example.txt $_INSTALL_ROOT/bin/\" failed"
    [ "$BOOM_TRACE" ] && echo "chmod 0644 $_INSTALL_ROOT/bin/example.txt"
    chmod 0644 $_INSTALL_ROOT/bin/example.txt || croak "\"chmod 0644 $_INSTALL_ROOT/bin/example.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 example.opf"
    gsl -q -quiet:1 example.opf || croak "\"gsl -q -quiet:1 example.opf\" failed"
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 example.icl"
    gsl -q -quiet:1 example.icl || croak "\"gsl -q -quiet:1 example.icl\" 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 example.opf"
    gsl -q -quiet:1 example.opf || croak "\"gsl -q -quiet:1 example.opf\" failed"
    [ "$BOOM_TRACE" ] && echo "gsl -q -quiet:1 example.icl"
    gsl -q -quiet:1 example.icl || croak "\"gsl -q -quiet:1 example.icl\" failed"
    echo "Source files successfully generated">stamp_generate
}

# -----------------------   REMOVE GENERATED BINARIES   -----------------------

a_remove_generated_binaries() {
:
    rm -f libopf.a
    rm -f example_test_test.o
    rm -f example_test_test
    rm -f example.o
    rm -f example_test.o
    rm -f example_test
    rm -f example.o
}

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

a_remove_generated_sources() {
:
    rm -f example.icl
    rm -f example_test.c
    rm -f example.h
    rm -f example.c
    rm -f example.txt
    rm -f stamp_generate
    rm -f configure
    rm -f Makefile.unix
    rm -f configure.bat
    rm -f Makefile.win32
    rm -f OPF_*.zip
    rm -f OPF_*.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 "OPF-1.0"
}

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

a_run_regression_tests() {
:
    carp "Running regression tests..."
    if [ "$BOOM_TRACE" ]; then
        RUN_COMMAND="./example_test"
    else
        RUN_COMMAND="./example_test -q"
    fi
    [ "$BOOM_TRACE" ] && echo "$RUN_COMMAND"
    $RUN_COMMAND || croak "\"$RUN_COMMAND\" failed"
    #   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, and"
    echo "    compiles all source files."
echo "compile:"
    echo "  From the raw project sources, compiles all source files without any"
    echo "    code generation."
echo "clean:"
    echo "  When working with the raw project sources, removes all generated"
    echo "    source and binary 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.  Note that binary"
    echo "    distributions can only be created at the product level."
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 "  From the packaged sources, compiles all source files without any"
    echo "    code generation."
echo "clean:"
    echo "  When working with the packaged sources, removes all generated"
    echo "    binary 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."
actions_default
    return
}

actions_stripped() {
echo "build:"
    echo "  From the stripped-down packaged sources, compiles all source files"
    echo "    without any code generation."
echo "clean:"
    echo "  When working with the packaged sources, removes all generated"
    echo "    binary files from the project directory."
actions_default
    return
}

actions_default() {
echo "install:"
    echo "  Installs deliverables into the final or temporary install tree."
    echo "    This action is only valid after a successful build."
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 "$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: build binary files"
            fi
            if [ -z "$exception" ]; then
                a_build_binary_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 source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: build binary files"
            fi
            if [ -z "$exception" ]; then
                a_build_binary_files
            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 "$debug" ]; then
                echo "Action: remove generated binaries"
            fi
            if [ -z "$exception" ]; then
                a_remove_generated_binaries
            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 all source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_all_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 source files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_source_files_exist
            fi
            if [ -n "$debug" ]; then
                echo "Action: build binary files"
            fi
            if [ -z "$exception" ]; then
                a_build_binary_files
            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 binaries"
            fi
            if [ -z "$exception" ]; then
                a_remove_generated_binaries
            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=
        ;;
        *)
            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: build binary files"
            fi
            if [ -z "$exception" ]; then
                a_build_binary_files
            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 binaries"
            fi
            if [ -z "$exception" ]; then
                a_remove_generated_binaries
            fi
            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 binary files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_binary_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 binary files exist"
            fi
            if [ -z "$exception" ]; then
                a_check_binary_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
