opt		=	-O2

xml_h		=	
xml_lib_x	=	-lxmltok -lxmlparse
xml_exp_x	=	-lexpat

xml_which	=	$(shell awk 'BEGIN {n=0} /^ *\#include <expat.h>/ {n++} END {print n}' reader.cxx)
ifeq ($(xml_which),0)
xml_lib		=	$(xml_lib_x)
xml_exp		=	$(xml_exp_x)
else
xml_lib		=	$(xml_exp_x)
xml_exp		=	$(xml_lib_x)
endif

root_fe		=	$(shell awk 'BEGIN {n=0} /^ *\#define USE_ROOT_FE/ {n++} END {print n}' reader.h)
ifeq ($(root_fe),1)
root_h		=	$(shell root-config --cflags)
root_lib	=	$(shell root-config --glibs)
endif

all:	clean fat-reader rmo

tcalfit.o:
	c++ tcalfit.cxx -c $(opt)

reader.o:
	c++ reader.cxx -c $(opt) $(root_h) $(xml_h) || \
	( sed 's-xml.*rse-expat-' reader.cxx > foo.cxx && mv foo.cxx reader.cxx && c++ reader.cxx -c $(opt) $(root_h) $(xml_h) )

fat-reader:	tcalfit.o reader.o
	c++ fat-reader.cxx -o fat-reader $(opt) $(root_lib) $(xml_lib) tcalfit.o reader.o && strip fat-reader || \
	c++ fat-reader.cxx -o fat-reader $(opt) $(root_lib) $(xml_exp) tcalfit.o reader.o && strip fat-reader

jj-reader:	clean tcalfit.o
	c++ jj-reader.cxx -o jj-reader $(opt) tcalfit.o && strip jj-reader

clean:
	rm *.o *~ jj-reader fat-reader >& /dev/null || true

rmo:
	rm *.o >& /dev/null || true

simple:	
	c++ tcalfit.cxx fat-reader.cxx reader.cxx -o fat-reader $(opt) $(root_lib) $(xml_lib) && strip fat-reader
