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

BUILD_DIR            = $(CURDIR)/ycm_build
GIT_URL              = https://github.com/Valloric/YouCompleteMe.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)


%:
	dh $@ --with python2 --sourcedirectory=$(CURDIR)/cpp --builddirectory=$(BUILD_DIR) --parallel

override_dh_install:
	dh_install
	# This file is an executable and it shouldn't be. It's a upstream bug
	chmod a-x $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/completers/cs/cs_completer.py
	# No need to install tests
	$(RM) -rv $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/completers/all/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/completers/completer_utils_test.py \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/completers/cpp/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/completers/general/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/server/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/tests \
	          $(DEB_INSTALL_DIR)/usr/lib/vim-youcompleteme/ycm/test_utils.py

get-orig-source:
	TMPDIR=`mktemp -d -t`; \
	git clone --recursive $(GIT_URL) $$TMPDIR; \
	cd $$TMPDIR; git checkout $(DEB_UPSTREAM_COMMIT); \
	tar --exclude=.git \
	    --exclude=.gitignore \
	    --exclude=.gitmodules \
	    --exclude=.travis.yml \
	    --exclude=cpp/BoostParts \
	    --exclude=cpp/llvm \
	    --exclude=cpp/ycm/tests/gmock \
	    --exclude=python/clang_includes \
	    --exclude=python/ycm/completers/cs/OmniSharpServer \
	    --exclude=third_party/argparse \
	    --exclude=third_party/bottle \
	    --exclude=third_party/jedi \
	    --exclude=third_party/pythonfutures \
	    --exclude=third_party/requests \
	    --exclude=third_party/waitress \
	    -czvf $(CURDIR)/$(DEB_SOURCE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz .; \
	cd $(CURDIR); \
	$(RM) -r $$TMPDIR
