# ===========================================================================
#
#                            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.
#
# ===========================================================================

default: install

TOP ?= $(CURDIR)
MODPATH =

include $(TOP)/Makefile.config

#-------------------------------------------------------------------------------
# install
# 
LIBRARIES_TO_INSTALL = \
    ngs-sdk.$(VERSION_SHLX) \
    ngs-c++.$(VERSION_LIBX) \
    ngs-adapt-c++.$(VERSION_LIBX) \

LIB_TARGET = $(INST_LIBDIR)$(BITS)

LIBRARIES_WITH_PREFIX = \
	$(addprefix $(LIB_TARGET)/$(LPFX),$(LIBRARIES_TO_INSTALL))

copylibs: $(LIB_TARGET) 
	@ echo "Installing libraries to $(LIB_TARGET)"
	@ $(MAKE) -f Makefile.install $(LIBRARIES_WITH_PREFIX)

copyincludes:
	@ echo "Installing includes to $(INST_INCDIR)"
	@ mkdir -p $(INST_INCDIR)
	@ cp -r $(TOP)/ngs $(INST_INCDIR)/   

copyexamples:
	@ echo "Installing examples to $(INST_SHAREDIR)"
	@ mkdir -p $(INST_SHAREDIR)/examples
	@ $(MAKE) -C $(TOP)/examples install INST_TARGET=$(INST_SHAREDIR)/examples

# unset outside defined variables
ROOT =
LINUX_ROOT =

#fake root for debugging
#uncomment this line and change the test for root ( see under install: ) to succeed:
#ROOT = ~/root

INCLUDE_SYMLINK = $(ROOT)/usr/include/ngs
PROFILE_FILE = $(ROOT)/etc/profile.d/ngs-sdk

ifeq (linux, $(OS))
    ifeq (0, $(shell id -u))
        LINUX_ROOT = true
    endif
endif

install: copylibs copyincludes copyexamples
ifeq (true, $(LINUX_ROOT))
	@ # install symlink to includes 
	@ echo "Updating $(INCLUDE_SYMLINK)"
	@ rm -f $(INCLUDE_SYMLINK)
	@ ln -s $(INST_INCDIR)/ngs $(INCLUDE_SYMLINK)
	@ #
	@ echo "Updating $(PROFILE_FILE).[c]sh"
	@ printf \
"#version $(VERSION)\n"\
"if ! echo \$$LD_LIBRARY_PATH | /bin/grep -q $(LIB_TARGET)\n"\
"then export LD_LIBRARY_PATH=$(LIB_TARGET):\$$LD_LIBRARY_PATH\n"\
"fi\n"\
"export NGS_LIBDIR=$(LIB_TARGET)\n" \
        >$(PROFILE_FILE).sh && chmod 644 $(PROFILE_FILE).sh || true;
	@ printf \
"#version $(VERSION)\n"\
"echo \$$LD_LIBRARY_PATH | /bin/grep -q $(LIB_TARGET)\n"\
"if ( \$$status ) setenv LD_LIBRARY_PATH $(LIB_TARGET):\$$LD_LIBRARY_PATH\n"\
"setenv NGS_LIBDIR $(LIB_TARGET)\n" \
        >$(PROFILE_FILE).csh && chmod 644 $(PROFILE_FILE).csh || true;
	@ #TODO: check version of the files above
	@ #
	@ echo "Use \$$NGS_LIBDIR in your link commands, e.g.:"
	@ echo "      ld -L\$$NGS_LIBDIR -lngs-sdk ..."
else
	@ #
	@ echo "Please add $(LIB_TARGET) to your LD_LIBRARY_PATH, e.g.:"
	@ echo "      export LD_LIBRARY_PATH=$(LIB_TARGET):\$$LD_LIBRARY_PATH"   
	@ #
	@ echo "Use $(LIB_TARGET) in your link commands, e.g.:"
	@ echo "      export NGS_LIBDIR=$(LIB_TARGET)"   
	@ echo "      ld -L\$$NGS_LIBDIR -lngs-sdk ..."
endif

TO_UNINSTALL = $(LIB_TARGET)/libngs-* $(INST_INCDIR)/ngs $(INST_SHAREDIR)/examples
TO_UNINSTALL_AS_ROOT = $(INST_INCDIR)/ngs $(INCLUDE_SYMLINK) $(PROFILE_FILE).*

uninstall:
	@ echo "Uninstalling $(TO_UNINSTALL) ..."
	@ rm -rf $(TO_UNINSTALL)
ifeq (true, $(LINUX_ROOT))
	@ echo "Uninstalling $(TO_UNINSTALL_AS_ROOT) ..."
	@ rm -rf $(TO_UNINSTALL_AS_ROOT)
endif
	@ echo "done."

.PHONY: install copylibs copyincludes copyexamples uninstall

