#!/bin/sh

# Check for g++ to avoid using different versions of gcc and g++ on systems
# with both g++-X and gcc-Y but not g++-Y installed.

prog=false
if g++-9 --version >/dev/null 2>&1; then
	prog=gcc-9
elif g++-8 --version >/dev/null 2>&1; then
	prog=gcc-8
elif g++-7 --version >/dev/null 2>&1; then
	prog=gcc-7
elif g++-6 --version >/dev/null 2>&1; then
	prog=gcc-6
elif g++-5 --version >/dev/null 2>&1; then
	prog=gcc-5
elif clang-9 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-9 is available." >&2
        echo "       Use 'nvcc -ccbin clang-9' to use that instead." >&2
        exit 1
elif clang-8 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-8 is available." >&2
        echo "       Use 'nvcc -ccbin clang-8' to use that instead." >&2
        exit 1
elif clang-7 --version >/dev/null 2>&1; then
	echo "ERROR: No supported gcc/g++ host compiler found, but clang-7 is available." >&2
	echo "       Use 'nvcc -ccbin clang-7' to use that instead." >&2
	exit 1
elif clang-6.0 --version >/dev/null 2>&1; then
	echo "ERROR: No supported gcc/g++ host compiler found, but clang-6.0 is available." >&2
	echo "       Use 'nvcc -ccbin clang-6.0' to use that instead." >&2
	exit 1
else
	echo "ERROR: No supported gcc/g++ host compiler found." >&2
	echo "       Use 'nvcc -ccbin <compiler>' to specify a host compiler." >&2
	exit 1
fi

exec $prog "$@"
