#---------------------------------------------------------------------------
#
# Makefile for a Linux System:
# use GNU FORTRAN compiler g77
# Makefile done for tracker dara
#
#---------------------------------------------------------------------------
#
# FC		Fortran compiler for standard rules
# FFLAGS	Fortran flags for standard rules
# CC		C Compiler for standard rules
# CFLAGS	C compiler flags for standard rules 
#
# Compiler:     g77     
# Flags:-g      	(enable debug) 
#	-Wall		(turn on all warnings)
#      	-fno-automatic	(force compiler to use static variables)  
# 	-fvxt		(use VAXish extensions)
#

# Fortran flags
FC=g77
FFLAGS=-fvxt -fno-automatic -Wall -Wno-globals -fbounds-check# -O2
# puo' essere tolto con cautela:
# impedisce di far vedere gli warning di HBNAME

# C flags
CC=gcc
CFLAGS=-Wall -O2
CFLADJ=-c

# used libraries 
#LIBS=-L/cern/pro/lib -lkernlib -lpacklib -lgraflib -lmathlib -lnsl
LIBS= `cernlib kernlib packlib graflib mathlib`

#---------------------------------------------------------------------------

LIBDIR=./lib
SRCDIR=./source
PARDIR=$(SRCDIR)/parameters
BINDIR=./bin
AUXDIR=$(BINDIR)/bin-aux

BINDIR2003=./bin-2003
AUXDIR2003=$(BINDIR2003)/bin-aux

usage:
	@echo ===========================
	@echo USAGE:
	@echo 
	@echo For external users:
	@echo 
	@echo "make all"
	@echo "make install"
	@echo 
	@echo 
	@echo Other labels for internal users:
	@echo 
	@echo "level0.exe"
	@echo "level1.exe"
	@echo "level2.exe"
	@echo "level2-2003.exe"
	@echo "all              (==> level0.exe level1.exe level2.exe lib)"
	@echo "level2-both      (==> level2.exe level2-2003.exe)"
	@echo "level2-efficiency.exe"
	@echo "level2-calibration.exe"
	@echo 
	@echo "install"
	@echo 
	@echo "lib"
	@echo 
	@echo "clean0"
	@echo "clean1"
	@echo "clean2"
	@echo "clean2003"
	@echo "cleanl"
	@echo "clean             (==> clean0 clean1 clean2 cleanl)"
	@echo "cleant"
	@echo 


all: level0.exe level1.exe level2.exe lib


FOBJ0= \
source/readraw/readraw_subroutines.o \
source/readraw/trkunpack.o

COBJ0= \
source/readraw/findstart.o \
source/readraw/hunpacker.o \
source/readraw/skipbyte.o \
source/readraw/dunpacker.o \
source/readraw/readtrailer.o \
source/readraw/readped.o \
source/readraw/readsig.o \
source/readraw/readbad.o \
source/readraw/find_cpu_header.o \
source/readraw/trkalarmpkt.o \
source/readraw/runheaderpkt.o \
source/readraw/get_bytes.o \
source/readraw/crc.o

COMMON0= \
source/common/commontracker.f \
source/common/common_readraw.f \
source/common/level0.f \
source/common/calib.f 

OBJTOF= \
source/tof/tofunpack.for \
source/tof/graytobin.for \
source/tof/tofcrc.cpp 

OBJTRI= \
source/trigger/triggerunpack.for \
source/trigger/trigcrc.cpp

TOFCOM=source/tof/common_tof.f 


level0.exe: bindir source/readraw/readraw.f $(FOBJ0) $(COBJ0)  $(OBJTOF) $(COMMON0) $(TOFCOM)
	@echo Creating $(BINDIR)/$@ .....
	$(FC) $(FFLAGS) -o bin/level0.exe source/readraw/readraw.f $(FOBJ0) $(COBJ0) $(OBJTOF) $(OBJTRI) $(LIBS)
	@echo $@ DONE!  
	@echo ===========================================================================================

FOBJ1= \
source/reduction/fillpedsig.o \
source/reduction/filladc.o \
source/reduction/cncomp.o \
source/reduction/cnoise.o \
source/reduction/cutcn.o \
source/common/access_level0.o \
source/common/functions.o

COMMON1= \
source/common/commontracker.f \
source/common/level0.f \
source/common/level1.f \
source/common/common_reduction.f \
source/common/calib.f


level1.exe: bindir source/reduction/reduction.f  $(FOBJ1) $(COMMON1)
	@echo Creating $(BINDIR)/$@ .....
	$(FC) $(FFLAGS) -o bin/level1.exe source/reduction/reduction.f $(FOBJ1) $(LIBS) 
	@echo $@ DONE!  
	@echo ===========================================================================================

FOBJ2=\
source/common/functions.f \
source/analysis/mini_2.f\
source/common/readalignparam.f \
source/common/functions_pfa.f \
source/reduction/fillpedsig.f\
source/analysis/tricircle.f \
source/analysis/BdL.f 

FOBJTRK=\
source/analysis/grkuta.f \
source/magnet/inter_B.f \
source/magnet/read_B.f 


COMMON2=\
source/common/commontracker.f \
source/common/calib.f \
source/common/level1.f \
source/common/common_mech.f \
source/common/common_level2debug.f \
source/common/common_momanhough.f \
source/common/momanhough_init.f \
source/common/common_xyzPAM.f \
source/common/common_align.f \
source/common/common_resxy.f \
source/common/level2.f \
source/common/common_mini_2.f 

COMMONTRK=\
source/common/common_B_inner.f \



level2-both:  level2.exe level2-2003.exe

level2.exe: auxdir source/analysis/momanhough.F $(FOBJ2) $(FOBJTRK) $(COMMON2) $(COMMONTRK) source/common/access_level1.f 
	@echo Creating $(BINDIR)/$@ .....
	cpp -x f77-cpp-input source/analysis/momanhough.F source/analysis/momanhough.f 
	$(FC) $(FFLAGS) -o bin/level2.exe source/analysis/momanhough.f $(FOBJ2) $(FOBJTRK) source/common/access_level1.f $(LIBS) 
	@rm -f source/analysis/momanhough.f
	@echo Moving auxiliary files in $(AUXDIR) .....
	@cp source/parameters/align_param/*.dat $(AUXDIR)/.
	@cp source/parameters/eta_param/*.dat $(AUXDIR)/.
	@cp source/parameters/charge_param/*.dat $(AUXDIR)/.
	@cp source/parameters/field_meas/*.rz $(AUXDIR)/.
	@cp source/parameters/mip_param/*.dat $(AUXDIR)/.
#	@cp source/parameters/mech_pos.dat $(AUXDIR)/.
	@echo $@ DONE!  
	@echo ===========================================================================================


level2-2003.exe: auxdir2003 source/analysis/momanhough.F $(FOBJ2) $(FOBJTRK) $(COMMON2) $(COMMONTRK) source/common/access_level1_2003.f 
	@echo Creating $(BINDIR2003)/$@ .....
	cp -f source/common/level1.f source/common/level1.f-cpu
	cp -f source/common/level1-2003.f source/common/level1.f
	cpp -x f77-cpp-input -DTEST2003 source/analysis/momanhough.F source/analysis/momanhough.f 
	$(FC) $(FFLAGS) -o $(BINDIR2003)/level2-2003.exe source/analysis/momanhough.f $(FOBJ2) $(FOBJTRK) source/common/access_level1_2003.f $(LIBS) 
	@rm -f source/analysis/momanhough.f
	cp -f source/common/level1.f-cpu source/common/level1.f
	@echo Moving auxiliary files in $(AUXDIR2003) .....
	@cp source/parameters/align_param-2003/*.dat $(AUXDIR2003)/.
	@cp source/parameters/eta_param-2003/*.dat $(AUXDIR2003)/.
	@cp source/parameters/charge_param-2003/*.dat $(AUXDIR2003)/.
	@cp source/parameters/field_meas/*.rz $(AUXDIR2003)/.
	@cp source/parameters/mip_param/*.dat $(AUXDIR)/.
#	@cp source/parameters/mech_pos.dat $(AUXDIR)/.
#	@cp source/common/level2.f utilities/template/trk_level2.inc
	@echo $@ DONE!  
#	cp bin/level2-2003.exe $(TRK_BIN)/.
	@echo ================================================

level2-efficiency.exe: auxdir source/analysis/momanhough-efficiency.F $(FOBJ2) $(FOBJTRK) $(COMMON2) $(COMMONTRK) source/common/access_level1.f 
	@echo Creating $(BINDIR)/$@ .....
	cpp -x f77-cpp-input source/analysis/momanhough-efficiency.F source/analysis/momanhough-efficiency.f
	$(FC) $(FFLAGS) -o bin/level2-efficiency.exe source/analysis/momanhough-efficiency.f $(FOBJ2) $(FOBJTRK) source/common/access_level1.f $(LIBS) 
	@rm -f source/analysis/momanhough-efficiency.f
	@echo Moving auxiliary files in $(AUXDIR) .....
	@cp source/parameters/align_param/*.dat $(AUXDIR)/.
	@cp source/parameters/eta_param/*.dat $(AUXDIR)/.
	@cp source/parameters/charge_param/*.dat $(AUXDIR)/.
	@cp source/parameters/field_meas/*.rz $(AUXDIR)/.
	@cp source/parameters/mip_param/*.dat $(AUXDIR)/.
#	@cp source/parameters/mech_pos.dat $(AUXDIR)/.
	@echo $@ DONE!  
	@echo ===========================================================================================

level2-calibration.exe: auxdir source/analysis/momanhough-calibration.F $(FOBJ2) $(FOBJTRK) $(COMMON2) $(COMMONTRK) source/common/access_level1.f source/common/common_calibration.f 
	@echo Creating $(BINDIR)/$@ .....
	cpp -x f77-cpp-input source/analysis/momanhough-calibration.F source/analysis/momanhough-calibration.f
	$(FC) $(FFLAGS) -o bin/level2-calibration.exe source/analysis/momanhough-calibration.f $(FOBJ2) $(FOBJTRK) source/common/access_level1.f $(LIBS) 
	@rm -f source/analysis/momanhough-calibration.f
	@echo Moving auxiliary files in $(AUXDIR) .....
	@cp source/parameters/align_param/*.dat $(AUXDIR)/.
	@cp source/parameters/eta_param/*.dat $(AUXDIR)/.
	@cp source/parameters/charge_param/*.dat $(AUXDIR)/.
	@cp source/parameters/field_meas/*.rz $(AUXDIR)/.
#	@cp source/parameters/mech_pos.dat $(AUXDIR)/.
	@cp source/parameters/mip_param/*.dat $(AUXDIR)/.
	@cp source/parameters/data-card.dat $(AUXDIR)/.
	@echo $@ DONE!  
	@echo ===========================================================================================



TESTDIR=./run-test
UTILDIR=./utilities

install:	
	@test -d "$(TRK_BIN)" && (echo "Now moving executable and auxiliary files to the working directory$(TRK_BIN)" ; \
	cp $(BINDIR)/*.exe $(TRK_BIN)/.; \
	cp -r $(AUXDIR) $(TRK_BIN)/. ) || (echo "*** NB  Tracker working directory not defined !!") 
	@echo "Installing test directory and utilities..."
	@cp $(BINDIR)/*.exe $(TESTDIR)/. 
	@cp -r $(AUXDIR) $(TESTDIR)/. 
#	@echo "Tar archive with tracker utilities:"
#	@tar -xvzf track.tar.gz -C $(UTILDIR)/trk-viewer/




lib: libdir source/subroutines/track.f $(FOBJTRK) $(COMMONTRK)
	@echo Creating ./$@ .....
	g77 $(FFLAGS) -static -c source/subroutines/track.f -o source/subroutines/track.o
	g77 $(FFLAGS) -static -c source/analysis/grkuta.f -o source/subroutines/grkuta.o
	g77 $(FFLAGS) -static -c source/magnet/inter_B.f -o source/subroutines/inter_B.o
	g77 $(FFLAGS) -static -c source/magnet/read_B.f -o source/subroutines/read_B.o
	gcc -Wall,-soname,libtrk.so -shared -o lib/libtrk.so source/subroutines/*.o #$(LIBS)
	ar rcs lib/libtrk.a source/subroutines/*.o
	ranlib lib/libtrk.a
	@cp -r $(AUXDIR) utilities/template/. 
	@echo Creating tar file with track utilities...
	@test -d track/ || mkdir track
	@cp source/subroutines/track.f source/analysis/grkuta.f source/magnet/*.f $(AUXDIR)/*.rz track/
	ls track/
	tar -cvzf track.tar.gz track/*
	@rm -fr track
	@echo $@ DONE!
#---------------------------------------------------------------------------


bindir:
	@test -d "${BINDIR}" || (echo "Creating ${BINDIR}"; mkdir ${BINDIR})
auxdir: bindir
	@test -d "${AUXDIR}" || (echo "Creating ${AUXDIR}"; mkdir ${AUXDIR})
bindir2003:
	@test -d "${BINDIR2003}" || (echo "Creating ${BINDIR2003}"; mkdir ${BINDIR2003})
auxdir2003: bindir2003
	@test -d "${AUXDIR2003}" || (echo "Creating ${AUXDIR2003}"; mkdir ${AUXDIR2003})
libdir: 
	@test -d "${LIBDIR}" || (echo "Creating ${LIBDIR}"; mkdir ${LIBDIR})




.PHONY : clean
clean: clean0 clean1 clean2 cleanl
	rm -fr $(BINDIR)

cleant: 
	rm -f run-test/*~
	rm -f run-test/raw-data
	rm -f run-test/*.exe
	rm -fr run-test/bin-aux
	rm -f *~

clean0:
	rm -f source/readraw/*.o
	rm -f source/tof/*.o
	rm -f source/trigger/*.o
	rm -f source/tof/*~
	rm -f source/trigger/*~
	rm -f source/readraw/*~
	rm -f source/common/*~
	rm -f $(BINDIR)/level0.exe	
	rm -f run-test/level0.exe
clean1:
	rm -f source/reduction/*.o
	rm -f source/reduction/*~
	rm -f source/common/*~
	rm -f $(BINDIR)/level1.exe	
	rm -f run-test/level1.exe
clean2:
	rm -f source/analysis/*.o
	rm -f source/magnet/*.o
	rm -f source/analysis/*~
	rm -f source/magnet/*~
	rm -f source/common/*~
	rm -f $(BINDIR)/level2*.exe	
	rm -f run-test/level2.exe

clean2003:
	rm -f source/analysis/*.o
	rm -f source/magnet/*.o
	rm -f source/analysis/*~
	rm -f source/magnet/*~
	rm -f source/common/*~
	rm -f $(BINDIR2003)/level2*.exe	
	rm -fr $(BINDIR2003)/bin-aux	
	rm -f run-test/level2.exe

cleanl:
	rm -f source/subroutines/*~
	rm -f source/subroutines/*.o
	rm -fr ./lib
#	rm -f lib/*.so
#	rm -f lib/*.a
