#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2026 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
#     config.csh/paraview
#
# Description
#     Setup file for paraview-[4-5].x
#     Sourced from OpenFOAM-<VERSION>/etc/cshrc or from foamPV alias
#
#------------------------------------------------------------------------------

# Clean the PATH
set 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-" \
            `
if ( $status == 0 ) setenv PATH $cleaned

# Detect the most recent version of cmake available and add to the PATH
set cmakeDir=`eval _foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/cmake-*`
if ( "$cmakeDir" != "" ) then
    setenv PATH $cmakeDir/bin:$PATH
endif
unset cmakeDir

# Set up the paraview environment
switch ("$ParaView_TYPE")
case none:

    unset PV_PLUGIN_PATH
    breaksw

case system:

    # Look for a paraview installation
    set pvserverExe=`which pvdataserver`
    if ( $status == 0 ) then
        set paraviewBinDir=`dirname $pvserverExe`
        set paraviewBinDir=`cd $paraviewBinDir && pwd -P`
    endif

    # Set the environment
    if ( $?paraviewBinDir ) then
    if ( -d "$paraviewBinDir" ) then
        setenv ParaView_DIR `dirname $paraviewBinDir`

        setenv ParaView_VERSION `unset LD_LIBRARY_PATH && \
                                 $pvserverExe --version | \
                                 awk '{print $NF}'`
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`

        # Set the ParaView installation
        setenv ParaView_LIB_DIR `unset LD_LIBRARY_PATH && ldd $pvserverExe | \
                                 grep -o "/[^ ]\+/libvtk.*-pv$ParaView_MAJOR\.so[^ ]*" | \
                                 head -n 1 | xargs dirname`
        setenv ParaView_INCLUDE_DIR $ParaView_DIR/include/paraview-$ParaView_MAJOR
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${LD_LIBRARY_PATH}

        # Optionally look for a separate VTK installation
        set vtkMajor=`ldd $pvserverExe | \
                      sed -n "s/[ \t]\+libvtkCommonCore-\([^ ]\+\).so[^ ]* => .*/\1/p"`
        set vtkLibDir=`unset LD_LIBRARY_PATH && ldd $pvserverExe | \
                       grep -o "/[^ ]\+/libvtk.*-$vtkMajor\.so[^ ]*" | \
                       head -n 1 | xargs dirname`
        set vtkIncludeDir=$ParaView_DIR/include/vtk-$vtkMajor
        if ( "$vtkMajor" != "" && -d "$vtkLibDir" && -d "$vtkIncludeDir" ) then
            setenv VTK_LIB_DIR $vtkLibDir
            setenv VTK_INCLUDE_DIR $vtkIncludeDir
            if ( "$VTK_LIB_DIR" != "$ParaView_LIB_DIR" ) then
                setenv LD_LIBRARY_PATH ${VTK_LIB_DIR}:${LD_LIBRARY_PATH}
            endif
        else
            setenv VTK_LIB_DIR $ParaView_LIB_DIR
            setenv VTK_INCLUDE_DIR $ParaView_INCLUDE_DIR
        endif

        # Set the plugin directory
        setenv PV_PLUGIN_PATH $FOAM_LIBBIN/paraview-$ParaView_MAJOR
        setenv LD_LIBRARY_PATH ${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}
    endif
    endif

    unset pvserverExe paraviewBinDir vtkMajor vtkLibDir vtkIncludeDir
    breaksw

case paraviewopenfoam:

    # Look for a paraview installation. The version should be set externally.
    if ( $?ParaView_VERSION ) then
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`
        set paraviewDir=/opt/paraviewopenfoam`echo $ParaView_MAJOR | tr -d '.'`
    endif

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

        setenv VTK_LIB_DIR $ParaView_LIB_DIR
        setenv VTK_INCLUDE_DIR $ParaView_INCLUDE_DIR

        # Add to the path and the library path
        setenv PATH $ParaView_DIR/bin:${PATH}
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}
    endif
    endif

    unset paraviewDir
    breaksw

case OpenFOAM:
case ThirdParty:

    # Look for a paraview installation
    if ( ! $?ParaView_VERSION ) then
        set paraviewDir=`eval _foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/ParaView-*`
    else
        set paraviewDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/ParaView-$ParaView_VERSION
    endif

    # Set the environment
    if ( -d "$paraviewDir" ) then
        setenv ParaView_DIR $paraviewDir
        setenv ParaView_LIB_DIR `echo "$ParaView_DIR"/lib* | tr ' ' ':'`
        setenv ParaView_VERSION `echo $ParaView_DIR | sed 's/.*ParaView-//g'`
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`
        setenv ParaView_INCLUDE_DIR $ParaView_DIR/include/paraview-$ParaView_MAJOR
        setenv PV_PLUGIN_PATH $FOAM_LIBBIN/paraview-$ParaView_MAJOR

        setenv VTK_LIB_DIR $ParaView_LIB_DIR
        setenv VTK_INCLUDE_DIR $ParaView_INCLUDE_DIR

        # Add to the path and the library path
        setenv PATH $ParaView_DIR/bin:${PATH}
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}

        # Add in python libraries if required
        set paraviewPython=$ParaView_DIR/Utilities/VTKPythonWrapping
        if ( -r $paraviewPython ) then
            if ( $?PYTHONPATH ) then
                setenv PYTHONPATH $PYTHONPATH:$paraviewPython:$ParaView_LIB_DIR
            else
                setenv PYTHONPATH $paraviewPython:$ParaView_LIB_DIR
            endif
        endif
        unset paraviewPython
    endif

    unset paraviewDir
    breaksw

endsw

# Report
if ( $?ParaView_DIR ) then
if ( -d "$ParaView_DIR" && $?FOAM_VERBOSE && $?prompt ) then
    echo "Using paraview"
    echo "    ParaView_DIR         : $ParaView_DIR"
    echo "    ParaView_LIB_DIR     : $ParaView_LIB_DIR"
    echo "    ParaView_INCLUDE_DIR : $ParaView_INCLUDE_DIR"
    if ( $ParaView_TYPE == system ) then
        echo "    VTK_LIB_DIR          : $VTK_LIB_DIR"
        echo "    VTK_INCLUDE_DIR      : $VTK_INCLUDE_DIR"
    endif
    echo "    PV_PLUGIN_PATH       : $PV_PLUGIN_PATH"
endif
endif

unset cleaned

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