#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2024 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
#
#------------------------------------------------------------------------------

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

# Detect the most recent version of cmake available and add to the PATH
cmakeDir=$(_foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/cmake-*)
if [ -n "$cmakeDir" ]
then
    export PATH=$cmakeDir/bin:$PATH
fi
unset cmakeDir

# Set up the paraview environment
case "$ParaView_TYPE" in
none)
    ;;

system)

    # Look for a paraview installation
    if pvserverExe=$(which pvserver 2> /dev/null)
    then
        paraviewBinDir=$(dirname $pvserverExe)
        paraviewBinDir=$(cd $paraviewBinDir && pwd -P)
    fi

    # Set the environment
    if [ -d "$paraviewBinDir" ]
    then
        export ParaView_DIR=$(dirname $paraviewBinDir)
        export ParaView_LIB_DIR=$(unset LD_LIBRARY_PATH && \
                                  ldd $paraviewBinDir/paraview | \
                                  grep -o "/.*/libpqCore-pv.*.so" | \
                                  xargs dirname)
        export ParaView_VERSION=$(unset LD_LIBRARY_PATH && \
                                  pvserver --version 2> /dev/null | \
                                  awk '{print $NF}')
        export ParaView_MAJOR=${ParaView_VERSION%.*}
        export ParaView_INCLUDE_DIR=$ParaView_DIR/include/paraview-$ParaView_MAJOR
        export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the library path
        export LD_LIBRARY_PATH=$ParaView_LIB_DIR:$PV_PLUGIN_PATH:$LD_LIBRARY_PATH
    fi

    unset pvserverExe paraviewBinDir
    ;;

paraviewopenfoam)

    # Look for a paraview installation. The version should be set externally.
    if [ -n "$ParaView_VERSION" ]
    then
        export ParaView_MAJOR=${ParaView_VERSION%.*}
        paraviewDir=/opt/paraviewopenfoam$(echo "$ParaView_MAJOR" | tr -d '.')
    fi

    # Set the environment
    if [ -d "$paraviewDir" ]
    then
        export ParaView_DIR=$paraviewDir
        export ParaView_LIB_DIR=$(echo "$ParaView_DIR"/lib* | tr ' ' ':')
        export ParaView_INCLUDE_DIR=$ParaView_DIR/include/paraview-$ParaView_MAJOR
        export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the path and the library path
        export PATH=$ParaView_DIR/bin:$PATH
        export LD_LIBRARY_PATH=$ParaView_LIB_DIR:$PV_PLUGIN_PATH:$LD_LIBRARY_PATH
    fi

    unset paraviewDir
    ;;

OpenFOAM | ThirdParty)

    # Look for a paraview installation
    if [ -z "$ParaView_VERSION" ]
    then
        paraviewDir=$(_foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/ParaView-*)
    else
        paraviewDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/ParaView-$ParaView_VERSION
    fi

    # Set the environment
    if [ -d "$paraviewDir" ]
    then
        export ParaView_DIR=$paraviewDir
        export ParaView_LIB_DIR=$(echo "$ParaView_DIR"/lib* | tr ' ' ':')
        export ParaView_VERSION=${paraviewDir##*ParaView-}
        export ParaView_MAJOR=${ParaView_VERSION%.*}
        export ParaView_INCLUDE_DIR=$paraviewDir/include/paraview-$ParaView_MAJOR
        export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the path and the library path
        export PATH=$ParaView_DIR/bin:$PATH
        export LD_LIBRARY_PATH=$ParaView_LIB_DIR:$PV_PLUGIN_PATH:$LD_LIBRARY_PATH

        # 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
        unset paraviewPython
    fi

    unset paraviewDir
    ;;

esac

# Report
if [ "$FOAM_VERBOSE" ] && [ "$PS1" ] && [ -d "$ParaView_DIR" ]
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

unset cleaned

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