# 
#
#
export debug=0
export release=1
#
export RED='\E[1;31;40m'
export GREEN='\E[1;32;40m'
export YELLOW='\E[1;33;40m'
export BLUE='\E[1;34;40m'
export WHITE='\E[1;37;40m'
export VIOLET='\E[1;35;40m'
export CYAN='\E[1;36;40m'
# Here the echo definition to use them
export EVIOLET=echo -e ${VIOLET}
export ECYAN=echo -e ${CYAN}
export ERED=echo -e ${RED}
export EGREEN=echo -e ${GREEN}
export EYELLOW=echo -e ${YELLOW}
export EBLUE=echo -e ${BLUE}
export EWHITE=echo -e ${WHITE}
# Reset the terminal
export RESET=tput sgr0
#
export BASEDIR=$(shell echo `pwd`)
#export LD_LIBRARY_PATH+=:$(BASEDIR)/event/
#
export LDFLAGS+=$(PAM_BIT) 
export CFLAGS+=$(PAM_BIT) 
export FFLAGS+=$(PAM_BIT) 
#
ifeq ($(GCC4),)
export GCCVERS=$(shell echo `gcc -dumpversion | sed 's/\./\ /g'`)
export GCCVERMAJ=$(word 1,$(GCCVERS))
export GCC4=$(shell echo `echo "${GCCVERMAJ} > 3" |bc -l`)
endif
#
DIRS=event PamOffLineSW YodaProfiler
#
all: 
ifeq ($(verbose),)
	@for sub_dir in $(DIRS) ; do \
		( cd $$sub_dir &>/dev/null && (${EGREEN} "Make all in $$sub_dir "; ${RESET}) && make -s BASEDIR=$(BASEDIR)) ; \
	done
else
	for sub_dir in $(DIRS) ; do \
		( cd $$sub_dir && make BASEDIR=$(BASEDIR)) ; \
	done
endif

YodaProfiler: YodaProfiler/inc/* YodaProfiler/src/*
ifeq ($(verbose),)
	@for sub_dir in "YodaProfiler" ; do \
		( cd $$sub_dir &>/dev/null && (${EGREEN} "Make all in $$sub_dir "; ${RESET}) && make -s BASEDIR=$(BASEDIR)) ; \
	done
else
	for sub_dir in "YodaProfiler" ; do \
		( cd $$sub_dir && make BASEDIR=$(BASEDIR)) ; \
	done
endif

distclean: 
ifeq ($(verbose),)
	@for sub_dir in $(DIRS) ; do \
		( cd $$sub_dir &>/dev/null && (${EGREEN} "Make distclean in $$sub_dir "; ${RESET}) && make -s distclean) ; \
	done
	@for sub_dir in `find ./ -name obj` ; do \
		rm -rf $$sub_dir ; \
	done
else
	for sub_dir in $(DIRS) ; do \
		( cd $$sub_dir && make distclean) ; \
	done
	for sub_dir in `find ./ -name obj` ; do \
		rm -rf $$sub_dir ; \
	done
endif

install:
ifeq ($(verbose),)
	@(${EGREEN} " Creating PAMELA environment "; ${RESET})
	@if [ "$(PAM_SOFTWARE)" == "" ]; then (${ERED} ""; ${RESET}); \
		(${ERED} " PLEASE SET THE PAMELA ENVIRONMENT BEFORE INSTALLING "; ${RESET}); \
		(${ERED} ""; ${RESET}); \
		exit; fi
	@if [ ! -d $(PAM_HOME) ]; then mkdir $(PAM_HOME); fi
	@if [ ! -d $(PAM_SOFTWARE) ]; then mkdir $(PAM_SOFTWARE); fi
	@if [ ! -d $(PAM_LIB) ]; then mkdir $(PAM_LIB); fi
	@if [ ! -d $(PAM_INC) ]; then mkdir $(PAM_INC); fi
	@ln -sf $(PAM_SOFTWARE)/inc $(PAM_SOFTWARE)/include
	@if [ ! -d $(PAM_BIN) ]; then mkdir $(PAM_BIN); fi
	@if [ ! -d $(PAM_DOC) ]; then mkdir $(PAM_DOC); fi
	@if [ ! -d $(PAM_MACROS) ]; then mkdir $(PAM_MACROS); fi
	@if [ ! -d $(PAM_SRC) ]; then mkdir $(PAM_SRC); fi
	@if [ ! -d $(PAM_CALIB) ]; then mkdir $(PAM_CALIB); fi
	@if [ ! -d $(FEV_PLUGIN) ]&&[ "$(FEV_PLUGIN)" != "" ]; then mkdir $(FEV_PLUGIN); fi
	@(${EGREEN} " Installing: library "${WHITE}"libyoda.so "; ${RESET})  
	@cp -f $(BASEDIR)/event/libyoda.so.0.0.0 $(PAM_LIB)/
	@(cd $(PAM_LIB) &>/dev/null && ln -sf libyoda.so.0.0.0 libyoda.so && cd - &>/dev/null)
	@(cd $(PAM_LIB) &>/dev/null && ln -sf libyoda.so.0.0.0 libyoda.so.0 && cd - &>/dev/null)
#	@ln -sf $(PAM_LIB)/libyoda.so.0.0.0 $(PAM_LIB)/libyoda.so 
#	@ln -sf $(PAM_LIB)/libyoda.so.0.0.0 $(PAM_LIB)/libyoda.so.0 
	@(${EGREEN} " Installing: exec file "${WHITE}"chewbacca"; ${RESET})  
	@cp -f $(BASEDIR)/PamOffLineSW/chewbacca $(PAM_BIN)/
	@(${EGREEN} " Installing: "${WHITE}"yoda headers";${RESET})  
	@(cd $(BASEDIR)/event/ &>/dev/null && make -s distclean)
	@rm -rf $(PAM_INC)/yoda/
	@cp -rf $(BASEDIR)/event/ $(PAM_INC)/yoda
	@cp -rf $(PAM_LIB)/libyoda.so.0.0.0 $(BASEDIR)/event/
	@ln -sf $(BASEDIR)/event/libyoda.so.0.0.0 $(BASEDIR)/event/libyoda.so 
	@(${EGREEN}"")
	@(cd YodaProfiler &>/dev/null && make -s forceinstall)
else
	(${EGREEN} " Creating PAMELA environment "; ${RESET})
	if [ "$(PAM_SOFTWARE)" == "" ]; then (${ERED} ""; ${RESET}); \
		(${ERED} " PLEASE SET THE PAMELA ENVIRONMENT BEFORE INSTALLING "; ${RESET}); \
		(${ERED} ""; ${RESET}); \
		exit; fi
	if [ ! -d $(PAM_HOME) ]; then mkdir $(PAM_HOME); fi
	if [ ! -d $(PAM_SOFTWARE) ]; then mkdir $(PAM_SOFTWARE); fi
	if [ ! -d $(PAM_LIB) ]; then mkdir $(PAM_LIB); fi
	if [ ! -d $(PAM_INC) ]; then mkdir $(PAM_INC); fi
	ln -sf $(PAM_SOFTWARE)/inc $(PAM_SOFTWARE)/include
	if [ ! -d $(PAM_BIN) ]; then mkdir $(PAM_BIN); fi
	if [ ! -d $(PAM_DOC) ]; then mkdir $(PAM_DOC); fi
	if [ ! -d $(PAM_MACROS) ]; then mkdir $(PAM_MACROS); fi
	if [ ! -d $(PAM_SRC) ]; then mkdir $(PAM_SRC); fi
	if [ ! -d $(PAM_CALIB) ]; then mkdir $(PAM_CALIB); fi
	if [ ! -d $(FEV_PLUGIN) ]&&[ "$(FEV_PLUGIN)" != "" ]; then mkdir $(FEV_PLUGIN); fi
	(${EGREEN} " Installing "${EWHITE}"libyoda.so "; ${RESET})  
	cp -f $(BASEDIR)/event/libyoda.so.0.0.0 $(PAM_LIB)/
	cd $(PAM_LIB) && ln -sf libyoda.so.0.0.0 libyoda.so && cd -
	cd $(PAM_LIB) && ln -sf libyoda.so.0.0.0 libyoda.so.0 && cd -
	(${EGREEN} " Installing exec file "${EWHITE}"chewbacca"; ${RESET})  
	cp -f $(BASEDIR)/PamOffLineSW/chewbacca $(PAM_BIN)/
	(${EGREEN} " Installing "${EWHITE}"yoda headers";${RESET})  
	(cd $(BASEDIR)/event/ && make distclean)
	rm -rf $(PAM_INC)/yoda/
	cp -rf $(BASEDIR)/event/ $(PAM_INC)/yoda
	cp -rf $(PAM_LIB)/libyoda.so $(BASEDIR)/event/
	ln -sf $(BASEDIR)/event/libyoda.so.0.0.0 $(BASEDIR)/event/libyoda.so 
	(${EGREEN}"")
	(cd YodaProfiler && make forceinstall)
endif