#This makefile performs the following operations:
# 1. Check build environment
# 2. Build VmeInterface
# 3. Build RodDaq
# 4. Build SctRodDaq

#Pickup some important variable definitions
include build/Makefile.include

.PHONY: all check VmeInterface RodDaq SctRodDaq docs build dist sourceDist binDist

all: build

ifeq ($(DF_INST_PATH),)
build: check SctRodDaq
else 
build: check VmeInterface RodDaq SctRodDaq RodUtils
endif


#Variable modifications:
VmeInterface: ROD_DAQ := $(SCTPIXEL_DAQ_ROOT)
VmeInterface: DAQ_LIB_DIR := $(DF_INST_PATH)/$(CMTCONFIG)/lib
VmeInterface: DAQ_INCL_DIR := $(DF_INST_PATH)/include
# Not built by default, but needs env
RodUtils: ROD_DAQ := $(SCTPIXEL_DAQ_ROOT)
RodUtils: DAQ_LIB_DIR := $(DF_INST_PATH)/$(CMTCONFIG)/lib
RodUtils: DAQ_INCL_DIR := $(DF_INST_PATH)/include
RodUtils: DAQ_SW_DIR := $(DF_INST_PATH)
RodDaq: DAQ_SW_DIR := $(DF_INST_PATH)
export ROD_DAQ
export DAQ_LIB_DIR
export DAQ_INCL_DIR
export DAQ_SW_DIR

#Rules

#Check these environment variables
check:
	@test -z `echo $$PATH | grep $$SCT_DAQ_ROOT/installed/share/bin` && echo "You have not sourced the setup.[c]sh script.  Please source it before first" && false || true;
	@test -z $$TDAQ_INST_PATH && echo "TDAQ_INST_PATH not defined.  Have you forgotten to install the online software?" && false || true; 
	@test -z $$BOOSTDIR && echo "BOOSTDIR not defined.  You should get an appropriate version of Boost from www.boost.org" && false || true;  
	@test -z $$ROOTSYS && echo "ROOTSYS not defined.  You need to get and install Root!" && false || true;
	@test -z `echo $$CLASSPATH | grep junit.jar` && echo "junit.jar not on classpath.  Please download from www.junit.jar and add it to the classpath" && false || true;
	@test -z $$SCT_DAQ_EXT && echo "SCT_DAQ_EXT not defined.  You should define this to point to wherever the external jar files that are needed are. \
	These jar files should be available as a tar file from the release website" && false || true;
	@test -z $$NAGDIR && echo "NAGDIR not defined.  Everything will still work, but slower ROOT fitting will be used.  \
	Get NagC v6 and point NAGDIR to it if you want real speed!" || true;
	@test -z $$DF_INST_PATH && echo "DF_INST_PATH not defined.  Without Dataflow software, some dummy interface software may build, but this cannont be \
	guarenteed.  To help you digest this, I will now sleep for 5 seconds" && sleep 5 || true;

VmeInterface:
	$(MAKE) -C $(SCTPIXEL_DAQ_ROOT)/../VmeInterface

RodDaq:
	$(MAKE) -C $(SCTPIXEL_DAQ_ROOT)/RodCrate

RodTests:
	$(MAKE) -C $(SCTPIXEL_DAQ_ROOT)/RodTests

# Make some attempt at building them...
RodUtils:
	-$(MAKE) -C $(SCTPIXEL_DAQ_ROOT)/RodUtils
	cd $(SCTPIXEL_DAQ_ROOT)/RodUtils; rm *.o

SctRodDaq:
	$(MAKE) -C build optimize

docs:
	$(MAKE) -C build docs

dist: sourceDist binDist
ifeq ($(DF_INST_PATH),)
	$(error DF_INST_PATH must be set to make a distribution)
endif

PARENTDIR := $(shell pwd | sed -e 's/.*\///')
RELEASEFILES :=  Makefile README setup.csh setup.sh start exampleSetup.csh exampleSetup.sh installed RodDaq/RodUtils 
BINDISTFILES := $(addprefix $(PARENTDIR)/,$(RELEASEFILES))  
SOURCEDISTFILES := $(addprefix $(PARENTDIR)/,$(wildcard *))

binDist:
	$(MAKE)
	$(MAKE) docs
	tar -C ../ -czf BinaryRelease.tar.gz $(BINDISTFILES)  

sourceDist:
	tar -C ../ -czf SourceRelease.tar.gz $(SOURCEDISTFILES)

#Default - don't do any check before hand or anything else - convenience only!
%:
	$(MAKE) -C build $(MAKECMDGOALS)
