#!/bin/sh

# We choose to behave like OpenMPI in this respect.
#
# All OpenMPI versions support "-showme". Modern OpenMPI version also
# support "-showme:compile" and "-showme:link".
#
# MPICH supports "-compile-info" and "-link-info".

# The MPI this mpif was built against is only a *default*. Applications link a
# generic -lmpi_abi from $MPIF_MPI_LIBDIR, so the executable's sole direct
# MPI dependency is the ABI library's conventional versioned name
# (libmpi_abi.so.1 / libmpi_abi.1.dylib), the same for every implementation:
# - at link time, MPIF_MPI_PREFIX selects a different default (and rpath);
# - at run time, LD_LIBRARY_PATH / DYLD_LIBRARY_PATH puts another
#   implementation in front of the rpath'd default, same binary, no relink.
# On ELF, -Wl,--enable-new-dtags is -Wl,--enable-new-dtags so the baked
# default is DT_RUNPATH, which LD_LIBRARY_PATH precedes; DT_RPATH would
# shadow the override.
#
# cmake/mpif.pc.in publishes this same inventory as pkg-config metadata, and
# makes the same choices for the same reasons. Every value both bake in comes
# from one CMake variable, so what can drift is the shape of the two link lines;
# ci-scripts/check-pkg-config.sh compares what the two report.

MPIF_FC=${MPIF_FC:-/opt/bin/armv6l-linux-gnueabihf-libgfortran5-cxx11-mpi+mpiabi/arm-linux-gnueabihf-gfortran}
MPIF_FCFLAGS=${MPIF_FCFLAGS:- }
MPIF_MPI_PREFIX=${MPIF_MPI_PREFIX:-/workspace/destdir}
# The MPI's libdir, which is not necessarily $MPIF_MPI_PREFIX/lib -- a
# distribution that uses lib64, or an MPI configured with --libdir, puts it
# elsewhere. What CMake bakes in here is the directory's name relative to the
# prefix, so overriding MPIF_MPI_PREFIX above still redirects this too; set
# MPIF_MPI_LIBDIR instead for an MPI whose layout differs from the build-time
# one, or whose libdir is not under its prefix at all (in which case CMake had
# to bake an absolute path and MPIF_MPI_PREFIX no longer reaches it).
MPIF_MPI_LIBDIR=${MPIF_MPI_LIBDIR:-/opt/arm-linux-gnueabihf/arm-linux-gnueabihf/sys-root/usr/local/lib}
#  is empty unless this is a sanitizer build, and then
# it names the sanitizer runtime and an rpath to it. It comes *first* because
# DT_NEEDED follows link order and ASan refuses to run unless its runtime leads
# the process's initial library list; and it is here, among the libraries,
# rather than in MPIF_FCFLAGS, because find_package(MPI) keeps what the wrapper
# reports as libraries and drops a bare -fsanitize=address from the link line
# while still passing it to the compile. See MPIF_SANITIZE_LINK_STR in
# CMakeLists.txt for both measurements.
MPIF_FCLIBS=${MPIF_FCLIBS:- -L/workspace/destdir/lib -Wl,-rpath,/workspace/destdir/lib -lmpif -L${MPIF_MPI_LIBDIR} -Wl,-rpath,${MPIF_MPI_LIBDIR} -lmpi_abi}

case "$1" in
    -showme:command)
        echo "$MPIF_FC"
        exit 0
        ;;
    -showme:compile)
        echo "$MPIF_FCFLAGS -I/workspace/destdir/include"
        exit 0
        ;;
    -showme:link)
        echo "$MPIF_FCFLAGS  -Wl,--enable-new-dtags -L/workspace/destdir/lib -Wl,-rpath,/workspace/destdir/lib $MPIF_FCLIBS"
        exit 0
        ;;
    -showme:incdirs)
        echo "/workspace/destdir/include"
        exit 0
        ;;
    -showme:libdirs)
        echo "/workspace/destdir/lib"
        exit 0
        ;;
    -showme:libs)
        echo "$MPIF_FCLIBS"
        exit 0
        ;;
    -showme:mpiprefix)
        # The MPI prefix this invocation links by default: the build-time MPI,
        # unless MPIF_MPI_PREFIX in the environment says otherwise. Tests and
        # scripts derive mpiexec and mpicc from this instead of naming an MPI.
        echo "$MPIF_MPI_PREFIX"
        exit 0
        ;;
    -showme:version)
        echo "mpif 1.0.1"
        echo "MPI Fortran bindings"
        echo "https://github.com/eschnett/mpif"
        exit 0
        ;;
    -showme:help)
        echo "$0 {-showme:{command|compile|link|incdirs|libdirs|libs|mpiprefix|version|help}|-show*} args"
        exit 0
        ;;
    -show*)
        echo "$MPIF_FC $MPIF_FCFLAGS -I/workspace/destdir/include  -Wl,--enable-new-dtags -L/workspace/destdir/lib -Wl,-rpath,/workspace/destdir/lib $MPIF_FCLIBS"
        exit 0
        ;;
esac

# Are we linking?
linking=true
for arg in "$@"; do
    if [ "$arg" = "-E" ] || [ "$arg" = "-c" ] || [ "$arg" = "-S" ]; then
        linking=false
        break
    fi
done

# Can we use rpath? (The Darwin linker has an '-r' argument that
# merges object files together. It does not work with '-rpath'.)
add_rpath=true
for arg in "$@"; do
    if [ "$arg" = "-r" ] || [ "$arg" = "-Wl,-r" ]; then
        add_rpath=false
        break
    fi
done

if [ $linking != true ]; then
    exec $MPIF_FC $MPIF_FCFLAGS -I/workspace/destdir/include "$@"
elif [ $add_rpath != true ]; then
    exec $MPIF_FC $MPIF_FCFLAGS -I/workspace/destdir/include  -Wl,--enable-new-dtags -L/workspace/destdir/lib "$@" $MPIF_FCLIBS
else
    exec $MPIF_FC $MPIF_FCFLAGS -I/workspace/destdir/include  -Wl,--enable-new-dtags -L/workspace/destdir/lib -Wl,-rpath,/workspace/destdir/lib "$@" $MPIF_FCLIBS
fi
