#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, licensed under GNU General Public License
#     <http://www.gnu.org/licenses/>.
#
# File
#     etc/config.sh/paraview
#     - sourced by OpenFOAM-*/etc/bashrc or via foamPV alias
#
# Description
#     Setup for PARAVIEW (partially cmake, qt too)
#
#     To disable use of the PV_PLUGIN_PATH:
#         ParaView_VERSION=none
#
#     For system-wide installations:
#         cmake_version=cmake-system
#         ParaView_QT=qt-system
#         ParaView_VERSION=system       [EXPERIMENTAL]
#
# Note
#     The following env. variables are required for building plugins:
#         ParaView_DIR
#         PV_PLUGIN_PATH
#     The OpenFOAM plugin must be the first in PV_PLUGIN_PATH and have
#     paraview-major.minor encoded in its name.
#
#     For central installations not located under ThirdParty, the environment
#     values must be set manually. For example,
#
#         export ParaView_DIR=/opt/paraview/paraview-5.6.0
#         export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-5.6
#
#         export PATH=$ParaView_DIR/bin:$PATH
#         export LD_LIBRARY_PATH=$ParaView_DIR/lib:$LD_LIBRARY_PATH
#         unset ParaView_VERSION        # skip ThirdParty settings
#
#     In paraview-5.4 and earlier, the libraries were in the sub-directory:
#
#         export LD_LIBRARY_PATH=$ParaView_DIR/lib/paraview-5.4:$LD_LIBRARY_PATH
#
# Note
#     When _foamAddLib is unset (eg, called from makeParaView or from foamPV):
#       - the ParaView_VERSION variable is retained.
#------------------------------------------------------------------------------
# USER EDITABLE PART: Changes made here may be lost with the next upgrade

ParaView_VERSION=5.6.0
ParaView_QT=qt-system
cmake_version=cmake-system

# END OF (NORMAL) USER EDITABLE PART
#------------------------------------------------------------------------------
pv_api=auto             # Either auto or pair of (major.minor) digits

# Compiler-specific location for ThirdParty installations
archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"

# Clean PATH and LD_LIBRARY_PATH
eval \
    "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=PATH \
    $ParaView_DIR $archDir/ParaView- $archDir/qt- $archDir/cmake-)"

eval \
    "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=LD_LIBRARY_PATH \
    $ParaView_DIR $archDir/ParaView- $archDir/qt-)"

# ThirdParty cmake
cmake="$archDir/$cmake_version"
if [ -r "$cmake/bin/cmake" ]
then
    # _foamAddPath not available when foamPV function is used
    PATH="$cmake/bin:$PATH"
fi

# Evaluate command-line parameters for ParaView
for i
do
    case "$i" in
    (ParaView*=*)
        # name=value  -> export name=value
        eval "export $i"
        ;;
    esac
done

case "$ParaView_VERSION" in
('')
    # empty - do nothing
    ;;

(none)
    unset ParaView_DIR PV_PLUGIN_PATH

    if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
    then
        echo "Using paraview (disabled)" 1>&2
    fi
    ;;

(system)
    # Obtain major.minor from `paraview --version`
    pv_api="$(paraview --version 2>/dev/null | \
        sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p')"

    if [ -n "$pv_api" ]
    then
        export PV_PLUGIN_PATH="$FOAM_LIBBIN/paraview-$pv_api"
    else
        unset ParaView_DIR PV_PLUGIN_PATH
    fi

    if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
    then
        if [ -n "$PV_PLUGIN_PATH" ]
        then
            echo "Using paraview (system)" 1>&2
            echo "    PV_PLUGIN_PATH       : $PV_PLUGIN_PATH" 1>&2
        else
            echo "system paraview (not found)" 1>&2
        fi
    fi
    ;;

(*)
    # ParaView_VERSION set and non-empty

    # Set API to correspond to VERSION
    # pv_api is <digits>.<digits> from ParaView_VERSION
    case "$ParaView_VERSION" in
    ("$pv_api".*)
        # API and VERSION appear to correspond
        ;;

    ([0-9]*)
        # Extract API from VERSION
        pv_api=$(echo "$ParaView_VERSION" | \
            sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p')
        ;;
    esac

    export ParaView_DIR="$archDir/ParaView-$ParaView_VERSION"

    # Set paths if binaries are present
    if [ -r "$ParaView_DIR" ]
    then
        # The OpenFOAM plugin directory must be the first in PV_PLUGIN_PATH
        # and have paraview-major.minor encoded in its name
        export PV_PLUGIN_PATH="$FOAM_LIBBIN/paraview-$pv_api"

        export PATH="$ParaView_DIR/bin:$PATH"
        pvLibDir="$ParaView_DIR/lib/paraview-$pv_api"
        #OBSOLETE? pvPython=$ParaView_DIR/Utilities/VTKPythonWrapping

        #OBSOLETE? # Python libraries as required
        #OBSOLETE? if [ -r $pvPython ]
        #OBSOLETE? then
        #OBSOLETE?    export PYTHONPATH=$PYTHONPATH:${PYTHONPATH:+:}$pvPython:$pvLibDir
        #OBSOLETE? fi

        # QT libraries as required, and Qt5_DIR for the root directory.
        # Another possibility: "qtpaths --qt-version"
        qtDir="$archDir/$ParaView_QT"
        if [ -d "$qtDir" ]
        then
            case "$ParaView_QT" in
            (*-5*)
                export Qt5_DIR="$qtDir"
                ;;
            esac
            for qtLibDir in "$qtDir/lib$WM_COMPILER_LIB_ARCH" "$qtDir/lib"
            do
                if [ -d "$qtLibDir" ]
                then
                    export LD_LIBRARY_PATH="$qtLibDir:$LD_LIBRARY_PATH"
                    break
                fi
            done
        fi

        # ParaView libraries
        # - 5.4 and earlier: lib/paraview-X.X
        # - 5.5 and later:   lib/
        case "$pv_api" in
        (5.[0-4]*)
            export LD_LIBRARY_PATH="$pvLibDir:$LD_LIBRARY_PATH"
            ;;
        (*)
            export LD_LIBRARY_PATH="$ParaView_DIR/lib:$LD_LIBRARY_PATH"
            ;;
        esac

        if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
        then
            echo "Using paraview" 1>&2
            echo "    ParaView_DIR         : $ParaView_DIR" 1>&2
            echo "    library dir          : $pvLibDir" 1>&2
            echo "    PV_PLUGIN_PATH       : $PV_PLUGIN_PATH" 1>&2
        fi
    else
        if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
        then
            echo "No paraview found" 1>&2
            echo "    ParaView_DIR         : $ParaView_DIR" 1>&2
        fi

        unset ParaView_DIR PV_PLUGIN_PATH
    fi
    ;;
esac

unset -f _foamParaviewEval 2> /dev/null
unset cleaned archDir
unset cmake cmake_version
unset pv_api pvLibDir pvPython qtDir qtLibDir

if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
then
    unset ParaView_VERSION ParaView_QT
fi

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