# SHELL = /bin/sh
RAYDIR=$(shell pwd)
UN=$(shell echo ${USER})
HOST=$(shell echo `hostname`)
EXEC=sw_flux
FOR=gfortran #-mcmodel=medium -fPIC
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 -fbackslash -funroll-all-loops -malign-double -fforce-addr -fno-align-commons # -shared-intel  #-frecursive  #
LD_FLAGS= #-Wl, -v #-Wl,-stack_size,100000000 # option must be passed using: -Wl, -(option)
WARNING=-fbounds-check# -Warray-bounds -Walign-commons -Wargument-mismatch #-Waliasing -Warray-temporaries -Wcharacter-truncation -Wunderflow #-Wconversion #-Wline-truncation #-Wunused #-Wextra # 
DEBUG=
OSV=MacOSX
OSTYPEV=$(shell echo ${OSTYPE})

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)/check_for_errors.for \
	$(DIR)/coordinates.for \
	$(DIR)/error.for \
	$(DIR)/exec.for \
	$(DIR)/geometry.for \
	$(DIR)/is_even.for \
	$(DIR)/is_integer.for \
	$(DIR)/matrix.for \
	$(DIR)/null_collision.for \
	$(DIR)/num2str.for \
	$(DIR)/profiles.for \
	$(DIR)/random_gen.for \
	$(DIR)/rdgfa.for \
	$(DIR)/read_data.for \
	$(DIR)/rotation.for \
	$(DIR)/scattering.for \
	$(DIR)/seed.for \
	$(DIR)/statistics.for \
	$(DIR)/str2num.for \
	$(DIR)/sw_direct.for \
	$(DIR)/sw_flux.for \
	$(DIR)/sw_reverse.for \
	$(DIR)/zufall.for 


INCLUDES= -I./includes

.PHONY: clean all
default: all

#dependencies
#quoi compiler ?
#OBJ_PROG=$(SRC_PROG:$(DIR)/%.for=$(OBJ)/%.o)
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 $(EXEC)
	@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: $(LD_FLAGS)


all :
#	@echo "UN="$(UN)
#	@echo "HOST="$(HOST)
	@echo $(DIR)
	@echo '------ Compilation -------'
	mkdir -p $(OBJ)
	make -f Makefile allend

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

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

runmulti :
	@echo 'Differents MCM stationnaires'
	@echo '/!\Pousuivre si les fichiers *.in existent'
	@echo 'de 1 a 19'
	@echo '#! /bin/sh' > z1.txt
	@echo 'N=1' > z2.txt
	@echo 'FIN=19' > z3.txt
	read leurre
	cat z1.txt z2.txt z3.txt monlotmulti2_sstete.bat > monlotmulti2.bat
	rm z1.txt z2.txt z3.txt
	at now +0 minutes < monlotmulti.bat

#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 '|------------------------------------------|'
