# Calorimeter double shower plugin
# SOFTWARE VERSION
VER= v1r01
# An almost general purpouse Makefile for PAMELA repository
# Let's start with some usefull definition for colorize the echo 
RED='\E[1;31;40m'
GREEN='\E[1;32;40m'
YELLOW='\E[1;33;40m'
BLUE='\E[1;34;40m'
WHITE='\E[1;37;40m'
VIOLET='\E[1;35;40m'
CYAN='\E[1;36;40m'
# Here the echo definition to use them
EVIOLET=echo -e ${VIOLET}
ECYAN=echo -e ${CYAN}
ERED=echo -e ${RED}
EGREEN=echo -e ${GREEN}
EYELLOW=echo -e ${YELLOW}
EBLUE=echo -e ${BLUE}
EWHITE=echo -e ${WHITE}
# Reset the terminal
RESET=tput sgr0
# Here the variables used to override the standard make flags
# C
CC=gcc
#CFLAGS=-Wall -O -fPIC -pthread -g ${PAM_BIT} 
CFLAGS=-Wall -O -fPIC -pthread ${PAM_BIT} 
# Enviroment:
GCCVER=`gcc --version | head -1`
UNAMEA=`uname -a`
TODAY=`date`
# F77
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
ifeq ($(GCC4),1)
F77=gfortran
LG2C=-lgfortran
EXTRAOPT=
else
F77=g77
LG2C=-lg2c
EXTRAOPT=-Wno-globals -fvxt
endif
#F77FLAGS=  -Wall -Wno-globals -fvxt -fno-automatic -fbounds-check -g -extend_source -static -fno-backslash ${PAM_BIT} 
F77FLAGS=  -Wall -funroll-loops -fPIC $(EXTRAOPT) -fno-automatic -fbounds-check -extend_source -static -fno-backslash ${PAM_BIT}
# C++
CXX=g++
#CXXFLAGS=-Wall -O -fPIC -pthread -g ${PAM_BIT} 
CXXFLAGS=-Wall -O -fPIC -pthread ${PAM_BIT} 
# ROOTCINT
ROOTCINT=rootcint
# Here the variables definig the local directories
DIRTOP=${shell pwd}
DIRLIB=lib/${ARCH}/
DIRBIN=bin/${ARCH}/
DIRSRC=src
DIRINC=inc
DIRDOC=doc
DIRMACROS=macros
DIRCALIB=calib
# Here the  PAMELA environmental variables to be checked
PAM_ENV= PAM_BIN_env PAM_LIB_env PAM_INC_env PAM_DOC_env PAM_CALIB_env PAM_YODA_env 
# Here the local variables used to build the targets
#-------------------------------------------------
FSRCS=${DIRSRC}/getdblsh.for \
      ${DIRSRC}/dblsc.for \
      ${DIRSRC}/dubsc.for 
FOBJS=$(FSRCS:${DIRSRC}/%.for=${DIRLIB}/%.o)
#-------------------------------------------------
CSRCS=	 
COBJS=$(CSRCS:${DIRSRC}%.cpp=${DIRLIB}/%.o)
#-------------------------------------------------
# librerie utilizzabili da root (ci deve essere anche il ***LinkDef.h )
LIBROOTSRCS=${DIRSRC}/CaloDoubleShower.cpp 
LIBROOTLIBS=$(LIBROOTSRCS:${DIRSRC}/%.cpp=${DIRLIB}/lib%_${VER}.so)
LIBROOTOBJS=${FOBJS}
LIBROOTCLIBS=libCaloDoubleShower
FULLROOTCLIBS=$(LIBROOTCLIBS:%=${DIRLIB}/%_${VER}.so)
#-------------------------------------------------
# librerie C++ NON utilizzabili da root
LIBCPPSRCS=#${DIRSRC}/PamLevel2.cpp 
LIBCPPLIBS=$(LIBCPPSRCS:${DIRSRC}/%.cpp=${DIRLIB}/lib%_${VER}.so)
LIBCPPCLIBS=#libPamLevel2 
FULLCPPCLIBS=$(LIBCPPCLIBS:%=${DIRLIB}/%_${VER}.so)
#-------------------------------------------------
# librerie F77 NON utilizzabili da root
LIBF77SRCS= 
LIBF77OBJS= $(LIBF77SRCS:${DIRSRC}/%.for=${DIRLIB}/%.o)
LIBF77LIBS= $(LIBF77SRCS:${DIRSRC}/%.for=${DIRLIB}/lib%_${VER}.so)
LIBF77CLIBS=
FULLF77CLIBS=$(LIBF77CLIBS:%=${DIRLIB}/%_${VER}.so)
#-------------------------------------------------
FULLLIBS=${FULLROOTCLIBS} ${FULLCPPCLIBS} ${FULLF77CLIBS} 

#-------------------------------------------------
# eseguibile
CXXEXESRCS=
CXXEXEOBJS=$(CXXEXESRCS:${DIRSRC}%.cpp=${DIRLIB}%.o)
CXXEXELIBS:=

OTHERLIBS=${PAM_LIB}/libCaloLevel0.so ${PAM_LIB}/libCaloLevel1.so ${PAM_LIB}/libCaloLevel2.so ${PAM_LIB}/libCaloCore.so  ${PAM_LIB}/libcaloclasses.so
#-------------------------------------------------
# variabili per l'installazione
EXESRCS=
INCSRCS=${DIRINC}/CaloDoubleShower.h 
MACROSSRCS=
CALIBFILES=
DOCFILES=
# Here the Variables used for info target
PACKAGE=Tracker flight software 
EXECUTABLES=${EXESRCS}
LIBS= ${FULLLIBS} ${OTHERLIBS}
DOCS=
SRCS= 
MACROS= ${MACROSSRCS}
INCLUDE= ${INCSRCS}
#
TOBEDEL = junk
ROOTINC=$$${shell root-config --incdir}
NROOTINC=${shell root-config --incdir}
# Let's start
all : test mkbindir mklibdir ${LIBF77LIBS} ${LIBROOTLIBS} ${LIBCPPLIBS} ${CXXEXELIBS} 
all: 
	@echo -e ${GREEN} ""
	@(${EGREEN} " Finished, now you can install the package \n  (use: make install or make upgrade or make forceinstall) "; ${RESET})
	@echo -e ${GREEN} ""
	@${RESET}

# Here some general rules to produce objects in our local dir
${DIRLIB}/%.o: ${DIRSRC}/%.cpp
	@(${EYELLOW} " C++ object ------------------------")
	@(${EGREEN} "Creating object file: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -c -o $@ ${DIRSRC}/`basename ${@} .o`.cpp -I${DIRINC} -I${PAM_INC} -I${PAM_YODA}/include/yoda/  `root-config --cflags`

${DIRLIB}/%.o : ${DIRSRC}/%.for
	@(${EYELLOW} " F77 object ------------------------")
	@(${EGREEN} "Creating object file: "${WHITE} "$@"; ${RESET})
	@${F77} ${F77FLAGS} -c -o $@ ${DIRSRC}/`basename ${@} .o`.for -I${DIRINC} -I${NROOTINC} -I${PAM_INC} -I${PAM_YODA}/include/yoda

 
# .so from .o fortran
${LIBF77LIBS}: ${LIBF77OBJS}
	@(${EYELLOW} " F77 shared library --------------------------")
	@(${EGREEN} "Creating library: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -Xlinker -soname=$(@F) -shared -o $@ $^ $(LG2C) `doc/cernlib_pam mathlib`


${LIBROOTLIBS}:  ${FOBJS} ${LIBROOTSRCS}
	@(${EYELLOW} " ROOT library ------------------------")
	@(${EGREEN} "Creating file: "${WHITE} "`basename $(@F) _${VER}.so | sed s/lib//`Dict.cpp"; ${RESET})
#
#	Create *Dict.cpp and *Dict.h
#
#	@${ROOTCINT} -f ${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`Dict.cpp -c -I${ROOTINC} -I${PAM_YODA}/include/yoda/ -I${PAM_INC} ${DIRINC}/`basename $(@F) _${VER}.so | sed s/lib//`.h  ${DIRINC}/`basename $(@F) _${VER}.so | sed s/lib//`LinkDef.h 
	@${ROOTCINT} -f ${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`Dict.cpp -c -I${PAM_INC} -I${ROOTINC} -I${PAM_YODA}/include/yoda/ ${DIRINC}/`basename $(@F) _${VER}.so | sed s/lib//`.h  ${DIRINC}/`basename $(@F) _${VER}.so | sed s/lib//`LinkDef.h 
#
# order of include directories changed since RunInfo.h can be found (with completely different definition) in YODA while we want to load the DV one!
# should be fixed some time changing names...
#
#	create .o 
#
	@(${EGREEN} "Compiling file: "${WHITE} "`basename $(@F) _${VER}.so | sed s/lib//`.cpp"; ${RESET})
	@cd ${DIRSRC} &>/dev/null && ${CXX} ${CXXFLAGS} -c `basename $(@F) _${VER}.so | sed s/lib//`.cpp -I${DIRTOP}/${DIRINC} -I${DIRTOP} -I${PAM_INC} -I${PAM_YODA}/include/yoda/ -I${NROOTINC}; 
	@(${EGREEN} "Compiling file: "${WHITE} "`basename $(@F) _${VER}.so | sed s/lib//`Dict.cpp"; ${RESET})
	@cd ${DIRSRC} &>/dev/null && ${CXX} ${CXXFLAGS} -c `basename $(@F) _${VER}.so | sed s/lib//`Dict.cpp -I${DIRTOP}/${DIRINC} -I${DIRTOP} -I${PAM_INC} -I${PAM_YODA}/include/yoda/ -I${NROOTINC}; 
#
#	create .so
#
	@(${EGREEN} "Compiling shared library: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -Xlinker -soname=$(@F) -shared -o $@ $(LG2C) -I${DIRTOP}/${DIRINC} -I${DIRTOP} -I${PAM_YODA}/include/yoda -I${PAM_INC} ${DIRTOP}/${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`.o ${DIRTOP}/${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`Dict.o ${LIBROOTOBJS} `doc/cernlib_pam mathlib` 1>/dev/null	 
#	@ln -sf  ${DIRTOP}/${DIRLIB}/$(@F) ${DIRTOP}/${DIRLIB}/$(patsubst %_${VER}.so,%.so,$(@F))

${LIBCPPLIBS}: ${LIBCPPSRCS}
#
#	create .o 
#
	@(${EGREEN} "Compiling file: "${WHITE} "`basename $(@F) _${VER}.so | sed s/lib//`.cpp"; ${RESET})
	@cd ${DIRSRC} &>/dev/null && ${CXX} ${CXXFLAGS} -c `basename $(@F) _${VER}.so | sed s/lib//`.cpp -I${DIRTOP}/${DIRINC} -I${DIRTOP} -I${PAM_YODA}/include/yoda/ -I${NROOTINC} -I${PAM_INC} ; 
#
#	create .so
#
	@(${EGREEN} "Compiling shared library: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -Xlinker -soname=$(@F) -shared -o $@ $(LG2C) -I${DIRTOP}/${DIRINC} -I${DIRTOP} -I${PAM_YODA}/include/yoda/ -I${PAM_INC} ${DIRTOP}/${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`.o 1>/dev/null


${CXXEXELIBS}: ${COBJS} ${FOBJS} ${CXXEXEOBJS}
	@(${EVIOLET} "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")
	@(${EGREEN} "Creating exec file: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -o ${DIRBIN}/$@ ${DIRLIB}/$@.o -I${DIRTOP}/${DIRINC} -I${PAM_INC} -I${PAM_YODA}/include/yoda/ ${PAM_YODA}/lib/libyoda.so ${FOBJS} ${COBJS} ${FULLLIBS} ${OTHERLIBS} -lThread `root-config --cflags --glibs` `doc/cernlib_pam mathlib` ;

# A small target to check environmental variables
%_env:
	@${test_env}

.PHONY: install installdoc installlib installmacros installinc installexe installcmacro
.PHONY: clean depclean info mkbindir mklibdir testcern testpamenv test

mkbindir: ${DIRBIN}
${DIRBIN}:
	@${test_and_mk}

mklibdir: ${DIRLIB}
${DIRLIB}:
	@${test_and_mk}

# Here what has to be deleted in clean target
clean: TOBEDEL= ${DIRSRC}/*.o ${DIRSRC}/*Dict.cpp ${DIRSRC}/*Dict.h ${FOBJS} ${FULLLIBS} ${DIRBIN}/${CXXEXELIBS} ${COBJS} ${CXXEXEOBJS} 
clean:
	@${test_and_rm}

distclean: clean
distclean: TOBEDEL= ${DIRBIN} ${DIRLIB}
distclean:
	@${test_and_rm}

forceinstall: FORCE=-f
forceinstall: install

upgrade: FORCE=-u
upgrade: install

install: installlib installinc installmacros installcalib installdoc installexe installcmacro

installlib: LINK=y
installlib: TOBEMOVED= $(FULLLIBS:${DIRLIB}%=${PAM_LIB}%)
installlib: ORIGIN=${DIRLIB}
installlib:
	@${test_and_cp}

installcmacro: LINK=y
installcmacro: TOBEMOVED= $(FULLMACROCLIBS:${DIRLIB}%=${PAM_LIB}%)
installcmacro: ORIGIN=${DIRLIB}
installcmacro:
	@${test_and_cp}

installinc: TOBEMOVED= $(INCSRCS:${DIRINC}/%=${PAM_INC}/%)
installinc: ORIGIN=${DIRINC}
installinc:
	@${test_and_cp}

installexe: TOBEMOVED= $(EXESRCS:${DIRBIN}/%=${PAM_BIN}/%)
installexe: ORIGIN=${DIRBIN}
installexe:
	@${test_and_cp}

installmacros: TOBEMOVED= $(MACROSSRCS:${DIRMACROS}/%=${PAM_MACROS}/%)
installmacros: ORIGIN=${DIRMACROS}
installmacros:
	@${test_and_cp}

installcalib: TOBEMOVED= $(CALIBFILES:${DIRCALIB}/%=${PAM_CALIB}/%)
installcalib: ORIGIN=${DIRCALIB}
installcalib:
	@${test_and_cp}

installdoc: TOBEMOVED= $(DOCFILES:${DIRDOC}/%=${PAM_DOC}/%)
installdoc: ORIGIN=${DIRDOC}
installdoc:
	@${test_and_cp}

#mkverpath:
#	@echo '#include <TSystem.h>' >  ${DIRTOP}/${DIRSRC}/TrkVerl2.cpp; echo ' void info(){ printf("\nTracker Level2 flight package version: ${VER}\n\nCompiled on '${TODAY}' with: '${GCCVER}'\n\nOn: '${UNAMEA}'\n\n"); }' >>  ${DIRTOP}/${DIRSRC}/TrkVerl2.cpp
#	@sleep 0.8



info:
	@echo " "
	@${EGREEN} "Infos for package :" ${WHITE} ${PACKAGE}
	@${EGREEN} "          VERSION :" ${WHITE} ${VER}
	@${EGREEN} "      EXECUTABLES :" ${WHITE} ${EXECUTABLES}
	@${EGREEN} "         INCLUDES :" ${WHITE} ${INCLUDE}
	@${EGREEN} "             LIBS :" ${WHITE} ${LIBS}
	@${EGREEN} "             DOCS :" ${WHITE} ${DOCS}
	@${EGREEN} "             SRCS :" ${WHITE} ${SRCS}
	@${EGREEN} "           MACROS :" ${WHITE} ${MACROS}
	@echo " "
	@${RESET}

test: testpamenv 

testpamenv: ${PAM_ENV}

root-config: OPT=--cflags
root-config: 
	@${test_cmd}

# Here follows some usefull macros
define 	test_and_cp
wd=`pwd`;\
for i in ${TOBEMOVED} ; do \
wv=$${i##/*/}; \
dest=$${i/$${wv}/}; \
${EGREEN} "Testing for :" ${WHITE} "$${i}" ; ${RESET}; \
if [ ! "${FORCE}" ] ; then \
 if [ ! -f $${i} ]; then  \
  ${EGREEN} "Copying : "${WHITE} ${ORIGIN}/$${i##/*/} ;\
  ${EGREEN} "      to: "${WHITE} $${i} ; ${RESET}; \
  cp ${ORIGIN}/$${i##/*/} $${i}; \
 else \
   ${ERED} "File already exist !!!! Please delete it " ${WHITE}; ${RESET}; \
 fi;\
else \
   if [ "${FORCE}" == "-u" ] ; then \
     if [ "`basename $${i}`" != "`basename $${i} _${VER}.so`" ]; then \
       ${EYELLOW} "Removing: "${WHITE} ${PAM_LIB}/`basename $${i} _${VER}.so`_v*r*.so; ${RESET}; \
       rm -f ${PAM_LIB}/`basename $${i} _${VER}.so`_v*r*.so ; \
       ${EGREEN} "Copying : "${WHITE} ${ORIGIN}/$${i##/*/} ;\
       ${EGREEN} "      to: "${WHITE} $${i} ; ${RESET}; \
       cp ${ORIGIN}/$${i##/*/} $${i}; \
     else \
       ${EYELLOW} " Forcing copy: "${WHITE} ${ORIGIN}/$${i##/*/} ;\
       ${EYELLOW} "          to: "${WHITE} $${i} ; ${RESET}; \
       cp -f ${ORIGIN}/$${i##/*/} $${i}; \
     fi; \
   else \
     ${EYELLOW} " Forcing copy: "${WHITE} ${ORIGIN}/$${i##/*/} ;\
     ${EYELLOW} "          to: "${WHITE} $${i} ; ${RESET}; \
     cp ${FORCE} ${ORIGIN}/$${i##/*/} $${i}; \
   fi;\
fi ;\
if [ ! -z ${LINK} ] ; then \
  ${EGREEN} "Creating symlink from : "${WHITE} $${wv/_${VER}/} ;\
  ${EGREEN} "                   to : "${WHITE} $${i##/*/} ; ${RESET}; \
  cd $${dest}; \
  ln -sf $${wv} $${wv/_${VER}/}; \
  cd $${wd}; \
fi \
done
endef

define 	test_and_mk
${EGREEN} "Testing for :" ${WHITE} "$@"; ${RESET}
test -d "$@" || \
(${EGREEN} "Creating : "${WHITE} "$@"; ${RESET}; mkdir -p $@)
endef

define 	test_env
${EGREEN} "Testing for env:" ${WHITE} $(patsubst %_env,%,$@); ${RESET}
printenv $(patsubst %_env,%,$@) 1>/dev/null || \
(${ERED} "ERROR, variable : "${WHITE} $(patsubst %_env,%,$@) \
${RED} "not defined !!! Check your env. settings"; \
${RESET}; exit 1) 
${EGREEN} " Defined:" ${WHITE} $${$(patsubst %_env,%,$@)}; ${RESET} 
endef

define 	test_cmd
${EGREEN} "Testing for cmd:" ${WHITE} $@ ${OPT}; ${RESET}
$@ ${OPT} 1>/dev/null 2>&1 || \
(${ERED} "ERROR, command : "${WHITE} $@ ${OPT}\
${RED} "not found !!! Check your installation"; \
${RESET}; exit 1) 
${EGREEN} " Present:" ${WHITE} $@ ${OPT}; ${RESET} 
endef

define 	test_and_rm
for i in ${TOBEDEL}; do \
test -d $${i} || (${ERED} "Removing file :"${WHITE} $${i}; ${RESET}; \
rm -f $${i} ); \
test ! -d $${i} || (${ERED} "Removing dir :"${WHITE} $${i}; ${RESET}; \
rm -rf $${i} ); \
done
endef
