# SHELL = /bin/sh
RAYDIR=$(shell pwd)
UN=$(shell echo ${USER})
HOST=$(shell echo `hostname`)
EXEC=kspectrum.exe
FOR=mpif77
#FOR=mpifort
MAKE=make
#ARCH=-m486 -msparclite -mips3 # options pour une machine 32 bits
#ARCH=-march=i686 -m64 # options pour un i686 en 64 bits

ARCH=-m64 #-march=i686
OPTI=-O3 -static #-std=legacy #-frepack-arrays #-std=gnu #-fast
MPIINC=/usr/bin/include
MPILD=/usr/bin/lib
WARNING=-fbounds-check -Warray-bounds -Walign-commons #-Wsurprising #-Warray-temporaries
DEBUG=
OSV=MacOSX
OSTYPEV=$(shell echo ${OSTYPE})


#############################
# FFLAGS= -O
# FFLAGS= -ff90
# FFLAGS= -ftrength-reduce (les loop vont plus vite cf gcc)
# FFLAGS= -g -ftrenght-reduce (cf gcc option va unused corteu)
############################Mieux vaut ajuster par des directives de compilation
############################inserer dans le prog quand le compilateur le permet.
FFLAGS = $(ARCH) $(OPTI) $(WARNING) $(IEEE) $(EMUL90) $(DEBUG)

####################################################################################
#   DIR is the path to source files directory				#
#   OBJ is the path to object files directory				#
#   SRC_PROG is the list of source files to be compiled			#
#   INCLUDES is: -I+path to directory that contains 'include' files	#
####################################################################################

DIR=${RAYDIR}/source
OBJ=${RAYDIR}/objects
DAT=${RAYDIR}/data


SRC_PROG = \
	$(DIR)/add_slash_if_absent.for \
	$(DIR)/band_limits.for \
	$(DIR)/BD_TIPS_2011_v1p0.for \
	$(DIR)/calc_k.for \
	$(DIR)/CIA.for \
	$(DIR)/clean.for \
	$(DIR)/co2_cont.for \
	$(DIR)/constantk.for \
	$(DIR)/convert_epsm2ch.for \
	$(DIR)/d1mach.for \
	$(DIR)/density.for \
	$(DIR)/Doppler_linewidth.for \
	$(DIR)/generate_base.for \
	$(DIR)/get_nlines.for \
	$(DIR)/error.for \
	$(DIR)/indexes.for \
	$(DIR)/identify_molecules.for \
	$(DIR)/init.for \
	$(DIR)/is_integer.for \
	$(DIR)/khi.for \
	$(DIR)/kspectrum.for \
	$(DIR)/last_int.for \
	$(DIR)/line_intensity.for \
	$(DIR)/line_truncation.for \
	$(DIR)/lin_interp.for \
	$(DIR)/lineshape.for \
	$(DIR)/look_for_errors.for \
	$(DIR)/Lorentz_linewidth.for \
	$(DIR)/lorentz_tabulation.for \
	$(DIR)/lorentztab_filename.for \
	$(DIR)/max_error.for \
	$(DIR)/modify_iso_index.for \
	$(DIR)/num2str.for \
	$(DIR)/parse_file.for \
	$(DIR)/partial_pressures.for \
	$(DIR)/Planck.for \
	$(DIR)/precalc.for \
	$(DIR)/print_status.for \
	$(DIR)/powercut.for \
	$(DIR)/read_composition.for \
	$(DIR)/read_data.for \
	$(DIR)/read_line_parameters.for \
	$(DIR)/read_isotopic_composition.for \
	$(DIR)/read_molparam.for \
	$(DIR)/read_options.for \
	$(DIR)/read_special_molecules.for \
	$(DIR)/read_truncation_distances.for \
	$(DIR)/set_iso_abundance.for \
	$(DIR)/sgrid.for \
	$(DIR)/sort_all_data.for \
	$(DIR)/ssort.for \
	$(DIR)/strlen.for \
	$(DIR)/temp.for \
	$(DIR)/TIPS.for \
	$(DIR)/utils.for \
	$(DIR)/voigt_tabulation.for \
	$(DIR)/voigttab_filename.for \
	$(DIR)/write_bandk.for \
	$(DIR)/write_bandk_tmp.for \
	$(DIR)/write_info.for

INCLUDES= -I./includes

#dependencies
#quoi compiler ?
OBJ_PROG=$(SRC_PROG:$(DIR)/%.for=$(OBJ)/%.o)
#comment compiler ?
$(OBJ)/%.o: $(DIR)/%.for
	$(FOR) $(FFLAGS) $(INCLUDES) -c -o $@ $(@:$(OBJ)/%.o=$(DIR)/%.for)

to :
	read leurre
	touch *

clean :
	@rm -f *.o $(OBJ)/*.o *.oub *.oup *.oug
	@rm -f *%
	@rm -f *~
	@rm -f $(DIR)/*.~
	@rm -f $(DIR)/*.*~
	@rm -f tmp.tmp
	@rm -f *.sum
	@rm -f *.eps
	@rm -f *.ps
	@rm -f *.jpg
	@rm -f core
	@rm -f last.kumac
	@rm -f paw.metafile
	@rm -f core
	@touch core
	@chmod 000 core
	@chmod a+r core
	@echo 'Files have been removed'

dat :
	@cd ./data;f0;make_data.exe
	@echo 'Program has been compiled'

wipe :
	@rm -f *%
	@rm -f *~
	@rm -f $(DIR)/*.~
	@rm -f $(DIR)/*.*~
	@rm -f tmp.tmp
	@rm -f *.sum
	@rm -f *.eps
	@rm -f *.ps
	@rm -f *.jpg
	@rm -f core
	@rm -f last.kumac
	@rm -f paw.metafile
	@rm -f core
	@touch core
	@chmod 000 core
	@echo 'Files have been removed'

skel :	clean
	rm -f $(EXEC)
	@echo 'Files have been removed'

where :
	@echo Current directory: $(RAYDIR)
	@echo Source directory: $(DIR)

info :
	@echo Host name: $(HOSTNAME)
	@echo Operating system: $(OSV)
	@echo Type of system: $(OSTYPEV)
	@echo User name: $(USER)
	@echo Special compilation options: $(SPEC)


all :
#	@echo "UN="$(UN)
#	@echo "HOST="$(HOST)
#	@echo "ARCH="$(ARCH)
#	@echo "OPTI="$(OPTI)
	@echo $(DIR)
	@echo '------ Compilation -------'
	make -f Makefile kspectrum

kspectrum :	ray
ray :	$(OBJ_PROG)
	@echo '------ Edition de liens -------'
	$(FOR) -o $(EXEC) $(OBJ_PROG) $(SPEC)
	@echo '--- Use program ' $(EXEC)  '(compiled for :' $(OSV) $(OSTYPEV) 'by:' $(USER) ')'
	@date > compdate.in
#	@echo 'ln -s ray'

run :
	@$(EXEC)
	@echo 'Done'

#outils pour debugger autres que print
gbd :
	@echo 'gdb ou xxgdb (ray)'

#outils pour profiling : optimisations scalaire (ou vectorielle)
gprof :
#	@gprof -s -l ray.exe gmon.out > gprof_sl	
	@gprof $(EXEC) gmon.out > gprof_sl
	@more gprof_sl

edit :
	xemacs &

save :
	@sauve.1d
	@echo 'done'

shell :
	xterm

man :
	make help

help :
	@echo '|------------------------------------------|'
	@echo '|------------------------------------------|'
	@echo '|  AIDE-MEMOIRE                            |'
	@echo '|------------------------------------------|'
	@echo '| help, edit, shell                        |'
	@echo '| gdb, gprof for debug                     |'
	@echo '|------------------------------------------|'
	@echo '| clean: remove  (.o, *~, etc.)            |'
	@echo '| skel: remove ' $(EXEC) '                 |'
	@echo '| to, all, run, runmulti                   |'
	@echo '|------------------------------------------|'
