#!/bin/sh -e

write() {
cat > conf-mpi.config <<EOF
includedir: "${MPI_INC_DIR}"
libdir: "${MPI_LIB_DIR}"
binpath: "${MPI_BIN_PATH}"
EOF
}

if [ ! -z ${MPI_INC_DIR+x} ] \
    &&  [ ! -z ${MPI_LIB_DIR+x} ] \
    && [ ! -z ${MPI_BIN_PATH+x} ]
then
  write
elif pkg-config --exists ompi
then
  MPI_INC_DIR=$(pkg-config --variable=includedir ompi)
  MPI_LIB_DIR=$(pkg-config --variable=libdir ompi)/lib
  MPI_BIN_PATH=$(pkg-config --variable=libdir ompi)/bin/
  write
elif pkg-config --exists mpich
then
  MPI_INC_DIR=$(pkg-config --variable=includedir mpich)
  MPI_LIB_DIR=$(pkg-config --variable=libdir mpich)
  MPI_BIN_PATH=$(pkg-config --variable=libdir mpich)/../bin/
  write
else
  OPENMPI_INCDIR=$(ls -d -1 /usr/include/openmpi* 2>/dev/null | head -n 1)
  MPICH_INCDIR=$(ls -d -1 /usr/include/mpich* 2>/dev/null | head -n 1)
  if [ -d "${OPENMPI_INCDIR}" ]
  then
    MPI_INC_DIR=${OPENMPI_INCDIR}
    MPI32=/usr/lib/openmpi
    MPI64=/usr/lib64/openmpi
    if [ -d ${MPI32} ]
    then
      MPI=${MPI32}
    elif [ -d ${MPI64} ]
    then
      MPI=${MPI64}
    fi
    if [ -d "${MPI}" ]
    then
      MPI_LIB_DIR=${MPI}/lib
      MPI_BIN_PATH=${MPI}/bin/
      write
    fi
  elif [ -d "${MPICH_INCDIR}" ]
  then
    MPI_INC_DIR=${MPICH_INCDIR}
    MPI32=/usr/lib/mpich
    MPI64=/usr/lib64/mpich
    if [ -d ${MPI32} ]
    then
      MPI=${MPI32}
    elif [ -d "${MPI64}" ]
    then
      MPI=${MPI64}
    fi
    if [ -d "${MPI}" ]
    then
      MPI_LIB_DIR=${MPI}/lib
      MPI_BIN_PATH=${MPI}/bin/
      write
    fi
  fi
fi

if [ ! -f conf-mpi.config ]
then
  echo "Unable to locate a valid MPI installation (openmpi or mpich)."
  echo
  echo "If you have one and it is not detected, you may manually set"
  echo "the following variables and restart this script:"
  echo "* MPI_INC_DIR: include directory containing 'mpi.h', e.g., /usr/include/openmpi"
  echo "* MPI_LIB_DIR: library directory containing 'libmpi.{so,dll}', e.g., /usr/lib/openmpi/lib"
  echo "* MPI_BIN_PATH: path to the 'mpicc' and 'mpirun' binaries, e.g., /usr/lib/openmpi/bin"
  echo
  echo "To install MPI, try: opam depext conf-mpi"
  exit 2
fi

