#!/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".

MPITRAMPOLINE_CXX=${MPITRAMPOLINE_CXX:-/usr/bin/c++}
MPITRAMPOLINE_CXXFLAGS=${MPITRAMPOLINE_CXXFLAGS:--march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security         -fstack-clash-protection -fcf-protection         -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Wp,-D_GLIBCXX_ASSERTIONS -g -ffile-prefix-map=/build/mpitrampoline/src=/usr/src/debug/mpitrampoline -flto=auto}

case "$1" in
    -showme:command)
        echo "$MPITRAMPOLINE_CXX $MPITRAMPOLINE_CXXFLAGS"
        exit 0
        ;;
    -showme:compile)
        echo "$MPITRAMPOLINE_CXXFLAGS -I/opt/mpitrampoline/include"
        exit 0
        ;;
    -showme:link)
        echo "$MPITRAMPOLINE_CXXFLAGS -L/opt/mpitrampoline/lib -Wl,-rpath,/opt/mpitrampoline/lib -lmpitrampoline -ldl"
        exit 0
        ;;
    -showme:incdirs)
        echo "/opt/mpitrampoline/include"
        exit 0
        ;;
    -showme:libdirs)
        echo "/opt/mpitrampoline/lib"
        exit 0
        ;;
    -showme:libs)
        echo "-lmpitrampoline -ldl"
        exit 0
        ;;
    -showme:version)
        echo "MPItrampoline 5.4.0"
        echo "MPI trampoline"
        echo "https://github.com/eschnett/MPItrampoline"
        exit 0
        ;;
    -showme:help)
        echo "$0 {-showme:{command|compile|link|incdirs|libdirs|libs|version|help}|-show*} args"
        exit 0
        ;;
    -show*)
        echo "$MPITRAMPOLINE_CXX $MPITRAMPOLINE_CXXFLAGS -I/opt/mpitrampoline/include  -L/opt/mpitrampoline/lib -Wl,-rpath,/opt/mpitrampoline/lib -lmpitrampoline -ldl"
        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 $MPITRAMPOLINE_CXX $MPITRAMPOLINE_CXXFLAGS -I/opt/mpitrampoline/include "$@"
elif [ $add_rpath != true ]; then
    exec $MPITRAMPOLINE_CXX $MPITRAMPOLINE_CXXFLAGS -I/opt/mpitrampoline/include  -L/opt/mpitrampoline/lib "$@" -lmpitrampoline -ldl
else
    exec $MPITRAMPOLINE_CXX $MPITRAMPOLINE_CXXFLAGS -I/opt/mpitrampoline/include  -L/opt/mpitrampoline/lib -Wl,-rpath,/opt/mpitrampoline/lib "$@" -lmpitrampoline -ldl
fi
