#OBJ_DIR = ./
HEADERS = src/Variant.h \
		  src/split.h \
		  src/pdflib.hpp \
		  src/var.hpp \
                  src/cdflib.hpp \
		  src/rnglib.hpp \
		  src/join.h
SOURCES = src/Variant.cpp \
		  src/rnglib.cpp \
		  src/var.cpp \
		  src/pdflib.cpp \
		  src/cdflib.cpp \
		  src/split.cpp
OBJECTS= $(SOURCES:.cpp=.o)

VCF_LIB_LOCAL:=$(shell pwd)
BIN_DIR:=bin
LIB_DIR:=lib
SRC_DIR=src
INC_DIR:=include
OBJ_DIR:=obj

LIB =		libvcflib.a
SOVERSION =	1
SLIB =		libvcflib.so.$(SOVERSION)

# TODO
#vcfstats.cpp

BIN_SOURCES = src/vcfecho.cpp \
			  src/vcfnormalizesvs.cpp \
			  src/dumpContigsFromHeader.cpp \
			  src/bFst.cpp \
			  src/pVst.cpp \
			  src/hapLrt.cpp \
			  src/popStats.cpp \
			  src/wcFst.cpp \
			  src/iHS.cpp \
			  src/segmentFst.cpp \
			  src/segmentIhs.cpp \
			  src/genotypeSummary.cpp \
			  src/sequenceDiversity.cpp \
			  src/pFst.cpp \
			  src/smoother.cpp \
			  src/LD.cpp \
			  src/plotHaps.cpp \
			  src/abba-baba.cpp \
			  src/permuteGPAT++.cpp \
			  src/permuteSmooth.cpp \
			  src/normalize-iHS.cpp \
			  src/meltEHH.cpp \
			  src/vcfaltcount.cpp \
			  src/vcfhetcount.cpp \
			  src/vcfhethomratio.cpp \
			  src/vcffilter.cpp \
			  src/vcf2tsv.cpp \
			  src/vcfgenotypes.cpp \
			  src/vcfannotategenotypes.cpp \
			  src/vcfcommonsamples.cpp \
			  src/vcfremovesamples.cpp \
			  src/vcfkeepsamples.cpp \
			  src/vcfsamplenames.cpp \
			  src/vcfgenotypecompare.cpp \
			  src/vcffixup.cpp \
			  src/vcfclassify.cpp \
			  src/vcfsamplediff.cpp \
			  src/vcfremoveaberrantgenotypes.cpp \
			  src/vcfrandom.cpp \
			  src/vcfparsealts.cpp \
			  src/vcfstats.cpp \
			  src/vcfflatten.cpp \
			  src/vcfprimers.cpp \
			  src/vcfnumalt.cpp \
			  src/vcfcleancomplex.cpp \
			  src/vcfintersect.cpp \
			  src/vcfannotate.cpp \
			  src/vcfallelicprimitives.cpp \
			  src/vcfoverlay.cpp \
			  src/vcfaddinfo.cpp \
			  src/vcfkeepinfo.cpp \
			  src/vcfkeepgeno.cpp \
			  src/vcfafpath.cpp \
			  src/vcfcountalleles.cpp \
			  src/vcflength.cpp \
			  src/vcfdistance.cpp \
			  src/vcfrandomsample.cpp \
			  src/vcfentropy.cpp \
			  src/vcfglxgt.cpp \
			  src/vcfroc.cpp \
			  src/vcfcheck.cpp \
			  src/vcfstreamsort.cpp \
			  src/vcfuniq.cpp \
			  src/vcfuniqalleles.cpp \
			  src/vcfremap.cpp \
			  src/vcf2fasta.cpp \
			  src/vcfsitesummarize.cpp \
			  src/vcfbreakmulti.cpp \
			  src/vcfcreatemulti.cpp \
			  src/vcfevenregions.cpp \
			  src/vcfcat.cpp \
			  src/vcfgenosummarize.cpp \
			  src/vcfgenosamplenames.cpp \
			  src/vcfgeno2haplo.cpp \
			  src/vcfleftalign.cpp \
			  src/vcfcombine.cpp \
			  src/vcfgeno2alleles.cpp \
			  src/vcfindex.cpp \
			  src/vcf2dag.cpp \
			  src/vcfsample2info.cpp \
			  src/vcfqual2info.cpp \
			  src/vcfinfo2qual.cpp \
			  src/vcfglbound.cpp \
			  src/vcfunphase.cpp \
			  src/vcfnull2ref.cpp \
			  src/vcfinfosummarize.cpp

# when we can figure out how to build on mac
# src/vcfsom.cpp

#BINS = $(BIN_SOURCES:.cpp=)
BINS = $(addprefix bin/,$(notdir $(BIN_SOURCES:.cpp=)))
SHORTBINS = $(notdir $(BIN_SOURCES:.cpp=))
# Use ?= to allow overriding from the env or command-line.

MAKE ?=		make
LOCALBASE ?=	/usr/local
LIB_PATH ?=	${LOCALBASE}/lib

CC ?=		cc
CXX ?=		c++
CXXFLAGS ?=	-O3
CFLAGS +=	-D_FILE_OFFSET_BITS=64 -fPIC
CXXFLAGS +=	$(CFLAGS) --std=c++11
#CXXFLAGS +=	-pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual

DESTDIR ?=	stage
PREFIX ?=	/usr/local
STRIP ?=	strip
INSTALL ?=	install -c
MKDIR ?=	mkdir -p
AR ?=		ar

SSW =		src/ssw.o src/ssw_cpp.o

INCLUDES =	-I${LOCALBASE}/include \
		-I${LOCALBASE}/include/smithwaterman \
		-I${LOCALBASE}/include/multichoose \
		-I${LOCALBASE}/include/fastahack \
		-I${LOCALBASE}/include/intervaltree
LDFLAGS +=	-L. -lvcflib \
		-L$(LIB_PATH) -lsw -ltabix -lhts -lfastahack -lfilevercmp \
		-lpthread -lz -lm

all: $(OBJECTS) $(BINS) $(LIB) $(SLIB)

ssw.o:		src/ssw.h
ssw_cpp.o:	src/ssw_cpp.h

openmp:
	$(MAKE) CXXFLAGS="$(CXXFLAGS) -fopenmp -D HAS_OPENMP"

profiling:
	$(MAKE) CXXFLAGS="$(CXXFLAGS) -g" all

gprof:
	$(MAKE) CXXFLAGS="$(CXXFLAGS) -pg" all

$(OBJECTS): $(SOURCES) $(HEADERS)
	$(CXX) -c -o $@ src/$(*F).cpp $(INCLUDES) $(CXXFLAGS)

$(SHORTBINS):
	$(MAKE) $(BIN_DIR)/$@

$(BINS): $(BIN_SOURCES) $(LIB) $(OBJECTS) $(SSW) pre
	$(CXX) src/$(notdir $@).cpp -o $@ $(INCLUDES) $(CXXFLAGS) $(LDFLAGS)

$(LIB): $(OBJECTS) $(SSW)
	ar rs $(LIB) $(OBJECTS) $(SSW)

$(SLIB): $(OBJECTS) $(SSW)
	$(CXX) -shared -Wl,-soname,$(SLIB) -o $(SLIB) $(OBJECTS) $(SSW)

install: all
	$(MKDIR) $(DESTDIR)$(PREFIX)/bin
	$(MKDIR) $(DESTDIR)$(PREFIX)/include/vcflib
	$(MKDIR) $(DESTDIR)$(PREFIX)/lib
	$(INSTALL) bin/* $(DESTDIR)$(PREFIX)/bin
	$(INSTALL) src/*.h src/*.hpp $(DESTDIR)$(PREFIX)/include/vcflib
	$(INSTALL) $(LIB) $(SLIB) $(DESTDIR)$(PREFIX)/lib

install-strip: install
	$(STRIP) $(DESTDIR)$(PREFIX)/bin/* $(DESTDIR)$(PREFIX)/lib/$(SLIB)

test: $(BINS)
	@prove -Itests/lib -w tests/*.t

clean:
	rm -f $(BINS) $(OBJECTS)
	rm -f ssw_cpp.o ssw.o
	rm -f $(LIB)
	rm -rf $(BIN_DIR)

pre:
	mkdir -p $(BIN_DIR)

.PHONY: clean all test pre
