# Compilers settings
ROOTCFLAGS  =`root-config --cflags`
ROOTLDFLAGS =`root-config --ldflags`
ROOTLIBS    =`root-config --libs` -lHbook
CERNLIBS    =`cernlib kernlib,packlib,genlib`
F77LIBS     =-lg2c -lnsl
SYSLIBS     =-lm -ldl -rdynamic
LD          =g++
CXXFLAGS=-g -O -Wall -fPIC ${ROOTCFLAGS}
#CXXFLAGS= -g -Wall -O ${ROOTCFLAGS}
LDFLAGS = -g

DICS = pCasID.cpp pHeaderID.cpp pTof.cpp pCat.cpp \
	pTofHit.cpp pCal.cpp pCatHit.cpp pHitID.cpp pTofID.cpp \
	pCalHit.cpp pCatID.cpp pNdID.cpp pTrdID.cpp \
	pCalID.cpp pCaliID.cpp \
	pCas.cpp pCasHit.cpp pSpeID.cpp

MGRS = pPheaMgr.cpp pPhitMgr.cpp pPdetMgr.cpp 
UTIL = pUtils.cpp pBlockPointer.cpp  

SRCS = pCasID.cpp pHeaderID.cpp pTof.cpp pCat.cpp pHit.cpp \
	pTofHit.cpp pCal.cpp pCatHit.cpp pHitID.cpp pTofID.cpp \
	pCalHit.cpp pCatID.cpp pNdID.cpp pTrdID.cpp \
	pCalID.cpp pDetector.cpp pCaliID.cpp pEvent.cpp \
	pCas.cpp pCasHit.cpp pHeader.cpp pSpeID.cpp \
	pPheaMgr.cpp pPhitMgr.cpp pPdetMgr.cpp \
	pUtils.cpp pBlockPointer.cpp  

HEAS =$(SRCS:.cpp=.h)
OBJS =$(SRCS:.cpp=.o)
DEPS =$(SRCS:.cpp=.d)
CLAS =$(DICS:.cpp= )
CLASH=$(DICS:.cpp=.h)
JUNK =$(addsuffix ~,${SRCS}) $(addsuffix ~,${HEAS}) 
DICO =$(addsuffix Dict.o, ${CLAS})
DICH =$(addsuffix Dict.h, ${CLAS})

all: gp2root libpEvent.so

testcashit: testcashit.o pAllEventDict.o ${OBJS} ${DICO}
	$(LD) $(LDFLAGS) -o $@ $^ ${ROOTLIBS} 


libpEvent.so: LDFLAGS=-O `root-config --ldflags`
libpEvent.so: SOFLAGS=-shared
libpEvent.so: pAllEventDict.o ${OBJS} ${DICO} 
	$(LD) $(SOFLAGS) $(LDFLAGS) $^ -o $@

%Dict.cxx: %.h %LinkDef.h
	rootcint -f $@ -c $^

%Dict.o: %Dict.cxx

%LinkDef.h: 
	@echo "#pragma link off all class;" > $@
	@echo "#pragma link off all function;" >> $@
	@echo "#pragma link off all global;" >> $@
	@echo "#pragma link C++ class `echo $@ | sed 's/LinkDef.h//'`;" >> $@

pAllEventLinkDef.h: pEvent.h pDetector.h pHit.h pHeader.h
	@echo "#pragma link off all class;" > $@
	@echo "#pragma link off all function;" >> $@
	@echo "#pragma link off all global;" >> $@
	@for i in $^; do \
		echo "#pragma link C++ class `echo "$$i" | sed 's/.h//'`;" >> $@;\
	 done


pAllEventDict.cxx: pEvent.h pDetector.h pHit.h pHeader.h pAllEventLinkDef.h
	rootcint -f $@ -c $^

gp2root:  gp2root.o pAllEventDict.o ${OBJS} ${DICO}
	$(LD) $(LDFLAGS) -o $@ $^ ${ROOTLIBS} ${CERNLIBS} ${F77LIBS} ${SYSLIBS}

depend: ${DEPS}

%.d: %.cpp
	@echo Creating dependencies for $< 
	@$(CXX) -MM $(CXXFLAGS) $< -o $@

-include ${DEPS}

.SUFFIXES: .cxx
.cxx.o:
	$(CXX) $(CXXFLAGS) -c $< `root-config --cflags`


.PHONY: clean 

clean:
	@echo Removing object files .....
	@rm -f ${OBJS} ${DICO} 
	@echo Removing dependencies files .....
	@rm -f ${DEPS}
	@echo Removing dictionary files .....
	@rm -f ${JUNK} ${DICH}
	@echo Removing temporary files .....
	@rm -f ${JUNK}