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

gsl_fe		=	$(shell awk 'BEGIN {n=0} /^ *\#define RECONSTRUCT/ {n++} END {print n}' reader.h)
ifeq ($(gsl_fe),1)
gsl		=	/usr/local
gsl_i3		=	/usr/home/icecube/tools/gsl-1.5
gsl_h		=	-I$(gsl)/include -I$(gsl_i3)/include
gsl_lib		=	-L$(gsl)/lib -L$(gsl_i3)/Linux-i386/lib -lgsl -lgslcblas
gsl_c		=	interpol.cxx llhreco.cxx
gsl_o		=	interpol.o llhreco.o
endif

rdmc_fe		=	$(shell awk 'BEGIN {n=0} /^ *\#define RECOF2KMC/ {n++} END {print n}' reader.h)
ifeq ($(rdmc_fe),1)
rdmc_h		=	-I.
rdmc_lib	=	-L. -lrdmc
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) )

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

llhreco.o:
	c++ llhreco.cxx -c $(opt) $(gsl_h)

fat-reader:	tcalfit.o $(gsl_o) reader.o
	c++ fat-reader.cxx -o fat-reader $(opt) $(root_lib) $(xml_lib) tcalfit.o $(gsl_o) reader.o $(gsl_lib) $(rdmc_h) $(rdmc_lib) && strip fat-reader || \
	c++ fat-reader.cxx -o fat-reader $(opt) $(root_lib) $(xml_exp) tcalfit.o $(gsl_o) reader.o $(gsl_lib) $(rdmc_h) $(rdmc_lib) && 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 $(gsl_c) fat-reader.cxx reader.cxx -o fat-reader $(opt) \
	$(gsl_h) $(root_h) $(xml_h) $(gsl_lib) $(root_lib) $(xml_lib) $(rdmc_h) $(rdmc_lib) && strip fat-reader
