#!/bin/sh
#
#	HP High Performance Systems Division
#	(c)Copyright 1997 Hewlett-Packard Co.
#
#	$Revision: 5922 $
#
#	Function:	- compile MPI F77 code
#
#	"@(#)HP MPI 02.02.05.00 (10/03/2006) Linux x86-64"
#

{
	PATH=${PATH}:/bin:/usr/bin:/usr/pgi/linux86-64/5.1/bin:/opt/pgi/linux86-64/5.1/bin

        if [ "$HPMPI_MPIF77_EXECED" != "1" ] ; then
                if [ ! -z "${HPMPI_F77:-}" ] ; then
                        MPI_F77="$HPMPI_F77"
                fi
        else
                if [ ! -z "${MPI_F90:-}" ] ; then
                        MPI_F77="$MPI_F90"
                fi
                if [ ! -z "${HPMPI_F90:-}" ] ; then
                        MPI_F77="$HPMPI_F90"
                fi
        fi
	mpif77=${MPI_F77:-""}
	sed=$(which sed 2>/dev/null)
        mpiroot=${MPI_ROOT:-""}
        if [ -z "$mpiroot" ] ; then
                mpiroot=$(cd $(dirname "$0")/.. ; pwd)
        fi
        if [ ! -x ${mpiroot}/bin/mpirun ] ; then
                mpiroot=/opt/hpmpi
        fi
        if [ ! -x ${mpiroot}/bin/mpirun ] ; then
                mpiroot=/opt/mpi
        fi

	case "$0" in
	*.mpich)
		mpiroot=${mpiroot}/MPICH1.2
		mpichmode=1 ;;
	*)
		mpichmode=0 ;;

	esac

	mpih=${mpiroot}/include/64

	palevel=linux_amd64
	prelib=""
	postlib=""
	ldopt=""
	dmpi=0
	mtmpi=0
	addpthread=0
	notv=0
	compile_only=0
	parallel=0
	autodbl_isi8=0
	autodbl_isr8=0
	autodbl_isr16=0
	evalcmd=eval
	linknompimalloc=0

	args=""
        while [ $# -ge 1 ] ; do
                arg="$1"
		case "$arg" in
		-c)		compile_only=1;;
		-ldmpi)		dmpi=1; arg="";;
		-lmtmpi)	mtmpi=1; arg="";;
		-lmtmpi_hmp)	mtmpi=1; arg="";;
		-notv)		notv=1; arg="";;
		-i8)		autodbl_isi8=1;;
		-r8)		autodbl_isr8=1;;
		-autodouble)	autodbl_isr8=1;;

		-show)		evalcmd=echo; arg="";;
		-nompimalloc)	linknompimalloc=1; arg="";;
		-mpif77)
			if [ "$HPMPI_MPIF77_EXECED" != "1" ] ; then
				mpif77="$2"
			fi
			shift; arg="";;
		-mpif90)
			if [ "$HPMPI_MPIF77_EXECED" = "1" ] ; then
				mpif77="$2"
			fi
			shift; arg="";;

		-mpi32)
			palevel=linux_ia32; mpih=${mpiroot}/include/32;arg="";;
		-mpi64)
			arg="";;
                -static | -Wl,*-static)
                        addpthread=1 ;;

		*\'* | *\\)
			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 "$mpif77" ] ; then
		mpif77=
		if [ -z "$mpif77" ] ; then
			mpif77=$(which ifort 2>/dev/null)
		fi
		if [ -z "$mpif77" ] ; then
			mpif77=$(which pgf90 2>/dev/null)
		fi
		if [ -z "$mpif77" ] ; then
                        if [ "$HPMPI_MPIF77_EXECED" = "1" ] ; then
				mpif77=f90
			else
				mpif77=g77
			fi
		fi
	fi

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

	exists_mpio=0
	exists_tvobj=0
	tvobj=""

	if [ -r ${mpilib}/libmpio.a -o -r ${mpilib}/libmpio.so ]; then
		exists_mpio=1
	fi

	if [ $mtmpi -eq 0 ]; then
		tvobj=${mpilib}/tv.o
	else
		tvobj=${mpilib}/mttv.o
	fi
	if [ -r $tvobj ]; then
		exists_tvobj=1
	fi

	autodbl_objs=
	if [ $autodbl_isi8 -eq 1 -a -r ${mpilib}/hpmpautodbl_isi8.o ] ; then
		autodbl_objs="${autodbl_objs} ${mpilib}/hpmpautodbl_isi8.o"
	fi
	if [ $autodbl_isr8 -eq 1 -a -r ${mpilib}/hpmpautodbl_isr8.o ] ; then
		autodbl_objs="${autodbl_objs} ${mpilib}/hpmpautodbl_isr8.o"
	fi
	if [ $autodbl_isr16 -eq 1 -a -r ${mpilib}/hpmpautodbl_isr16.o ] ; then
		autodbl_objs="${autodbl_objs} ${mpilib}/hpmpautodbl_isr16.o"
	fi

#
# Add in the mpi compilation options.
#
	if [ $compile_only -eq 0 ]; then
		if [ $notv -eq 1 -a $exists_tvobj -eq 1 ]; then
			prelib="${prelib} $tvobj"
		fi
		if [ ! -z "$autodbl_objs" ]; then
			prelib="${prelib} $autodbl_objs"
		fi
		if [ $linknompimalloc -eq 1 ] ; then
			mpilib=${mpilib}/nomalloc:${mpilib}
		fi
		prelib="${prelib} -L${mpilib}"

		if [ $mtmpi -eq 1 ]; then

			postlib="-lmtmpi -lpthread -ldl"

			if [ $dmpi -eq 1 ]; then
				 postlib="-ldmpi ${postlib}"
			fi
		else
			postlib="-lhpmpi -ldl"

			if [ $mpichmode -eq 1 ]; then
				postlib="-Wl,-rpath-link,${mpilib} -lmpich -ldl"
			fi

			if [ $dmpi -eq 1 ]; then
				postlib="-ldmpi ${postlib}"
			fi

			if [ $exists_mpio -ne 0 ]; then
				mpiolib="-lhpmpio"

				postlib="${mpiolib} ${postlib}"
			fi
		fi

                if [ $addpthread -eq 1 -a $mtmpi -eq 0 ] ; then
                        postlib="${postlib} -lpthread"
                fi

	fi

#
# Submit the resulting compilation command
#
	cmd="$mpif77 $prelib $args -I${mpih} $postlib $ldopt"

	$evalcmd "$cmd"
	ret=$?

	exit $ret
}
