#
# 
SHELL = /bin/sh
#
F90 = @F90@
F90LINKER = @F90LINKER@
F90SUFFIX = @F90SUFFIX@
# Module suffix.  M and mod are known
MOD = @MOD@
SHELL = /bin/sh
.SUFFIXES:
.SUFFIXES: .o .$(MOD) .$(F90SUFFIX) .f
#
prefix = @prefix@
includedir = @includedir@
includechoice_dir = @includechoice_dir@
includenochoice_dir = @includenochoice_dir@
mpiincdir  = @MPIINCDIR@
libbuild_dir = @libbuild_dir@
includebuild_dir = @includebuild_dir@
top_srcdir = @top_srcdir@
srcdir     = @srcdir@

@VPATH@
#
INSTALL_DATA = @INSTALL_DATA@
#
#
all: @MPIMOD@.$(MOD) mpi-base.$(MOD)
#
# There isn't a single suffix that all compilers accept.  Many take f90 and
# a few insist on it, so that is our default.  Some (e.g., IBM) refuse
# to take anything but f, so we handle that here.
# Because of VPATH, we may also need to make a copy of a few files to
# the local directory as well.
copysrcs:
	@for file in mpi1 mpi2 mpi_constants ; do \
	    if [ ! -s $${file}.${F90SUFFIX} ] ; then \
		cp ${srcdir}/$$file.f90 $${file}.${F90SUFFIX} ; fi ; \
	done
@MPIMOD@.$(MOD): copysrcs \
		mpi.$(F90SUFFIX) @MPICONSTMOD@.$(MOD) \
		@MPIMOD@1.$(MOD) @MPIMOD@2.$(MOD) \
		choice_modules
	@if [ -s mpi.$(F90SUFFIX) ] ; then \
	    $(F90) -c $(FFLAGS) mpi.$(F90SUFFIX) ; \
	else \
	    $(F90) -c $(FFLAGS) ${srcdir}/mpi.$(F90SUFFIX) ; fi
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAME@.a ./mpi.o	
	if [ ! -d ${includebuild_dir}/f90choice ] ; then \
		mkdir ${includebuild_dir}/f90choice ; fi
	cp @MPIMOD@*.$(MOD) ${includebuild_dir}/f90choice
mpi-base.$(MOD): mpi.$(F90SUFFIX) \
	@MPICONSTMOD@.$(MOD) @MPIMOD@1.$(MOD) @MPIMOD@2.$(MOD)
	if [ ! -d .tmp ] ; then mkdir .tmp ; fi
	cp mpi-nochoice.$(F90SUFFIX) .tmp/mpi.$(F90SUFFIX)
	rm -f .tmp/@MPIMOD@2.$(MOD) .tmp/@MPIMOD@1.$(MOD) .tmp/@MPICONSTMOD@.$(MOD)
	@(cd .tmp ; @LN_S@ ../@MPIMOD@1.$(MOD) @MPIMOD@1.$(MOD) ;\
	@LN_S@ ../@MPIMOD@2.$(MOD) @MPIMOD@2.$(MOD) ;\
	@LN_S@ ../@MPICONSTMOD@.$(MOD) @MPICONSTMOD@.$(MOD) ;\
	$(F90) -c $(FFLAGS) ./mpi.$(F90SUFFIX) )
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAMENC@.a .tmp/mpi.o mpi1.o mpi2.o \
		mpi_constants.o
	@cp .tmp/@MPIMOD@.$(MOD) mpi-base.$(MOD)
	@rm -f .tmp/*
	@if [ -d .tmp ] ; then rmdir .tmp ; fi
	if [ ! -d ${includebuild_dir}/f90base ] ; then \
		mkdir ${includebuild_dir}/f90base ; fi
	cp mpi-base.${MOD} ${includebuild_dir}/f90base/@MPIMOD@.${MOD}
	cp @MPICONSTMOD@.${MOD} ${includebuild_dir}/f90base
	cp @MPIMOD@1.${MOD} ${includebuild_dir}/f90base
	cp @MPIMOD@2.${MOD} ${includebuild_dir}/f90base

@MPICONSTMOD@.$(MOD): mpi_constants.$(F90SUFFIX) $(mpiincdir)/mpif.h
	$(F90) $(FFLAGS) -c -I$(mpiincdir) mpi_constants.$(F90SUFFIX)
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAME@.a mpi_constants.o
# Systems that make the module name upper case when the file name is lower
# case make this harder than it should be.
@MPIMOD@1.$(MOD): mpi1.$(F90SUFFIX)
	$(F90) $(FFLAGS) -c mpi1.${F90SUFFIX}
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAME@.a mpi1.o
@MPIMOD@2.$(MOD): mpi2.$(F90SUFFIX)
	$(F90) $(FFLAGS) -c mpi2.${F90SUFFIX}
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAME@.a mpi2.o

# These are for the maintainers only
ExtractRawBindings:
CreateModuleSrc:
${srcdir}/mpi1f.h: ExtractRawBindings
	if [ -x ./ExtractRawBindings ] ; then ./ExtractRawBindings ; fi
	touch mpi1f.h
${srcdir}mpi1__type_s.f90 ${srcdir}/mpi1.f90 ${srcdir}/mpi2.f90: ${srcdir}/mpi1f.h CreateModuleSrc
	if [ -x ./CreateModuleSrc ] ; then \
	    ./CreateModuleSrc -exclude=${srcdir}/exclude ; fi
# to here

# This installation target is suitable when installing Fortran 90 
# support for non-MPICH implementations of MPI.  
install: @MPIMOD@.$(MOD) mpi-base.$(MOD)
	if [ ! -d $(includechoice_dir) ] ; then \
		mkdir $(includechoice_dir ; fi
	for file in (*.$(MOD)) ; do \
	        $(INSTALL_DATA) $$file $(includechoice_dir) ; done
	if [ ! -d $(includenochoice_dir) ] ; then \
		mkdir $(includenochoice_dir) ; fi
	$(INSTALL_DATA) mpi-base.$(MOD) $(includenochoice_dir)/@MPIMOD@.$(MOD)
	$(INSTALL_DATA) @MPIMOD@1.$(MOD) $(includenochoice_dir)
	$(INSTALL_DATA) @MPIMOD@2.$(MOD) $(includenochoice_dir)
	$(INSTALL_DATA) @MPICONSTMOD@.$(MOD) $(includenochoice_dir)

uninstall:


#
# Use -traditional to get the integer*2 types if you perfer that
# Use -types="integer8 real2" if you want to specify specific types
mpi.$(F90SUFFIX): ${srcdir}/mpi1__type_s.f90
	@( MAKE="@MAKE@" ; export MAKE ; \
	${srcdir}/CreateChoiceSrc @CHOICE_TYPE@ @TYPELIST@ -f90suffix=@F90SUFFIX@ )

#
# This MUST use f90 as the suffix, because we use this to find the
# files.  The make command will find the correct file when the suffix is
# different from f90
# This must *not* use srcdir, since each system will generate slightly
# different (based on available data kinds) files
choice_modules: 
	@-for file in `ls mpi[12]__[cilr][a-z0-9]*_[vs]*.${F90SUFFIX}` ; do \
	    if [ ! -s $$file ] ; then \
		echo "No Fortran 90 choice files!" ; ls -l ; break ; \
	    fi ; \
	    filename=`basename $$file .${F90SUFFIX}` ; \
	    @MAKE@ -f Makefile $$filename.$(MOD) ; done

f90modflag:
	@echo "@MODDIRFLAG@"
f90suffix:
	@echo "@F90SUFFIX@"
f90incflag:
	@echo "@F90INC@"
#
# Add the module's object file to the module library as well.
.$(F90SUFFIX).$(MOD): 
	$(F90) $(FFLAGS) -c $<
	@AR@ ${libbuild_dir}/lib@MPIMODLIBNAME@.a $*.o

# If there is ANOTHER f90 suffix, add it here rather than using
# $(F90SUFFIX) in case .f or .f90 is the F90SUFFIX
.f90:
	$(F90) -c $(FFLAGS) $<
	$(F90LINKER) -o $* $(FFLAGS) $*.o
.f:
	$(F90) -c $(FFLAGS) $<
	$(F90LINKER) -o $* $(FFLAGS) $*.o
.f.o:
	$(F90) -c $(FFLAGS) $<

configure: configure.in
	./makeconfigure

# Remove both .f90 and .f incase the Fortran90 compiler does not accept f90
# Ditto for .M and .mod
clean:
	@-rm -f *.o *.M conftest* ${srcdir}/*.M ${srcdir}/*.o *.mod \
		${srcdir}/*.mod ${srcdir}/mpi.f90
	@-rm -f  mpi1__[cilr][a-z0-9]*_[vs]*.f90 \
	       mpi2__[cilr][a-z0-9]*_[vs]*.f90 \
	       mpi.add mpi.f90 mpi-nochoice.f90 \
	       mpi1__[cilr][a-z0-9]*_[vs]*.f \
	       mpi2__[cilr][a-z0-9]*_[vs]*.f \
	       mpi.f mpi-nochoice.f
distclean: clean
	@-rm -f config.log config.status config.cache config.system Makefile
maintainer-clean: distclean
	rm -f  mpi1__type_[sv]*.f90 mpi2__type_[sv]*.f90 mpi1.f90 mpi2.f90
	rm -f mpi1f.h mpi1fv.h mpi2f.h mpi2fv.h mpi1fvs.h mpi1fsv.h \
		mpi1fs.h mpi2fvs.h mpi2fsv.h mpi2fs.h
