#!/bin/sh

########################  build.borealis.sh  ###################################
#
#  Build Borealis from source in your sandbox.  Before running this script:
#
#      * You need to check out borealis.
#
#      * Set any environment variables you'd like to have override defaults.
#        There are several packages required to build and run Borealis.
#        The installation directories for these packages are determined by:
#
#            First using the command following arguments:
#
#                -nmstl     <install>
#                -antlr     <install>
#                -mysql     <install>
#                -bdb       <install>
#                -tinydb    <install>
#                -xercesc   <install>
#
#            Secondly by checking these environment variables:
#
#                INSTALL_NMSTL
#                INSTALL_ANTLR
#                INSTALL_MYSQL
#                INSTALL_BDB
#                INSTALL_TINYDB
#                INSTALL_XERCESC
#
#            Thirdly by using default values encoded in this script.
#            Site administrators my want to make a local copy of this script
#            and modify the InstallDefault function to use different values.
#
#            Note that components to access Berkeley-Db, MySql, and TinyDb
#            are optional.  By default those components are not built unless
#            they are specified.
#
#        The CVS_SANDBOX variable can be used to specify where you have
#        checked out the source code.  By default CVS_SANDBOX is set to
#        the value of the HOME variable.
#
#      * If nmstl is not already installed this script can build it.
#        In this case you need to checkout nmstl as well.
#        If the -nmstl argument is not used and INSTALL_NMSTL is not set
#        then this script will build nmstl under your home directory.
#        It will be installed in the ${HOME}/install_nmstl/ directory.
#
#      * -udb   Build the library containing user defined boxes.
#
#      * -test  Build the test area (borealis/utility/test) as well.
#               Borealis will be built before building the the test area.
#               If Borealis has already been built then it will not be rebuilt.
#
#      * -demo  Build the demos (borealis/demo) as well.
#               Borealis will be built before building the the demos.
#               If Borealis has already been built then it will not be rebuilt.
#
#      * -tool  Build the tools (borealis/tool) as well.
#               Borealis will be built before building the the tools.
#               If Borealis has already been built then it will not be rebuilt.
#
#      * -configure  Run configure for the selected components.
#                    Creates Makefiles, but does not run them.
#
#  This script will also set and export the LD_LIBRARY_PATH unless it is
#  already defined.  It will be set to to the libraries needed to run Borealis.
#  You may want to do this in your login script.
#
#  To run Borealis you will also need to add the nmstl bin/ directory to the
#  PATH variable.  If you let this script build it for you it will be in
#  "${HOME}/install_nmstl/bin".  You may want to do this in your login script.
#
#  The return status is 0 when the script runs to completion.
#  If an error is detected (not all may be) the return status is -1.
#...............................................................................



################################################################################
#
InstallDefault()              #  Site specific default settings.
{
#  These settings work at Brown.
#
#  You may want to make a copy of this script and modify it for use at
#  your site.  Note the there is no default location for nmstl.  This
#  is because it's location must be specified as input to this script
#  or nmstl is built locally.
#...............................................................................


INSTALL_ROOT=/pro/borealis/software

if [ -z ${INSTALL_ANTLR} ]; then
    INSTALL_ANTLR=${INSTALL_ROOT}/antlr
fi

if [ -z ${INSTALL_XERCESC} ]; then
    INSTALL_XERCESC=${INSTALL_ROOT}/xerces
fi

#if [ -z ${INSTALL_BDB} ]; then
#    INSTALL_BDB=${INSTALL_ROOT}/bdb
#fi

#if [ -z ${INSTALL_MYSQL} ]; then
#    INSTALL_MYSQL=${INSTALL_ROOT}/mysql
#fi

#if [ -z ${INSTALL_TINYDB} ]; then
#    INSTALL_TINYDB=${INSTALL_ROOT}/tinydb
#fi

return
}



################################################################################
#
Usage()      # Message issued when the command line syntax is bad.
{
#...............................................................................


echo  "USAGE:  build.borealis.sh"
echo  "           -udb     -test     -tool    -configure"
echo  "           -nmstl     <install path>"
echo  "           -antlr     <install path>"
echo  "           -mysql     <install path>"
echo  "           -bdb       <install path>"
echo  "           -tinydb    <install path>"
echo  "           -xercesc   <install path>"

return
}



################################################################################
#
ParseCommandLine()      # Parse command line arguments.
{
#...............................................................................


BUILD_UDB=0
BUILD_TEST=0
BUILD_TOOL=0
BUILD_DEMO=0
CONFIGURE_ONLY=0

while [ $# -ge 1 ]; do
    if [ "$1" = "-test" ]; then
        shift  1
        BUILD_TEST=1

    elif [ "$1" = "-demo" ]; then
        shift  1
        BUILD_DEMO=1

    elif [ "$1" = "-tool" ]; then
        shift  1
        BUILD_TOOL=1

    elif [ "$1" = "-udb" ]; then
        shift  1
        BUILD_UDB=1

    elif [ "$1" = "-configure" ]; then
        shift  1
        CONFIGURE_ONLY=1

    else
        if [ $# -eq 1 ]; then
           echo  "ERROR:  Argument expected for $1."
           exit  -1
        fi

        case $1 in
          -xercesc)  INSTALL_XERCESC=$2;;
            -nmstl)    INSTALL_NMSTL=$2;;
            -antlr)    INSTALL_ANTLR=$2;;
              -bdb)      INSTALL_BDB=$2;;
            -mysql)    INSTALL_MYSQL=$2;;
           -tinydb)   INSTALL_TINYDB=$2;;
                 *)  echo  "Unknown argument: $1 $2"
                     Usage
                     exit  -1    ;;
        esac

        shift 2
    fi
done

return
}



################################################################################
#
ValidateInstall()        # Validate installation libraries.
{
#...............................................................................


echo " INSTALL_ANTLR   = ${INSTALL_ANTLR}"
echo " INSTALL_XERCESC = ${INSTALL_XERCESC}"

if [ -z ${INSTALL_NMSTL} ]; then
    echo   " INSTALL_NMSTL   = ${HOME}/install_nmstl"
fi

if [ ! -z ${INSTALL_NMSTL} ]; then
    echo   " INSTALL_NMSTL   = ${INSTALL_NMSTL}"

    if [ ! -d "${INSTALL_NMSTL}/lib" ]; then
        echo  "ERROR:  Missing nmstl library (${INSTALL_NMSTL}/lib"
        exit  -1
fi; fi

if [ ! -d "${INSTALL_XERCESC}/lib" ]; then
    echo  "ERROR:  Missing xercesc library (${INSTALL_XERCESC}/lib"
    exit  -1
fi

if [ ! -d "${INSTALL_ANTLR}/lib" ]; then
    echo  "ERROR:  Missing antlr library (${INSTALL_ANTLR}/lib"
    exit  -1
fi

if [ ! -z ${INSTALL_BDB} ]; then
    echo " INSTALL_BDB     = ${INSTALL_BDB}"

    if [ ! -d "${INSTALL_BDB}/lib" ]; then
        echo  "ERROR:  Missing Berkeley-Db library (${INSTALL_BDB}/lib"
        exit  -1
fi; fi

if [ ! -z ${INSTALL_MYSQL} ]; then
    echo " INSTALL_MYSQL   = ${INSTALL_MYSQL}"

    if [ ! -d "${INSTALL_MYSQL}/lib" ]; then
        echo  "ERROR:  Missing mysql library (${INSTALL_MYSQL}/lib"
        exit  -1
fi; fi

if [ ! -z ${INSTALL_TINYDB} ]; then
    echo " INSTALL_TINYDB     = ${INSTALL_TINYDB}"

    if [ ! -d "${INSTALL_TINYDB}/lib" ]; then
        echo  "ERROR:  Missing TinyDb library (${INSTALL_TINYDB}/lib"
        exit  -1
fi; fi

echo

return
}



################################################################################
#
Sandbox()        # Establish the users sandbox directory.
{
#...............................................................................


if [ -z "${CVS_SANDBOX}" ]; then
    CVS_SANDBOX=${HOME}
    export  CVS_SANDBOX

elif [ ! -d "${CVS_SANDBOX}" ]; then
   echo  "ERROR:  Invalid CVS_SANDBOX setting:  ${CVS_SANDBOX}."
   exit  -1
fi

echo  " CVS_SANDBOX = ${CVS_SANDBOX}"

return
}



################################################################################
#
BuildNmstl()    # Build nmstl locally.
{
#...............................................................................


#  Configure nmstl for your environment.
#
if [ ! -d "${CVS_SANDBOX}/nmstl" ]; then
    echo  ERROR:  You need to checkout nmstl.
    exit  -1
fi

cd  ${CVS_SANDBOX}/nmstl
autoconf

if [ $? -ne 0 ]; then
    echo  "ERROR:  Borealis setup failed."
    exit  -1
fi

./configure  --prefix=${INSTALL_NMSTL}                #

if [ $? -ne 0 ]; then
    echo  "ERROR:  Nmstl configure failed."
    exit  -1
fi


# Now you can build and install nmstl.
#
if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    # Install nmstl in your home directory.  Then use the same directory path as
    # argument when configuring borealis (below)
    #
    rm  -r -f  ${INSTALL_NMSTL}                           # Remove any prior installation.
    mkdir      ${INSTALL_NMSTL}                           # Any subdirectory of home.

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Could not:  mkdir  ${INSTALL_NMSTL}"
        exit  -1
    fi

    make clean

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Nmstl 'make clean' failed."
        exit  -1
    fi


    make

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Nmstl 'make' failed."
        exit  -1
    fi


    make install

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Nmstl 'make install' failed."
        exit  -1
    fi
fi

return
}                            # end BuildNmstl



################################################################################
#
BuildBorealis()    # Build Borealis.
{
#  Run setup to generate a fresh configure script.  The setup script runs:
#
#      aclocal      - Automatically generate aclocal.m4 from configure.ac
#      libtoolize   - Configures either shared or static libraries.
#      autoheader   - Create a template header for configure.
#      automake --add-missing   automatically create Makefile.in's from Makefile.am's
#      autoconf     - Generate  a  configuration script from `configure.ac'
#                     Output is sent to `configure'.
#
#      configure.ac ->  aclocal      -> aclocal.m4
#
#                                       AC_CONFIG_AUX_DIR=config
#                                    -> config/config.sub   @ /usr/share/libtool
#      configure.ac ->  libtoolize   -> config/config.guess @ /usr/share/libtool
#                                    -> config/ltmain.sh    @ /usr/share/libtool
#
#                                    -> config.h.in
#      configure.ac ->  autoheader   -> autom4te.cache/output.0
#                                    -> autom4te.cache/requests
#                                    -> autom4te.cache/traces.0
#
#                                    -> stamp-h.in
#     */Makefile.am ->               -> */Makefile.in
#      configure.ac ->  automake     -> config/install-sh    @ /usr/share/automake-1.4
#        aclocal.m4 ->               -> config/mkinstalldirs @ /usr/share/automake-1.4
#                                    -> config/ missing      @ /usr/share/automake-1.4
#
#      configure.ac ->  autoconf     -> configure
#        aclocal.m4 ->
#
#       source code ->  configure    -> config.log
#                           |        -> config.status
#                           V
#       config.h.in -> config.status -> config.h
#     */Makefile.in ->               -> */Makefile
#
# config.status
#    Run this file to recreate the current configuration.
#
# config.log
#    Compiler output produced by configure, useful for debugging
#    configure, is in config.log if it exists.
#
# stamp-h.in
#    It always contains the string `timestamp'.  It is used as a timestamp
#    file indicating whether `config.in' is up to date.
#...............................................................................


if [ ! -d "${CVS_SANDBOX}/borealis" ]; then
    echo  ERROR:  You need to checkout borealis.
    exit  -1
fi

cd  ${CVS_SANDBOX}/borealis/src

if [ $? -ne 0 ]; then
    echo  "ERROR:  Could not:  cd  ${CVS_SANDBOX}/borealis/src"
    exit  -1
fi


./setup

if [ $? -ne 0 ]; then
    echo  "ERROR:  Borealis setup failed."
    exit  -1
fi


# Configure Borealis.
#
# "wtf" comes with nmstl and pretty-prints all gcc error messages (great tool)!
# It translates acronyms and filename suffixes for you.
# The "--enable-shared --disable-static" flags should prevent the
# compiler from generating static libraries.
#
WITH_OPTION=

if [ ! -z ${INSTALL_BDB} ]; then
    WITH_OPTION="--with-bdb=${INSTALL_BDB}"
fi

if [ ! -z ${INSTALL_MYSQL} ]; then
    WITH_OPTION="${WITH_OPTION} --with-mysql=${INSTALL_MYSQL}"
fi

if [ ! -z ${INSTALL_TINYDB} ]; then
    WITH_OPTION="${WITH_OPTION} --with-tinydb=${INSTALL_TINYDB}"
fi

if [ ${BUILD_UDB} -eq 1 ]; then
    WITH_OPTION="${WITH_OPTION} --enable-udb"
fi

wtf ./configure --enable-shared --disable-static   \
     --with-antlr=${INSTALL_ANTLR}                 \
     --with-nmstl=${INSTALL_NMSTL}                 \
     --with-xercesc=${INSTALL_XERCESC}             \
     ${WITH_OPTION}


#
# Compile Borealis
#
if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    make  clean

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis 'make clean' failed."
        exit  -1
    fi


    make

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis 'make' failed."
        exit  -1
    fi
fi

return
}                            # end BuildBorealis



################################################################################
#
BuildTest()    # Build the test area.
{
#...............................................................................


#
#  Run setup to generate a fresh configure script.
#
if [ ! -d "${CVS_SANDBOX}/borealis/utility/test" ]; then
    echo  ERROR:  You need to checkout the Borealis test area.
    exit  -1
fi

cd  ${CVS_SANDBOX}/borealis/utility/test

if [ $? -ne 0 ]; then
    echo  "ERROR:  Could not:  cd  ${CVS_SANDBOX}/borealis/utility/test"
    exit  -1
fi

./setup

if [ $? -ne 0 ]; then
    echo  "ERROR:  Borealis test area setup failed."
    exit  -1
fi


#
# Configure the Borealis test area.
#
WITH_OPTION=

if [ ! -z ${INSTALL_BDB} ]; then
    WITH_OPTION="--with-bdb=${INSTALL_BDB}"
fi

wtf ./configure --enable-shared --disable-static   \
     --with-nmstl=${INSTALL_NMSTL}                 \
     --with-xercesc=${INSTALL_XERCESC}             \
     --with-antlr=${INSTALL_ANTLR}                 \
     ${WITH_OPTION}


#
# Compile the Borealis test area.
#
if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    make  clean

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis test area 'make clean' failed."
        exit  -1
    fi

    make

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis test area 'make' failed."
        exit  -1
    fi
fi

return
}                            # end BuildTest



################################################################################
#
BuildTool()    # Build the Borealis tools.
{
#...............................................................................


#  Run setup to generate a fresh configure script.
#
if [ ! -d "${CVS_SANDBOX}/borealis/tool" ]; then
    echo  ERROR:  You need to checkout the Borealis tool directory.
    exit  -1
fi

cd  ${CVS_SANDBOX}/borealis/tool

if [ $? -ne 0 ]; then
    echo  "ERROR:  Could not:  cd  ${CVS_SANDBOX}/borealis/tool"
    exit  -1
fi

./setup

if [ $? -ne 0 ]; then
    echo  "ERROR:  Borealis tool setup failed."
    exit  -1
fi


#
# Configure the Borealis tool build.
#
wtf ./configure --enable-shared --disable-static   \
     --with-nmstl=${INSTALL_NMSTL}                 \
     --with-xercesc=${INSTALL_XERCESC}             \
     --with-antlr=${INSTALL_ANTLR}


#
# Compile the Borealis tools.
#
if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    make  clean

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis tool 'make clean' failed."
        exit  -1
    fi

    make

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis tool 'make' failed."
        exit  -1
    fi
fi

return
}                            # end BuildTool



################################################################################
#
BuildDemo()    # Build the Borealis demos.
{
#...............................................................................


#  Run setup to generate a fresh configure script.
#
if [ ! -d "${CVS_SANDBOX}/borealis/demo" ]; then
    echo  ERROR:  You need to checkout the Borealis demo directory.
    exit  -1
fi

cd  ${CVS_SANDBOX}/borealis/demo

if [ $? -ne 0 ]; then
    echo  "ERROR:  Could not:  cd  ${CVS_SANDBOX}/borealis/demo"
    exit  -1
fi

./setup

if [ $? -ne 0 ]; then
    echo  "ERROR:  Borealis demo setup failed."
    exit  -1
fi


#
# Configure the Borealis tool build.
#
wtf ./configure --enable-shared --disable-static   \
     --with-nmstl=${INSTALL_NMSTL}                 \
     --with-xercesc=${INSTALL_XERCESC}             \
     --with-antlr=${INSTALL_ANTLR}


#
# Compile the Borealis demos.
#
if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    make  clean

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis demo 'make clean' failed."
        exit  -1
    fi

    make

    if [ $? -ne 0 ]; then
        echo  "ERROR:  Borealis demo 'make' failed."
        exit  -1
    fi
fi

return
}                            # end BuildDemo



################################################################################
############################### main entry point ###############################
################################################################################


InstallDefault                    # Default installation directories.
ParseCommandLine  $@              # Parse the build options.
ValidateInstall                   # Validate installed libraries.


# Set up the LD_LIBRARY_PATH to contain the libraries needed to run Borealis.
# You may want to put this in your login script.
#
if [ -z ${LD_LIBRARY_PATH} ]; then
    LD_LIBRARY_PATH=${INSTALL_ANTLR}/lib
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_XERCESC}/lib

    if [ ! -z ${INSTALL_BDB} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_BDB}/lib
    fi

    if [ ! -z ${INSTALL_MYSQL} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_MYSQL}/lib
    fi

    if [ ! -z ${INSTALL_NMSTL} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_NMSTL}/lib

    else
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${HOME}/install_nmstl/lib
    fi

    export  LD_LIBRARY_PATH

else
    LD_LIBRARY_PATH=${INSTALL_ANTLR}/lib
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_XERCESC}/lib

    if [ ! -z ${INSTALL_BDB} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_BDB}/lib
    fi

    if [ ! -z ${INSTALL_MYSQL} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_MYSQL}/lib
    fi

    if [ ! -z ${INSTALL_NMSTL} ]; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${INSTALL_NMSTL}/lib

    else
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${HOME}/install_nmstl/lib
    fi
fi

echo "LD_LIBRARY_PATH = ${LD_LIBRARY_PATH}"

Sandbox                          # Locate the CVS_SANDBOX.

BUILD_SET=""

# Optionally build and install nmstl.
#
if [ -z ${INSTALL_NMSTL} ]; then
    INSTALL_NMSTL=${HOME}/install_nmstl
    BuildNmstl
    BUILD_SET="Nmstl, "
fi


# Add the nmstl bin/ directory to your path.
# If you installed locally it will be in "${HOME}/install_nmstl/bin".
# You may want to set this up in your login script.
#
PATH=${PATH}:${INSTALL_NMSTL}/bin


if [ ${BUILD_TEST} -eq 0 ]  &&                   \
   [ ${BUILD_DEMO} -eq 0 ]  &&                   \
   [ ${BUILD_TOOL} -eq 0 ]; then

    BuildBorealis
    BUILD_SET="${BUILD_SET}Borealis "

else
    if [ ! -f "${CVS_SANDBOX}/borealis/src/src/borealis" ]; then
        BuildBorealis
        BUILD_SET="${BUILD_SET}Borealis, "
    fi

    if [ ${BUILD_TEST} -ne 0 ]; then
        BuildTest
        BUILD_SET="${BUILD_SET}Tests"

        if [ ${BUILD_DEMO} -ne 0 ]  ||  [ ${BUILD_TOOL} -ne 0 ]; then
            BUILD_SET="${BUILD_SET}, "
        fi
    fi

    if [ ${BUILD_DEMO} -ne 0 ]; then
        BuildDemo
        BUILD_SET="${BUILD_SET}Demos"

        if [ ${BUILD_TOOL} -ne 0 ]; then
            BUILD_SET="${BUILD_SET}, "
        fi
    fi

    if [ ${BUILD_TOOL} -ne 0 ]; then
        BuildTool
        BUILD_SET="${BUILD_SET}Tools"
    fi
fi

if [ ${CONFIGURE_ONLY} -eq 0 ]; then
    echo  "<<<  build.borealis.sh:  Successfully built:  ${BUILD_SET} >>>"

else
    echo  "<<<  build.borealis.sh:  Successfully configured:  ${BUILD_SET} >>>"
fi

exit  0

#
############################  end build.borealis.sh  ###########################