#!/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

get-orig-source:
	TMPDIR=`mktemp -d -t`; \
	git clone $(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 \
	    -czvf $(CURDIR)/$(DEB_SOURCE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz .; \
	cd $(CURDIR); \
	$(RM) -r $$TMPDIR
