# ===================e=======================================================
#
#                            PUBLIC DOMAIN NOTICE
#               National Center for Biotechnology Information
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government have not placed any restriction on its use or reproduction.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
#  Please cite the author in any work or product based on this material.
#
# ===========================================================================

TOP ?= $(abspath ..)

# All of this section is to provide semi-automatic detection of
# required libraries and headers. They SHOULD have been built and possibly installed
# already before building these examples.
#
# In order to build without installing, specify locations of required libraries and headers
# through the command line, e.g.:
#
# NCBI_VDB_INCDIR=... \				path to ncbi-vdb/interfaces/ngs/ncbi/NGS.hpp
# NCBI_VDB_LIBDIR=... \				path to libncbi-vdb-static.a
# NGS_INCDIR=... \					path to ngs-sdk/ngs/*.hpp
# NGS_LIBDIR=... \					path to libngs-c++.a
# make

ifeq (,$(wildcard $(TOP)/Makefile.config))
	# Apparently running in the installed location.
	# This is TBD
else
	# Apparently running in a checkout of ngs source code. Assume ncbi-vdb is checked
	# out side by-side with it

	include $(TOP)/Makefile.config

	NCBI_VDB_LIBDIR ?= $(dir $(firstword $(shell find $(subst :, ,$(LD_LIBRARY_PATH) -maxdepth 1 -name libncbi-vdb-static.a) 2>/dev/null) ) )
	NCBI_VDB_INCDIR ?= $(abspath $(dir $(shell find $(CURDIR)/../../../ncbi-vdb/interfaces/ngs/ncbi -maxdepth 1 -name NGS.hpp 2>/dev/null)))
	NGS_LIBDIR 		?= $(dir $(firstword $(shell find $(subst :, ,$(LD_LIBRARY_PATH) $(LIBDIR) -maxdepth 1 -name libngs-c++.a) 2>/dev/null) ) )
	NGS_INCDIR      ?= $(TOP)

endif

$(info NCBI_VDB_LIBDIR=$(NCBI_VDB_LIBDIR))
$(info NCBI_VDB_INCDIR=$(NCBI_VDB_INCDIR))
$(info NGS_LIBDIR=$(NGS_LIBDIR))
$(info NGS_INCDIR=$(NGS_INCDIR))

ifeq (,$(NCBI_VDB_INCDIR))

ncbi-headers:
	@ echo "NCBI_VDB headers are not found (looking in $(NCBI_VDB_INCDIR)"
	@ echo "please specify the location as NCBI_VDB_INCDIR"
	@ false

else

CPPFLAGS += -I$(NCBI_VDB_INCDIR)
ncbi-headers:
	@ true

endif

ifeq (,$(NGS_INCDIR))

ngs-headers:
	@ echo "NGS headers are not found (looking in $(NGS_INCDIR))"
	@ echo "please specify the location as NGS_INCDIR"
	@ false

else

CPPFLAGS += -I$(NGS_INCDIR)
ngs-headers:
	@ true

endif

ifeq (,$(wildcard $(NCBI_VDB_LIBDIR)/libncbi-vdb-static.a))

$(NCBI_VDB_LIBDIR)/libncbi-vdb-static.a:
	@ echo "These example applications require $(@F). Please install ncbi-vdb libraries and add their location to LD_LIBRARY_PATH."
	@ echo "LD_LIBRARY_PATH=$${LD_LIBRARY_PATH}"
	@ false

else

$(NCBI_VDB_LIBDIR)/libncbi-vdb-static.a:
	@ true

endif

ifeq (,$(wildcard $(NGS_LIBDIR)/libngs-c++.a))

$(NGS_LIBDIR)/libngs-c++.a:
	@ echo "These example applications require $(@F). Please install ngs libraries and add their location to LD_LIBRARY_PATH."
	@ echo "LD_LIBRARY_PATH=$${LD_LIBRARY_PATH} $(NGS_LIBDIR)/libngs-c++.a"
	@ false

else

$(NGS_LIBDIR)/libngs-c++.a:
	@ true

endif

.PHONY: ncbi-headers ngs-headers
