#!/bin/bash

# PETSc depends on the specific MPI version (including release/patch version)
# that it was built against, whether OpenMPI or MPICH.
#
# This script returns a string identifying the required dependency,
# intended to be inserted as a Dependency for the version-specific PETSc -dev packages.

source <(sed "s/=/=\"/; s/$/\"/"  /usr/share/mpi-default-dev/debian_defaults)

if [ "x$ARCH_DEFAULT_MPI_IMPL" != "xopenmpi" ]; then
    # "not OpenMPI", assume MPICH
    MPI_DEV_PACKAGE=libmpich-dev
else
    # OpenMPI
    MPI_DEV_PACKAGE=libopenmpi-dev
fi

MPI_DEB_VERSION=$(dpkg -s $MPI_DEV_PACKAGE | awk '/Version:/ {print $2}')

# extract the current MPI version (drop epoch and debian package version)
MPI_VERSION=$(echo $MPI_DEB_VERSION | sed "s/^.[^:]*://; s/-[^-]*$//")

# "Main" version is upstream version up to the last '.'
MPI_MAIN_VERSION=$(echo $MPI_VERSION | sed "s/.[^.]*$//")
# "Release" version is upstream version after the last '.'
MPI_RELEASE_VERSION=$(echo $MPI_VERSION | sed "s/^.*\.//")

# Hopefully the release version is just a number. Use it to form the "next" version.
MPI_NEXT_VERSION=${MPI_MAIN_VERSION}.$(( $MPI_RELEASE_VERSION + 1 ))

# MPI_DEV_DEPENDS is a string like "libopenmpi-dev (>= 1.10.3), libopenmpi-dev (<< 1.10.4)"
MPI_DEV_DEPENDS="$MPI_DEV_PACKAGE (>= $MPI_VERSION), $MPI_DEV_PACKAGE (<< $MPI_NEXT_VERSION)"

echo $MPI_DEV_DEPENDS
