# DarthVader flight analysis software 
# SOFTWARE VERSION IS
VER= v10r09
#
include Make.def
#CERNLIB
CERNLIB=./docs/cernlib_pam
#CERNLIB=cernlib

# Here the variables definig the local directories
DIRTOP=${shell pwd}
#DIRLIB=../lib/${ARCH}
#DIRBIN=../bin/${ARCH}
DIRLIB=lib/${ARCH}
DIRBIN=bin/${ARCH}
DIRSRC=src
DIRINC=inc
DIRDOC=doc
DIRMACROS=macros
DIRCALIB=param
# 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
# -- F77 - no libraries --
FSRCS=
FOBJS=$(FSRCS:${DIRSRC}%.for=${DIRLIB}%.o)

# -- CPP - no libraries --
CPPSRCS=${DIRSRC}/DarthVaderVerl2.cpp
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)

# -- CPP - Executables --
CXXEXESRCS=${DIRSRC}/DarthVader.cpp 
CXXEXEOBJS=$(CXXEXESRCS:${DIRSRC}%.cpp=${DIRLIB}%.o)
CXXEXELIBS:=DarthVader 

# -- list of objects needed to link
##OBJS=$(shell cd ${DIRLIB}&>/dev/null;for file in `ls *.*`; do if [ $${file} != "" ] && [ $${file} != "DarthVader.o" ] && [ $${file} != "R2-D2.o" ]; then echo ${DIRLIB}/$${file}; fi; done)
OBJS=$(shell cd ${DIRLIB}&>/dev/null;for file in `ls lib*.so`; do if [ $${file} != "" ]; then echo ${DIRLIB}/$${file}; fi; done) ${CPPOBJS}
DARTHVLIB=${DIRLIB}/libDarthVader_${VER}.so

# -- directory where to find tracker headers needed to compile
#TRKHEAD=${DIRTOP}/../tracker/inc
#TRKHEAD=-I${PAM_INC} -ITrackerLevel2/inc/ -IRunInfo/inc/ -IGLTables/inc/

#
SUBDIR=./RunInfo ./S4Level2 ./NDLevel2 ./TrackerLevel2 ./TriggerLevel2 ./ToFLevel2 ./CalorimeterLevel2 ./AnticounterLevel2 ./OrbitalInfo
SUBINC=$(shell for dir in `echo ${SUBDIR}`; do echo -I$${dir}/inc; done)

# List of all libraries we want to install
FULLLIBS = ${FULLROOTCLIBS} ${FULLLIBCPPLIBS} ${DARTHVLIB}

# Other objects to install
EXESRCS=${DIRBIN}/DarthVader 

INCSRCS=

MACROSSRCS=

CALIBFILES=${DIRCALIB}/trk-param ${DIRCALIB}/cal-param ${DIRCALIB}/tof-param ${DIRCALIB}/orb-param

DOCFILES=

# Here the Variables used for info target
PACKAGE=PAMELA analysis flight software 
EXECUTABLES= ${EXESRCS}
LIBS= ${FULLLIBS}
DOCS=
SRCS= 
#SRCS= ${FSRCS} ${CRSCS} ${CXXEXESRCS} ${LIBROOTSRCS} ${LIBCPPSRCS}
MACROS= ${MACROSSRCS}
INCLUDE= ${INCSRCS}
#
TOBEDEL = junk
ROOTINC=$$${shell root-config --incdir}
NROOTINC=${shell root-config --incdir}
# Directories where you place detectors software
#SUBDIR=./GLTables ./RunInfo ./TrackerLevel2 ./CalorimeterLevel2

# Let's start
#all: test mkbindir mklibdir mkverpath ${FOBJS} ${CPPOBJS} ${LIBROOTLIBS} ${LIBCPPLIBS} ${CXXEXELIBS} 

all: 
	@for dir in `echo ${SUBDIR}`; do (${EBLUE}  "Making all in $${dir}";  ${RESET}); (cd $${dir} &>/dev/null && ${MAKE} -s all); done
#	@for dir in `echo ${SUBDIR}`; do (${EBLUE}  "Making all in $${dir}";  ${RESET}); (cd $${dir} && ${MAKE} all); done
	@make -s locall ;

locall: prepare libs exe
locall: 
	@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}

#subdir:
#	@for dir in `echo ${SUBDIR}`; do  \
#	 echo "Making ${target} in $${dir}"; \
#	 (cd $${dir} && ${MAKE} ${target}); \
#	 done;

prepare: test mkbindir mklibdir mkverpath

exe: prepare ${CXXEXELIBS} 

libs: prepare ${LIBROOTLIBS} ${LIBCPPLIBS}

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

${DIRLIB}/%.o: ${DIRSRC}/%.c
	@(${EGREEN} "Creating C object file: "${WHITE} "`basename $@`"; ${RESET})
	@${CXX} ${CXXFLAGS} -c -o $@ ${DIRSRC}/`basename ${@} .o`.c -I${DIRINC}  -I${PAM_YODA}/include/yoda/ -I${PAM_INC}/ `root-config --cflags`

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

${LIBROOTLIBS}: ${LIBROOTSRCS}
	@(${EGREEN} "ROOTCINT - 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
#
#	create .o 
#
	@(${EGREEN} "Compiling CPP 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${PAM_INC}/ -I${NROOTINC}; 
	@(${EGREEN} "Compiling CPP 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_YODA}/include/yoda/ -I${PAM_INC}/ -I${NROOTINC}; 
#
#	create .so
#
	@(${EGREEN} "Creating shared library: "${WHITE} "`basename $@`"; ${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 1>/dev/null
	@ln -sf  ${DIRTOP}/${DIRLIB}/$(@F) ${DIRTOP}/${DIRLIB}/$(patsubst %_${VER}.so,%.so,$(@F))

${LIBCPPLIBS}: ${LIBSRCS} ${COBJS} ${FOBJS}
#
#	create .o 
#
	@(${EGREEN} "Compiling CPP 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} ${SUBINC} -I${PAM_YODA}/include/yoda/ -I${PAM_INC}/ -I${NROOTINC}; 
#
#	create .so
#
	@(${EGREEN} "Creating shared library: "${WHITE} "`basename $@`"; ${RESET})
	@${CXX} ${CXXFLAGS} -Xlinker -soname=$(@F) -shared -o $@ $(LG2C) -I${DIRTOP}/${DIRINC} -I${DIRTOP} ${SUBINC} -I${PAM_YODA}/include/yoda/ -I${PAM_INC}/ ${DIRTOP}/${DIRSRC}/`basename $(@F) _${VER}.so | sed s/lib//`.o ${FOBJS} ${COBJS} 1>/dev/null
	@ln -sf  ${DIRTOP}/${DIRLIB}/$(@F) ${DIRTOP}/${DIRLIB}/$(patsubst %_${VER}.so,%.so,$(@F))

${CXXEXELIBS}: ${CPPOBJS} ${COBJS} ${CXXEXEOBJS}
	@(${EGREEN} "Creating exec file: "${WHITE} "$@"; ${RESET})
	@${CXX} ${CXXFLAGS} -o ${DIRBIN}/$@ ${DIRTOP}/${DIRLIB}/$@.o ${OBJS} ${PAM_LIB}/libGLTables.so ${PAM_LIB}/libsgp4.so ${PAM_YODA}/lib/libyoda.so -lThread `root-config --cflags --glibs` `${CERNLIB} mathlib`
	@(${EGREEN} "Creating libDarthVader.so library: "${WHITE} "libDarthVader_${VER}.so"; ${RESET})
	@${CXX} ${CXXFLAGS} -Xlinker -soname=libDarthVader_${VER}.so -shared -o ${DIRTOP}/${DIRLIB}/libDarthVader_${VER}.so $(LG2C) ${DIRTOP}/${DIRLIB}/libAcLevel2.so ${DIRTOP}/${DIRLIB}/libCaloLevel0.so ${DIRTOP}/${DIRLIB}/libCaloLevel1.so ${DIRTOP}/${DIRLIB}/libCaloLevel2.so  ${PAM_LIB}/libsgp4.so ${DIRTOP}/${DIRLIB}/libNDLevel2.so ${DIRTOP}/${DIRLIB}/libS4Level2.so ${DIRTOP}/${DIRLIB}/libToFLevel2.so ${DIRTOP}/${DIRLIB}/libTrigLevel2.so ${DIRTOP}/${DIRLIB}/libTrkLevel2.so ${DIRTOP}/${DIRLIB}/libTrkLevel1.so ${DIRTOP}/${DIRLIB}/libTrkLevel0.so ${DIRTOP}/${DIRLIB}/libTrkHough.so  ${DIRTOP}/${DIRLIB}/libTrkCalib.so ${DIRTOP}/${DIRLIB}/libTrkParams.so ${PAM_LIB}/libGLTables.so ${DIRTOP}/${DIRLIB}/libRunInfo.so ${DIRTOP}/${DIRLIB}/libOrbitalInfo.so ${DIRTOP}/${DIRLIB}/libOrientationInfo.so ${DIRTOP}/${DIRLIB}/libInclinationInfo.so ${DIRTOP}/${DIRLIB}/libExtTrkingAlg.so ${DIRTOP}/${DIRLIB}/libExtTrack.so  `root-config --glibs` `${CERNLIB} mathlib` 

#	@${CXX} ${CXXFLAGS} -o ${DIRBIN}/$@ ${DIRTOP}/${DIRLIB}/$@.o ${OBJS} ${PAM_YODA}/lib/libyoda.so ${PAM_YODA}/lib/libutils.so -lThread `root-config --cflags --glibs` `cernlib 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: 
	@for dir in `echo ${SUBDIR}`; do  \
	 (${EBLUE}  "Making clean in $${dir}";  ${RESET});\
	 (cd $${dir} &>/dev/null && ${MAKE} -s clean); \
	done; make -s locclean 

locclean: TOBEDEL= ${DIRSRC}/*.o ${DIRSRC}/*Dict.cpp ${DIRSRC}/*Dict.h ${FOBJS} ${FULLLIBS} ${DIRBIN}/${CXXEXELIBS} ${CPPOBJS} ${CXXEXEOBJS} ${FULLLINKCLIBS} ${FULLMACROCLIBS} ${FULLLINKMACROCLIBS} ${DIRSRC}/DarthVaderVerl2.cpp
locclean:
	@${test_and_rm}

distclean: 
	@for dir in `echo ${SUBDIR}`; do  \
	(${EBLUE}  "Making distclean in $${dir}";  ${RESET});\
	 (cd $${dir} &>/dev/null  && ${MAKE} -s distclean); \
	done; make -s locdistclean 

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


forceinstall: 
	@for dir in `echo ${SUBDIR}`; do  \
	(${EBLUE}  "Making forceinstall in $${dir}";  ${RESET});\
	 (cd $${dir} &>/dev/null && ${MAKE} -s forceinstall); \
	done; make locforceinstall

locforceinstall: FORCE=-f
locforceinstall: install

upgrade: 
	@for dir in `echo ${SUBDIR}`; do  \
	(${EBLUE}  "Making upgrade in $${dir}";  ${RESET});\
	 (cd $${dir}&>/dev/null && ${MAKE} -s upgrade); \
	done; make -s locupgrade

locupgrade: FORCE=-u
locupgrade: locinstall

upgradenc: 
	@for dir in `echo ${SUBDIR}`; do  \
	(${EBLUE}  "Making upgradenc in $${dir}";  ${RESET});\
	 (cd $${dir}&>/dev/null && ${MAKE} -s upgrade); \
	done; make -s locupgradenc

locupgradenc: FORCE=-u
locupgradenc: locinstallnc

install: 
	@for dir in `echo ${SUBDIR}`; do  \
	(${EBLUE}  "Making install in $${dir}";  ${RESET});\
	 (cd $${dir} &>/dev/null && ${MAKE} -s install); \
	done; make -s locinstall

locinstall: installlib installinc installmacros installcalib installdoc installexe installcmacro

locinstallnc: installlib installinc installmacros 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:
	@${cp_dir}

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

mkverpath:
	@echo '#include <TSystem.h>' >  ${DIRTOP}/${DIRSRC}/DarthVaderVerl2.cpp; echo 'const char *DarthVaderInfo(bool print){ if ( print ) printf("\nPAMELA Level2 flight package version: ${VER}\n\nCompiled on '${TODAY}' with: '${GCCVER}'\n\nOn: '${UNAMEA}'\n\n"); return("${VER}"); }' >>  ${DIRTOP}/${DIRSRC}/DarthVaderVerl2.cpp
	@sleep 0.8



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}
	@${EGREEN} "          INCLUDE :" ${WHITE} ${INCLUDE}
	@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 	cp_dir
wd=`pwd`;\
for i in ${TOBEMOVED} ; do \
wv=$${i##/*/}; \
dest=$${i/$${wv}/}; \
${EYELLOW} " Copying directory: "${WHITE} ${ORIGIN}/$${i##/*/} ;\
${EYELLOW} "          to: "${WHITE} ${PAM_CALIB} ; ${RESET}; \
cp -rf ${ORIGIN}/$${i##/*/} ${PAM_CALIB}; \
done; \
${EYELLOW} " Extracting mask directories into: "${WHITE} ${PAM_CALIB}/trk-param ; ${RESET}; \
cd  ${PAM_CALIB}/trk-param/ ;\
tar jxvf mask_param.tar.bz2; \
rm -rf mask_param.tar.bz2; \
cd - &>/dev/null; \
${EYELLOW} " done "; ${RESET}
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