#!/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++-#DEFAULT_GCC_VERSION# --version >/dev/null 2>&1; then
	prog=gcc-#DEFAULT_GCC_VERSION#
elif g++-13 --version >/dev/null 2>&1; then
	prog=gcc-13
elif g++-12 --version >/dev/null 2>&1; then
	prog=gcc-12
elif g++-11 --version >/dev/null 2>&1; then
	prog=gcc-11
elif g++-10 --version >/dev/null 2>&1; then
	prog=gcc-10
elif 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 clang-17 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-17 is available." >&2
        echo "       Use 'nvcc -ccbin clang-17' to use that instead." >&2
        exit 1
elif clang-16 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-16 is available." >&2
        echo "       Use 'nvcc -ccbin clang-16' to use that instead." >&2
        exit 1
elif clang-15 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-15 is available." >&2
        echo "       Use 'nvcc -ccbin clang-15' to use that instead." >&2
        exit 1
elif clang-14 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-14 is available." >&2
        echo "       Use 'nvcc -ccbin clang-14' to use that instead." >&2
        exit 1
elif clang-13 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-13 is available." >&2
        echo "       Use 'nvcc -ccbin clang-13' to use that instead." >&2
        exit 1
elif clang-12 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-12 is available." >&2
        echo "       Use 'nvcc -ccbin clang-12' to use that instead." >&2
        exit 1
elif clang-11 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-11 is available." >&2
        echo "       Use 'nvcc -ccbin clang-11' to use that instead." >&2
        exit 1
elif clang-10 --version >/dev/null 2>&1; then
        echo "ERROR: No supported gcc/g++ host compiler found, but clang-10 is available." >&2
        echo "       Use 'nvcc -ccbin clang-10' to use that instead." >&2
        exit 1
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 "$@"
