#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2018-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     have_kahip
#
# Description
#     Detection/setup of KAHIP
#
# Requires
#     config.sh/kahip
#
# Functions provided
#     have_kahip, no_kahip, echo_kahip, query_kahip
#
# Variables set on success
#     HAVE_KAHIP
#     KAHIP_ARCH_PATH
#     KAHIP_INC_DIR
#     KAHIP_LIB_DIR
#
#------------------------------------------------------------------------------
. ${WM_PROJECT_DIR:?}/wmake/scripts/sysFunctions    # General system functions

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

# Reset variables
no_kahip()
{
    unset HAVE_KAHIP KAHIP_ARCH_PATH KAHIP_INC_DIR KAHIP_LIB_DIR
    unset KAHIP_VERSION
}


# Report
echo_kahip()
{
    echo "kahip=${HAVE_KAHIP:-false}"
    echo "root=$KAHIP_ARCH_PATH"
    echo "include=$KAHIP_INC_DIR"
    echo "library=$KAHIP_LIB_DIR"
}


# Query settings
query_kahip()
{
    local config="config.sh/kahip"
    local settings

    if settings="$("$WM_PROJECT_DIR"/bin/foamEtcFile -mode=o "$config")"
    then
        . "$settings"
        _process_query kahip "$KAHIP_ARCH_PATH"
    else
        echo "(no $config settings)" 1>&2
        echo "kahip=unknown"
    fi
}


# On success, return 0 and export variables
# -> HAVE_KAHIP, KAHIP_ARCH_PATH, KAHIP_INC_DIR, KAHIP_LIB_DIR
have_kahip()
{
    local warn="==> skip kahip"
    local config="config.sh/kahip"
    local settings

    if settings="$("$WM_PROJECT_DIR"/bin/foamEtcFile "$config")"
    then
        . "$settings"
    else
        [ -n "$warn" ] && echo "$warn (no $config settings)"
        return 1
    fi

    # Expected location, include/library names
    local prefix="$KAHIP_ARCH_PATH"
    local incName="kaHIP_interface.h"
    local libName="libkahip"
    local header library

    # ----------------------------------
    if isNone "$prefix"
    then
        [ -n "$warn" ] && echo "$warn (disabled)"
        return 1
    elif hasAbsdir "$prefix"
    then
        header=$(findFirstFile "$prefix/include/$incName")
        library=$(findExtLib "$libName")
    elif isSystem "$prefix"
    then
        header=$(findSystemInclude -name="$incName")
        prefix=$(sysPrefix "$header")
    else
        unset prefix
    fi
    # ----------------------------------

    # Header
    [ -n "$header" ] || {
        [ -n "$warn" ] && echo "$warn (no header)"
        return 2
    }

    # Library
    [ -n "$library" ] \
    || library=$(findLibrary -prefix="$prefix" -name="$libName") \
    || {
        [ -n "$warn" ] && echo "$warn (no library)"
        return 2
    }

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

    # kahip itself is 32-bit int, but our interface itself handles some
    # 64-bit conversion (mesh size).

    echo "kahip (label=32) - $prefix"
    export HAVE_KAHIP=true
    export KAHIP_ARCH_PATH="$prefix"
    export KAHIP_INC_DIR="${header%/*}"     # Basename
    export KAHIP_LIB_DIR="${library%/*}"    # Basename
}


# Reset variables
no_kahip

# Test/query
case "$1" in
-test)
    have_kahip
    echo_kahip
    ;;
-query)
    query_kahip
    ;;
esac

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