# Main target
PRG=whizard

# Histograms
PLOTS=$(PRG)-plots
# Feynman graphs
GRAPHS=$(PRG)-graphs
# Phase space channels
CHANNELS=$(PRG)-channels
# Grid histograms
GRIDS=$(PRG)-grids
# Plot of integration history
HISTORY=$(PRG)-history

# Directories
GMLDIR=/home/ilc/miyamoto/work/120801-whizard/build/whizard-1.95/gml-src
FMFDIR=/home/ilc/miyamoto/work/120801-whizard/build/whizard-1.95/fmf-src
CONFDIR=/home/ilc/miyamoto/work/120801-whizard/build/whizard-1.95/conf
BINDIR=/home/ilc/miyamoto/work/120801-whizard/build/whizard-1.95/bin
CHCODE=/home/ilc/miyamoto/work/120801-whizard/build/whizard-1.95/processes-src

# General settings
MP=
GML=$(GMLDIR)/gml
WEAVE= -delay -index
LATEX=
DVIPS=
CP=cp -f

# The input files
CONF=$(PRG).in $(PRG).mdl whizard.prc
CUTS=$(PRG).cut0 $(PRG).cut1 $(PRG).cut2 $(PRG).cut3 $(PRG).cut4 $(PRG).cut5
PHS=$(PRG).phx
OUT=\
$(PRG).wgt $(PRG).evx $(PRG).evt $(PRG).out $(PRG).*.out $(PRG).*.cut0 $(PRG).*.dat

CONFIG=whizard.prc
MAKETEST=fulltest.pl

# # The executable 
# prg: $(PRG)
# $(PRG): $(BINDIR)/$(PRG)
# 	$(CP) $< .

# # The configuration files
# conf: $(CONF)
# $(PRG).in: $(CONFDIR)/$(PRG).in
# 	$(CP) $< .
# $(PRG).phs: $(CONFDIR)/$(PRG).phs
# 	$(CP) $< .
# $(PRG).cut1: $(CONFDIR)/$(PRG).cut1
# 	$(CP) $< .
# $(PRG).cut5: $(CONFDIR)/$(PRG).cut5
# 	$(CP) $< .

# Run WHIZARD
run: FORCE

test: $(PRG).in $(PRG).prc $(PRG)
	for prc in nnh_c nnh_m nnh_o; do \
	  grep -q "^$$prc" $(PRG).prc && PRC="$$PRC $$prc"; \
	done; \
	./$(PRG) -p "process_id='$$PRC'"

run $(PRG).out: $(PRG) $(CONF)
	./$(PRG)
FORCE:

# Run WHIZARD via script that checks results
fulltest test-QED test-QCD test-SM: $(CONFIG) $(MAKETEST)
	./$(MAKETEST)


# Make plots: allow this only if Metapost works
ifneq "$(MP)" ""
# Plotting package
GMLALL=$(BINDIR)/gml $(GMLDIR)/gamelan.mem gamelan.sty
gml: $(GMLALL)

gamelan.sty: $(GMLDIR)/gamelan.sty
	$(CP) $< .

plots: $(PLOTS).ps

$(PLOTS).tex: $(PLOTS).tex.in
	sed -e's/E\([+-][0-9][0-9]\)/\\times 10^{\1}/g' <$< >$@

$(PLOTS).ps: $(GMLALL) $(PRG).out $(PLOTS).tex
	$(LATEX) $(PLOTS) && $(GML) $(PLOTS) && $(LATEX) $(PLOTS) && \
	$(DVIPS) $(PLOTS) -o $@
endif


# Make channels: allow this only if Metapost works
ifneq "$(MP)" ""

channels: $(CHANNELS).ps

$(CHANNELS).tex: $(PRG).out

feynmp.sty: $(FMFDIR)/feynmp.sty
	$(CP) $< .
feynmp.mp: $(FMFDIR)/feynmp.mp
	$(CP) $< .

$(CHANNELS).ps: $(CHANNELS).tex feynmp.sty feynmp.mp
	$(LATEX) $(CHANNELS) && $(MP) $(CHANNELS).fmf.mp && $(LATEX) $(CHANNELS) && \
	$(DVIPS) $(CHANNELS) -o $@
endif


# Make grids: allow this only if Metapost works
ifneq "$(MP)" ""

grids: $(GRIDS).ps

$(GRIDS).ps: $(GMLALL) $(PRG).out $(GRIDS).tex
	$(LATEX) $(GRIDS) && \
	( for f in $(GRIDS).*.mp; do $(GML) $$f; done ) && \
	$(LATEX) $(GRIDS) && \
	$(DVIPS) $(GRIDS) -o $@
	rm -f $(GRIDS).*.mp

endif


# Plot results history: allow this only if Metapost works
ifneq "$(MP)" ""

history: $(HISTORY).ps

$(HISTORY).ps: $(GMLALL) $(PRG).out $(HISTORY).tex
	$(LATEX) $(HISTORY) && \
	$(GML) $(HISTORY) && \
	$(LATEX) $(HISTORY) && \
	$(DVIPS) $(HISTORY) -o $@
	rm -f $(HISTORY).*.mp

endif



clear:
	rm -f $(PRG).gr* $(PRG).evx $(GRAPHS).fmf.*

clean:
	rm -f $(PRG).phx $(PRG).*.gr* $(PRG).*.gx* 
	rm -f $(PLOTS).* $(GRAPHS).* $(GRIDS).* $(HISTORY).* 
	rm -f $(OUT) core

realclean: clean
	rm -f *~ $(PRG) $(CONF) $(PHS) $(CUTS) $(MAKETEST) gamelan.sty

distclean: realclean
	rm -f $(PRG).prc $(PRG).mdl $(PRG).in $(PRG).cut1 $(PRG).cut5 Makefile