#!/bin/sh
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@

{
	PATH=${PATH}:/bin:/usr/bin

	if [ "$HPMPI_MPICC_EXECED" != "1" ] ; then
		if [ ! -z "${HPMPI_CC:-}" ] ; then
			MPI_CC="$HPMPI_CC"
		fi
	else
		if [ ! -z "${MPI_CXX:-}" ] ; then
			MPI_CC="$MPI_CXX"
		fi
		if [ ! -z "${HPMPI_CXX:-}" ] ; then
			MPI_CC="$HPMPI_CXX"
		fi
	fi
	mpicc=${MPI_CC:-""}
	sed=$(which sed 2>/dev/null)

	mpih=${MPI_ROOT}/include

	palevel=linux_amd64
	prelib=""
	postlib=""
	ldopt=""
	linker=1
	evalcmd=eval
	mpiCClib=-lmpiCC

        if [ "$HPMPI_MPICC_EXECED" != "1" ] ; then
                set -- "$@" ${MPI_CC_OPTIONS:-}
        else
                set -- "$@" ${MPI_CXX_OPTIONS:-}
        fi
	args=""
	while [ $# -ge 1 ] ; do
		arg="$1"
		case "$arg" in
		-c | -E | -P | -S)	linker=0;;
		-ldmpi)			arg="";;
		-lmtmpi)		arg="";;
		-lmtmpi_hmp)		arg="";;
		-non-standard-ext)      arg="";;
		-notv)			arg="";;
		-show)			evalcmd=echo; arg="";;
		-mpiCClib)		mpiCClib="$2"; shift; arg="";;
		-mpicxxlib)		mpiCClib="$2"; shift; arg="";;
		-mpicc)
			if [ "$HPMPI_MPICC_EXECED" != "1" ] ; then
				mpicc="$2"
			fi
			shift; arg="";;
		-mpicxx)
			if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
				mpicc="$2"
			fi
			shift; arg="";;

		-mpi32)
			palevel=linux_ia32; arg="";;
		-mpi64)
			arg="";;
		-static | -Wl,*-static)
			;;

		*\'* | *\\)
			if [ ! -z "$sed" ] ; then
				arg=$(printf "%s\n" "$arg" | $sed \
					-e 's/\([^a-zA-Z0-9/._]\)/\\\1/g');
			else
				arg=\""$arg"\"
			fi ;;
		*[!a-zA-Z0-9/._]*)
			arg=\'"$arg"\' ;;
		esac
		shift

		args="$args $arg"
	done

	if [ -z "$mpicc" ] ; then
		complist="icc ecc pgcc pathcc gcc"
		if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
			complist="icc ecc pgCC pathCC g++"
		fi
		dirs=$(IFS=: ; set -- $PATH ; echo "$@")
		for dir in $dirs ; do
			for comp in $complist ; do
				if [ -z "$mpicc" ] ; then
					if [ -x "$dir/$comp" ] ; then
						mpicc="$dir/$comp"
					fi
				fi
			done
		done
		if [ -z "$mpicc" ] ; then
			echo "Error: no C compiler found;" \
				"please specify one manually,"
			if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
				echo "for example using MPI_CXX."
			else
				echo "for example using MPI_CC."
			fi
			exit 1
		fi
	fi

	mpilib=${MPI_ROOT}/lib/${palevel}

#
# Add in the mpi compilation options.
#
	if [ $linker -eq 1 ]; then
		prelib="${prelib} -L${mpilib}"

#
# Link with the MPI thread-compliant libraries or the regular libraries?
#
		postlib="-lmtmpi -lpthread -ldl"

		if [ "$HPMPI_MPICC_EXECED" = "1" ] ; then
			case "/$mpicc" in
			*/icc)
				postlib="${postlib} -Kc++" ;;
			esac
		fi

	fi

#
# Submit the resulting compilation command
#

	cmd="$mpicc $prelib $postlib $args -I${mpih} $ldopt"

	$evalcmd "$cmd"
	ret=$?

	exit $ret
}
