#!/bin/sh
cd ${0%/*} || exit 1    # run from this directory
makeType=${1:-libso}

wmakeCheckPwd "$WM_PROJECT_DIR/src" || {
    echo "Error: Current directory is not \$WM_PROJECT_DIR/src"
    echo "    The environment variables are inconsistent with the installation."
    echo "    Check the OpenFOAM entries in your dot-files and source them."
    exit 1
}

[ -n "$FOAM_EXT_LIBBIN" ] || {
    echo "Error: FOAM_EXT_LIBBIN not set"
    echo "    Check the OpenFOAM entries in your dot-files and source them."
    exit 1
}

set -x
set -e

# update OpenFOAM version strings if required
wmakePrintBuild -check || /bin/rm -f OpenFOAM/Make/*/global.? 2>/dev/null

wmakeLnInclude OpenFOAM
wmakeLnInclude OSspecific/${WM_OSTYPE:-POSIX}

OSspecific/${WM_OSTYPE:-POSIX}/Allwmake

case $WM_COMPILER in
*mingw* )
    MINGW=TRUE

    # Cyclic dependency with libOpenFOAM.dll, so link
    # with objects on first pass
    wmake libo Pstream/dummy
    ;;
* )
    Pstream/Allwmake $*
    ;;
esac

wmake $makeType OpenFOAM

if [ "TRUE" = "$MINGW" ]; then
    # Can now link against libOpenFOAM.dll
    Pstream/Allwmake $*

    # Force relink of libOpenFOAM.dll against libPstream.dll
    touch OpenFOAM/global/global.Cver
    export LINK_PLIB=TRUE
    wmake $makeType OpenFOAM

    # mingw excludes default compiler path so 
    # missing /usr/include/FlexLexer.h for flex
    cp /usr/include/FlexLexer.h "$FOAM_SRC/OpenFOAM/lnInclude/."
fi

gpu/Allwmake $*

wmake $makeType fileFormats
wmake $makeType triSurface
wmake $makeType meshTools
wmake $makeType edgeMesh
wmake $makeType surfMesh

# Decomposition methods needed by dummyThirdParty
parallel/decompose/AllwmakeLnInclude

if [ "TRUE" != "$MINGW" ]; then
# Cyclic dependencies with decompositionMethods, so don't try building for MINGW
# dummyThirdParty (dummy metisDecomp, scotchDecomp etc) needed by e.g. meshTools
dummyThirdParty/Allwmake $*
fi

wmake $makeType finiteVolume
wmake $makeType lagrangian/basic

# Build the proper scotchDecomp, metisDecomp etc.
parallel/Allwmake $*

wmake $makeType lagrangian/distributionModels
wmake $makeType genericPatchFields

wmake $makeType conversion

wmake $makeType sampling

wmake $makeType dynamicMesh
wmake $makeType dynamicFvMesh
wmake $makeType topoChangerFvMesh

wmake $makeType ODE
wmake $makeType randomProcesses

thermophysicalModels/Allwmake $*
transportModels/Allwmake $*
turbulenceModels/Allwmake $*

# Depends on RAS/turbulenceModels/compressible
# hence not in thermophysicalModels
wmake $makeType thermophysicalModels/chemistryModel
wmake $makeType thermophysicalModels/solidChemistryModel
regionModels/Allwmake $*

wmake $makeType combustionModels
lagrangian/Allwmake $*
postProcessing/Allwmake $*
mesh/Allwmake $*

fvAgglomerationMethods/Allwmake $*

wmake $makeType fvMotionSolver
wmake $makeType engine

# ----------------------------------------------------------------- end-of-file
