opt		=	-O2 -D_FILE_OFFSET_BITS=64
static		=	-static

xml		=	
xml_lib_x	=	-lxmltok -lxmlparse
xml_exp_x	=	-lexpat

ifeq ($(xml),)
xml_h		=	
xml_lib_y	=	$(xml_lib_x)
xml_exp_y	=	$(xml_exp_x)
else
ifeq ($(xml),.)
xml_h		=	-I.
xml_exp_y	=	-L. -lexpat
else
xml_h		=	-I$(xml)/include
xml_lib_y	=	-L$(xml)/lib $(xml_lib_x)
xml_exp_y	=	-L$(xml)/lib $(xml_exp_x)
endif
endif

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

root_fe		=	$(shell awk 'BEGIN {n=0} /^ *\#define (USE_ROOT_FE|OUTHIST)/ {n=1} END {print n}' reader.h outhist.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)
ifdef I3_PORTS
gsl		=	$(I3_PORTS)
gsl_h		=	-I$(gsl)/include/gsl-1.8
gsl_lib		=	-L$(gsl)/lib/gsl-1.8 -lgsl -lgslcblas
else
gsl		=	/usr/local
gsl_h		=	-I$(gsl)/include/gsl
gsl_lib		=	-L$(gsl)/lib -lgsl -lgslcblas
endif
gsl_c		=	interpol.cxx llhreco.cxx
gsl_o		=	interpol.o llhreco.o
endif

i3m_fe		=	$(shell awk 'BEGIN {n=0} /^ *\#define I3HELPER/ {n++} END {print n}' reader.h)
ifeq ($(i3m_fe),1)
i3m_lib		=	-L. -li3helper $(shell ln -sf i3helper/public/i3helper/i3helper.h)
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

version		=	$(shell sed -n "2s/^/-D VERSION='\"/p; 5s/^/(/; 5s/$$/)\"'/p" README)
arch		=	$(shell uname -m)
ifeq ($(arch),x86_64)
archx64		=	-D ARCHX64
endif

all:	clean fat-reader rmo

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

reader.o:
	c++ $(version) reader.cxx -c $(opt) $(gsl_h) $(root_h) $(xml_h) || \
	( sed 's-expat-xmlparse-' reader.cxx > foo.cxx && mv foo.cxx reader.cxx && c++ reader.cxx -c $(opt) $(gsl_h) $(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 $(i3m_lib) $(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 $(i3m_lib) $(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++ $(version) tcalfit.cxx $(gsl_c) fat-reader.cxx reader.cxx -o fat-reader $(opt) $(gsl_h) $(root_h) $(xml_h) $(i3m_lib) $(gsl_lib) $(root_lib) $(xml_lib) $(rdmc_h) $(rdmc_lib) && strip fat-reader

static:
	c++ $(version) $(static) tcalfit.cxx $(gsl_c) fat-reader.cxx reader.cxx -o fat-reader $(opt) $(gsl_h) $(root_h) $(xml_h) $(i3m_lib) $(gsl_lib) $(root_lib) $(xml_lib) $(rdmc_h) $(rdmc_lib) && strip fat-reader

i3reco:
	sh -c "for i in *.{h,cxx}; do echo \$$i; cat \$$i | sed 's-I3FATreader-I3FRreco-' > foo; mv foo \$$i; done"

expat:
	ln -s $(I3_PORTS)/lib/expat-1.2/libexpat.a
	ln -s $(I3_PORTS)/include/expat-1.2/xmlparse/xmlparse.h expat.h

i3:
	./scripts/i3dirs.sh
	ln -s $(I3_BUILD)/lib/libi3helper.so
	ln -s $(I3_BUILD)/neutrinoflux/resources
