#!/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) and TypeScript tests. This package doesn't
# support them.
# 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='.*CsCompleter.*' --exclude='.*TypeScript.*' --exclude='.*UnknownExtraConfException.*'

# 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
	# Some tests only works if system language is UTF-8 based
	LC_ALL=C.UTF-8 ./run_tests.sh --skip-build -- --no-byte-compile $(EXCLUDE_TEST_PARAMS)
endif

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -rfv *.so
	$(RM) -rfv $(BUILD_DIR)

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
	# No need to install 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

get-orig-source:
	TMPDIR=`mktemp -d -t`; \
	git clone --recursive $(GIT_URL) $$TMPDIR; \
	cd $$TMPDIR; git checkout $(DEB_UPSTREAM_COMMIT); \
	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
