# Flight quick look - FQLOOK
# SOFTWARE VERSION
VER= v1r00
# 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++
CXX=g++
CXXFLAGS=-Wall
# ROOTCINT
ROOTCINT=rootcint
# Here the variables definig the local directories
DIRTOP=${shell pwd}
DIRLIB=lib
DIRBIN=bin
DIRSRC=src
DIRDOC=doc
DIRMACROS=macros
# Here the  PAMELA environmental variables to be checked
PAM_ENV= 

# Here the local variables used to build the targets
# -- F77 - no libraries --
FSRCS=
FOBJS=$(FSRCS:${DIRSRC}%.for=${DIRLIB}%.o)

# -- CPP - no libraries --
CPPSRCS=
CPPOBJS=$(CPPSRCS:${DIRSRC}%.cpp=${DIRLIB}%.o)

# -- C - no libraries --
CSRCS=
COBJS=$(CSRCS:${DIRSRC}%.c=${DIRLIB}%.o)

# -- CPP - ROOT libraries --
LIBROOTSRCS=
LIBROOTLIBS=$(LIBROOTSRCS:${DIRSRC}/%.cpp=${DIRLIB}/lib%_${VER}.so)
LIBROOTCLIBS=
FULLROOTCLIBS=$(LIBROOTCLIBS:%=${DIRLIB}/%_${VER}.so)

# -- CPP - NON-ROOT libraries --
LIBCPPSRCS=
LIBCPPLIBS=$(LIBCPPSRCS:${DIRSRC}/%.cpp=${DIRLIB}/lib%_${VER}.so)
LIBCPPCLIBS=
FULLLIBCPPLIBS=$(LIBCPPCLIBS:%=${DIRLIB}/%_${VER}.so)


# Here the local variables used to build the targets

CXXEXESRCS=${DIRSRC}/TriggerScanBasic.cpp ${DIRSRC}/TriggerScanExpert.cpp
CXXEXEOBJS=$(CXXEXESRCS:${DIRSRC}%.cpp=${DIRLIB}%.o)
CXXEXELIBS=$(CXXEXESRCS:${DIRSRC}/%.cpp=%)
EXESRCS= ${DIRBIN}/TriggerScanBasic ${DIRBIN}/TriggerScanExpert

# Here the Variables used for info target
PACKAGE=Trigger flight quick look software 
EXECUTABLES=
LIBS= ${FLIBS} ${FOBJS} ${CLIBS} ${COBJS} ${CXXEXEOBJS} ${FULLLIBS}
DOCS=
SRCS= ${FSRCS} ${CRSCS} ${CXXEXESRCS}
MACROS= ${MACROSSRCS}
NROOTINC=${shell root-config --incdir}
#
TOBEDEL = 
ROOTINC=$$${shell root-config --incdir}
NROOTINC=${shell root-config --incdir}
# Let's start
all : test mkbindir mklibdir ${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
${CXXEXELIBS}: ${CXXEXESRCS}
	@(${EGREEN} "Creating exec file: "${WHITE} "$@"; ${RESET})
	${CXX} ${CXXFLAGS} -c ${DIRSRC}/$@.cpp -o ${DIRLIB}/$@.o -I${PAM_INC} -I${PAM_YODA}/include/yoda/ -I${NROOTINC};
	${CXX} ${CXXFLAGS} -o ${DIRBIN}/$@ ${DIRLIB}/$@.o -I${PAM_INC} -I${PAM_YODA}/include/yoda/ ${PAM_YODA}/lib/libyoda.so `root-config --cflags --glibs`;



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

.PHONY: install installdoc installexe
.PHONY: clean depclean info 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= ${DIRBIN}  
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: installdoc installexe 

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

info:
	@echo " "
	@${EGREEN} "Infos for package :" ${WHITE} ${PACKAGE}
	@${EGREEN} "          VERSION :" ${WHITE} ${VER}
	@${EGREEN} "      EXECUTABLES :" ${WHITE} ${EXECUTABLES}
	@${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
