#!/usr/bin/make -f
# -*- makefile -*-

BUILD_DIR            = $(CURDIR)/ycm_build
GIT_URL              = https://github.com/Valloric/ycmd.git
DEB_SOURCE           = $(shell dpkg-parsechangelog | grep Source: | sed -e 's/Source: //')
DEB_VERSION          = $(shell dpkg-parsechangelog | grep Version: | sed -e 's/Version: //')
DEB_UPSTREAM_COMMIT  = $(shell echo $(DEB_VERSION) | sed -rn 's/.*git([^-]*).*/\1/p')
DEB_UPSTREAM_VERSION = $(shell echo $(DEB_VERSION) | sed -e 's/-[^-]*$$//')
DEB_INSTALL_DIR      = $(CURDIR)/debian/$(DEB_SOURCE)


# Skip CsCompleter (OmniSharp), javascript (tern) and rust tests.
# This package doesn't support them yet.
# We are also skipping UnknownExtraConfException test.
# If ycmd package is installed in your system or
# /usr/lib/ycmd/ycm_extra_conf.py is present, UnknownExtraConfException is
# failing due to global_ycm_extra_conf defined in
# 02-generic-ycm-extra-conf-py.patch
EXCLUDE_TEST_PARAMS  = --exclude='^cs$$' --exclude='UnknownExtraConfException' --exclude='javascript' --exclude='rust' --exclude='go'

# This package is providing extra golang completion feature if golang and
# gocode packages are installed. *GoCodeCompleter* tests also require golang
# and gocode packages. gocode and golang is not available in every
# architecture. Skip GoCodeCompleter tests if DEB_BUILD_ARCH is not supported
# by golang
GO_DEP_ARCH          = $(shell dpkg-architecture -qDEB_BUILD_ARCH | egrep -x "(amd64|armel|armhf|i386)" > /dev/null; echo $$?)
ifneq ($(GO_DEP_ARCH),0)
  EXCLUDE_TEST_PARAMS += --exclude='.*GoCodeCompleter.*'
endif

%:
	dh $@ --with python2 --sourcedirectory=$(CURDIR)/cpp --builddirectory=$(BUILD_DIR) --parallel
      
override_dh_auto_configure:
	dh_auto_configure -- -DUSE_SYSTEM_LIBCLANG=ON -DUSE_SYSTEM_BOOST=ON -DUSE_SYSTEM_GMOCK=ON

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	cd $(BUILD_DIR)/ycm/tests && ./ycm_core_tests
	# Test JediHTTP
	cd third_party/JediHTTP && HOME=$(CURDIR) nosetests -v --with-id -d --no-byte-compile
	cd third_party/JediHTTP && HOME=$(CURDIR) nosetests3 -v --with-id -d --no-byte-compile
	# Some tests requires UTF-8 compatible locale
	HOME=$(CURDIR) LC_ALL=C.UTF-8 ./run_tests.py --skip-build --no-byte-compile $(EXCLUDE_TEST_PARAMS)
endif

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -rfv .noseids third_party/JediHTTP/.noseids .cache
	$(RM) -rfv *.so*
	$(RM) -rfv $(BUILD_DIR)
	find third_party/JediHTTP -name '*.pyc' -exec $(RM) -v {} \;

override_dh_install:
	dh_install
	# Remove dot from .ycm_extra_conf.py
	mv $(DEB_INSTALL_DIR)/usr/lib/ycmd/.ycm_extra_conf.py \
	   $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycm_extra_conf.py
	# Remove tests
	$(RM) -rv $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/all/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/completer_utils_test.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/cpp/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/general/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/go/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/third_party/JediHTTP/jedihttp/tests
	# Fix permissions
	chmod 644 $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/typescript/typescript_completer.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/all/identifier_completer.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/cpp/clang_completer.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/check_core_version.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/go/go_completer.py \
	          $(DEB_INSTALL_DIR)/usr/lib/ycmd/ycmd/completers/cs/cs_completer.py

override_dh_link:
	$(eval YCMD_CLANG_VER=$(shell objdump -x ycm_core.so | grep -oP 'libclang-\K(\d+\.\d+)'))
	echo "misc:Clang-Ver=$(YCMD_CLANG_VER)" >> debian/ycmd.substvars
	dh_link -pycmd usr/lib/clang/$(YCMD_CLANG_VER)/include usr/lib/ycmd/clang_includes

get-orig-source:
	TMPDIR=`mktemp -d -t`; \
	git clone $(GIT_URL) $$TMPDIR; \
	cd $$TMPDIR; \
	git checkout $(DEB_UPSTREAM_COMMIT); \
	git submodule update --init third_party/frozendict; \
	git submodule update --init third_party/JediHTTP; \
	tar -czvf $(CURDIR)/ycmd.orig.tar.gz .; \
	cd $(CURDIR); \
	dpt repack.sh --upstream-version $(DEB_UPSTREAM_VERSION) ycmd.orig.tar.gz ; \
	$(RM) ycmd.orig.tar.gz ; \
	$(RM) -r $$TMPDIR
