#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

UPSTREAM_GIT    ?= https://bitbucket.org/jimplank/gf-complete.git

DEBVERS         ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION         ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//')
DEBFLAVOR       ?= $(shell dpkg-parsechangelog | grep -E ^Distribution: | cut -d" " -f2)
DEBPKGNAME      ?= $(shell dpkg-parsechangelog | grep -E ^Source: | cut -d" " -f2)
GIT_TAG         ?= $(shell echo '$(VERSION)' | sed -e 's/~/_/')

%:
	dh $@  --with autoreconf --parallel

override_dh_autoreconf:
	echo "Please don't do anything!"

override_dh_auto_configure:
	echo "Please don't do anything!"

override_dh_auto_install:
ifeq ($(DEB_HOST_ARCH_CPU),amd64)
	set -e ; set -x ; if ! [ -x debian/check_sse ] ; then \
		gcc debian/check_sse.c -o debian/check_sse ; \
	fi
	set -e ; set -x ; if ./debian/check_sse ; then \
		echo "No SSE support here..." ; \
	else \
		dh_autoreconf ; \
		dh_auto_configure ; \
		dh_auto_build ; \
		dh_auto_install ; \
		dh_autoreconf_clean ; \
		mv debian/tmp debian/tmp-with-sse ; \
	fi
endif
	patch -p1 <debian/do-not-use-sse-ac-ext.patch
	dh_autoreconf
	dh_auto_configure
	dh_auto_build
	dh_auto_install
	dh_autoreconf_clean
	patch -p1 -R <debian/do-not-use-sse-ac-ext.patch

override_dh_auto_clean:
	dh_auto_clean      
	rm -rf debian/tmp-with-sse debian/check_sse

override_dh_install:
	dh_install
ifeq ($(DEB_HOST_ARCH_CPU),amd64)
	set -e ; set -x ; if ./debian/check_sse ; then \
		echo "No SSE support here..." ; \
	else \
		mkdir -p $(CURDIR)/debian/libgf-complete1-sse/usr/lib ; \
		mv $(CURDIR)/debian/tmp-with-sse/usr/lib/* $(CURDIR)/debian/libgf-complete1-sse/usr/lib ; \
		rm $(CURDIR)/debian/libgf-complete1-sse/usr/lib/*/*.so ; \
		rm $(CURDIR)/debian/libgf-complete1-sse/usr/lib/*/*.a ; \
		rm $(CURDIR)/debian/libgf-complete1-sse/usr/lib/*/*.la ; \
	fi
endif

	# NOTE(jamespage): Don't ship example binaries, just the actual tools
	rm -rf $(CURDIR)/debian/gf-complete-tools/usr/bin/gf_example*

gen-orig-xz:
	git tag -v $(GIT_TAG) || true
	if [ ! -f ../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ] ; then \
		git archive --prefix=$(DEBPKGNAME)-$(VERSION)/ $(GIT_TAG) | xz >../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ; \
	fi
	[ ! -e ../build-area ] && mkdir ../build-area || true
	[ ! -e ../build-area/$(DEBPKGNAME)_$(VERSION).orig.tar.xz ] && cp ../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ../build-area

fetch-upstream-remote:
	git remote add upstream $(UPSTREAM_GIT) || true
	git fetch upstream
