opt		=	-O2 -D_FILE_OFFSET_BITS=64

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define XMLPARSE/ {n++} END {print n}' fat-reader.cxx),1)
xml		=	-lexpat # -lxmltok -lxmlparse
endif

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define USEZLIB/ {n++} END {print n}' fat-reader.cxx),1)
zlib		=	-lz
endif

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define RECONSTRUCT/ {n++} END {print n}' fat-reader.cxx),1)
ifdef I3_PORTS
dir		=	-I$(I3_PORTS)/include/gsl-1.8 -L$(I3_PORTS)/lib/gsl-1.8
else
dir		=
endif
gsl		=	$(dir) -lgsl -lgslcblas
endif

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define OUTHIST/ {n=1} END {print n}' fat-reader.cxx),1)
root		=	$(shell root-config --cflags) $(shell root-config --glibs)
endif

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define RECOF2KMC/ {n++} END {print n}' fat-reader.cxx),1)
rdmc		=	-Irdmc -Lrdmc -lrdmc
endif

ifeq ($(shell awk 'BEGIN {n=0} /^ *\#define I3HELPER/ {n++} END {print n}' fat-reader.cxx),1)
i3m		=	-L$(I3_BUILD)/lib -li3helper
endif

version		=	$(shell sed -n "2s/^/-D VERSION='\"/p; 5s/^/(/; 5s/$$/)\"'/p" README)
libs		=	$(opt) $(xml) $(zlib) $(gsl) $(root) $(rdmc) $(i3m)
all		=	$(version) fat-reader.cxx -o fat-reader $(libs)

all:
	eval `cat .i3dirs 2>/dev/null` make simple

static:
	c++ $(all) -static && strip fat-reader

simple:
	c++ $(all)

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

i3:
	./scripts/i3dirs.sh

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