###########################
# Makefile for GLISSANDO 3
###########################

# preprocessor options 

# description of the preprocessor parameters:

# _nnwp_=0      - use the hard-sphere NN wounding profile
# _nnwp_=1      - use Gaussian NN wounding profile
# _nnwp_=2      - use NN wounding profile given by the gamma distribution 
# _bindep_=1    - binary and wounding depend on each other, =0 - do not
# _files_=1     - read the nuclear distributions from external files, =0 - generate randomly
# _profile_=1   - generate the nucleon profile and NN correlation data, =0 - do not
# _weight_=1    - generate data for the NN collision profiles and for the weight (RDS) ditributions, 0 - do not 
# _evout_=0     - do not generate the text event data, =1 - short, =2 -long
# _clusters_=1  - generate clusters in light nuclei, =0 do not
# _uncluster_=1 - smoothe out the alpha clusters in nucleus A, =2 - in nucleus B, =3 in both nuclei, =0 - do not
# _partons_=1   - wounded parton model, =0 - wounded nucleon model
# _pardis_=1    - gaussian parton distribution in nucleus, =0 - exponential
# _rdsconv_=2   - convention for RDS: 1 - Nw/2, 2 - Nw


# default for wounded quarks:
 PREPROCESS := -D_nnwp_=1 -D_bindep_=1 -D_files_=0 -D_profile_=0 -D_weight_=0 -D_evout_=0 -D_clusters_=0 -D_uncluster_=0 -D_partons_=1 -D_pardis_=0 -D_rdsconv_=2

# default for wounded nucleons:
# PREPROCESS   := -D_nnwp_=2 -D_bindep_=1 -D_files_=0 -D_profile_=0 -D_weight_=0 -D_evout_=0 -D_clusters_=0 -D_uncluster_=0 -D_partons_=0 -D_pardis_=0 -D_rdsconv_=1

# version information from file glissando3.cxx
VERSION = $(shell grep "\#define _VER_"  $(DIR_CXX)glissando3.cxx | sed 's|[\#,_,3_,a-z,A-Z, ]*||')

## directory structure
DIR_MAIN    = ./
# DIR_ADDONS  = $(DIR_MAIN)addons/
DIR_BUILD   = $(DIR_MAIN)build/
DIR_DOXYGEN = $(DIR_BUILD)doxygen/
DIR_H       = $(DIR_BUILD)include/
DIR_OBJ     = $(DIR_BUILD)obj/
DIR_CXX     = $(DIR_BUILD)src/
DIR_DOC     = $(DIR_MAIN)$(shell grep "OUTPUT_DIRECTORY *=" Doxyfile | sed 's|[A-Z,_, \t,=,./]*||')
DIR_DOCHTML = $(DIR_DOC)html/
DIR_DOCTEX  = $(DIR_DOC)latex/
DIR_MACRO   = $(DIR_MAIN)macro/
DIR_INPUT   = $(DIR_MAIN)input/
DIR_OUTPUT  = $(DIR_MAIN)output/
DIR_NUCL    = $(DIR_MAUN)nucl/

# search paths
vpath %.h   $(DIR_H)
vpath %.cxx $(DIR_CXX)
vpath %.o   $(DIR_OBJ)

# distribution file lists
F_INCLUDE   = $(DIR_H)*.h
F_SOURCE    = $(DIR_CXX)*.cxx
F_DOXYGEN   = $(DIR_MAIN)Doxyfile
F_MACRO     = $(DIR_MACRO)
F_INPUT     = $(DIR_INPUT)
F_BASH      = $(DIR_MAIN)*.sh
# F_ADDONS    = $(DIR_ADDONS)*.cxx $(DIR_ADDONS)*.mk
F_PACK       = glissando3$(VERSION).tar.gz 
F_README    = $(DIR_MAIN)README
F_NUCL      = $(DIR_NUCL)*.dat

# compilation file lists
BIN_GLISS  = glissando3
HSRC_GLISS = glissando3.cxx
SRC_GLISS  = $(HSRC_GLISS:%=$(DIR_CXX)%) $(BIN_GLISS:%=$(DIR_CXX)%.cxx)
OBJ_GLISS  = $(SRC_GLISS:$(DIR_CXX)%.cxx=$(DIR_OBJ)%.o)

# compilation
CXX         = g++
LD          = g++
CXXFLAGS    = -O3 -g -w -Wno-deprecated -I $(DIR_H) $(PREPROCESS) `root-config --cflags`
LFLAGS      = `root-config --libs` -lm

#################################################################################
# RULES                                                                         #

#################################################################################

all: $(BIN_GLISS:%=$(DIR_OBJ)%) 
	$(info PREPROCESS parameters are $(PREPROCESS))
	cp $^ $(DIR_MAIN)
	mkdir -p $(DIR_OUTPUT)
	echo "Type \"./glissando3\" to run the code"

$(DIR_OBJ)glissando3: $(OBJ_GLISS)
	echo "Linking:   $@ ($(LD))"
	$(LD)  $^ -o $@ $(LFLAGS) 

$(DIR_OBJ)%.o: %.cxx $(F_INCLUDE) Makefile
	@[ -d $(DIR_OBJ) ] || mkdir -p $(DIR_OBJ)
	echo "Compiling: $< ($(CXX))"
	$(CXX) $(CXXFLAGS) -c $< -o $@

doc: $(F_INCLUDE) $(F_SOURCE) $(F_MACRO)
	doxygen
	rm -f $(DIR_DOXYGEN)file_*.dox
	echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><HTML><HEAD><META http-equiv='REFRESH' content='0;url=$(DIR_DOCHTML)index.html'></HEAD><BODY></BODY></HTML>" > manual.html
	$(MAKE) -C $(DIR_DOCTEX)
	ln -s $(DIR_DOCTEX)refman.pdf manual.pdf
	echo
	echo "GLISSANDO 3 reference manual generated with Doxygen"
	echo "- manual.html"
	echo "- manual.pdf"

ver:
	echo "version 3$(VERSION)"

package: $(F_INCLUDE) $(F_SOURCE) $(F_MACRO) $(F_DOXYGEN) $(F_INPUT) $(F_BASH) $(F_README) ./doc/latex/refman.pdf ./Makefile
	echo "$(VERSION)" > version
	tar -zcvf $(F_PACK) $(F_INCLUDE) $(F_SOURCE) $(F_MACRO) $(F_DOXYGEN) $(F_INPUT) $(F_BASH) $(F_README) ./doc/latex/refman.pdf ./Makefile
	echo "Package '$(F_PACK)' created"

clean:
	rm -rf $(DIR_OBJ)
	rm -f $(DIR_OBJ)$(BIN_GLISS) $(DIR_MAIN)$(BIN_GLISS)
	rm -f err 
	rm -f version
	echo "*.o and binary files removed"

cleandoc:
	rm -rf $(DIR_DOC)
	rm -f $(DIR_MAIN)manual.html $(DIR_MAIN)manual.pdf
	echo "Doxygen documentation removed"

cleanoutput:
	rm -f $(DIR_OUTPUT)*
	echo "Output files removed"

.SILENT :

