#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2021 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# File
#     etc/config.sh/paraview
#
# Description
#     Setup file for paraview-[4-5].x
#     Sourced from OpenFOAM-<VERSION>/etc/bashrc or from foamPV alias
#
# Note
#     The env. variables 'ParaView_DIR' and 'ParaView_MAJOR'
#     are required for building plugins
#------------------------------------------------------------------------------

# Clean the PATH
cleaned=$($WM_PROJECT_DIR/bin/foamCleanPath "$PATH" \
        "$ParaView_DIR \
         $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/cmake- \
         $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/paraview-" \
        ) \
        && PATH="$cleaned"

# Determine the cmake to be used. Take the most recent.
unset CMAKE_HOME CMAKE_ROOT
for cmake in $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/cmake-*
do
    if [ -d $cmake ]
    then
        if [ -z $CMAKE_HOME ] || \
            $WM_PROJECT_DIR/bin/tools/foamVersionCompare $CMAKE_HOME lt $cmake
        then
            export CMAKE_HOME=$cmake
            export CMAKE_ROOT=$cmake
        fi
    fi
done
if [ -n $CMAKE_HOME ]
then
    export PATH=$cmake/bin:$PATH
fi

#- ParaView version, automatically determine major version
#export ParaView_VERSION=4.0.1
#export ParaView_VERSION=4.1.0
#export ParaView_VERSION=4.3.1
#export ParaView_VERSION=4.4.0
#export ParaView_VERSION=5.0.0
#export ParaView_VERSION=5.0.1
#export ParaView_VERSION=5.4.0
#export ParaView_VERSION=5.5.0
export ParaView_VERSION=5.6.3
export ParaView_MAJOR=detect

#export ParaView_GL=system
export ParaView_GL=mesa

# Evaluate command-line parameters for ParaView
_foamParaviewEval()
{
    while [ $# -gt 0 ]
    do
        case "$1" in
        ParaView*=*)
            # name=value  -> export name=value
            eval "export $1"
            ;;
        esac
        shift
    done
}

# Evaluate command-line parameters
_foamParaviewEval $@

# Set MAJOR version to correspond to VERSION
# ParaView_MAJOR is "<digits>.<digits>" from ParaView_VERSION
case "$ParaView_VERSION" in
"$ParaView_MAJOR".* )
    # Version and major appear to correspond
    ;;

[0-9]*)
    # Extract major from the version
    ParaView_MAJOR=$(echo $ParaView_VERSION | \
                   sed -e 's/^\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/')
    ;;
esac
export ParaView_VERSION ParaView_MAJOR

# Set the binary and source directories
export ParaView_DIR=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/ParaView-$ParaView_VERSION
paraviewSrcDir=$WM_THIRD_PARTY_DIR/ParaView-$ParaView_VERSION

# Set paths if binaries or source are present
if [ -d $ParaView_DIR -o -d $paraviewSrcDir ]
then
    export ParaView_INCLUDE_DIR=$ParaView_DIR/include/paraview-$ParaView_MAJOR

    paraviewArch=
    if [ -d $ParaView_DIR/lib64 ]
    then
        paraviewArch=64
    fi
    paraviewLibSubDir=
    if $WM_PROJECT_DIR/bin/tools/foamVersionCompare $ParaView_VERSION lt 5.5.0
    then
        paraviewLibSubDir=/paraview-$ParaView_MAJOR
    fi
    export ParaView_LIB_DIR=$ParaView_DIR/lib$paraviewArch$paraviewLibSubDir

    export PATH=$ParaView_DIR/bin:$PATH
    export LD_LIBRARY_PATH=$ParaView_LIB_DIR:$LD_LIBRARY_PATH
    export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-$ParaView_MAJOR

    if [ "$FOAM_VERBOSE" -a "$PS1" ]
    then
        echo "Using paraview"
        echo "    ParaView_DIR         : $ParaView_DIR"
        echo "    ParaView_LIB_DIR     : $ParaView_LIB_DIR"
        echo "    ParaView_INCLUDE_DIR : $ParaView_INCLUDE_DIR"
        echo "    PV_PLUGIN_PATH       : $PV_PLUGIN_PATH"
    fi

    # Add in python libraries if required
    paraviewPython=$ParaView_DIR/Utilities/VTKPythonWrapping
    if [ -r $paraviewPython ]
    then
        if [ "$PYTHONPATH" ]
        then
            export PYTHONPATH=$PYTHONPATH:$paraviewPython:$ParaView_LIB_DIR
        else
            export PYTHONPATH=$paraviewPython:$ParaView_LIB_DIR
        fi
    fi

    # Alias paraview to launch with mesa if necessary
    if [ "$ParaView_GL" = mesa ]
    then
        alias paraview='LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ParaView_LIB_DIR/mesa paraview'
    fi
else
    unset PV_PLUGIN_PATH
fi

unset _foamParaviewEval
unset cleaned cmake paraviewInstDir paraviewPython

#------------------------------------------------------------------------------
