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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1


# Path to package source directory 
PKGDIR := $(dir $(firstword $(MAKEFILE_LIST)))..
# Get version numbers for SONAME from changelog
UPVER := $(shell cd $(PKGDIR) && dpkg-parsechangelog -SVersion | \
		sed -rne 's/([0-9]+\.[0-9]+).*/\1/p')
export UPVER

export DEB_CFLAGS_MAINT_APPEND += -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
ifeq (amd64,$(DEB_HOST_ARCH))
    DEB_CFLAGS_MAINT_APPEND += -msse
endif
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@ --with=python2,python3


override_dh_auto_install:
	dh_auto_install -- libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/


override_dh_compress:
	dh_compress -Xexamples


override_dh_strip:
	dh_strip --dbg-package=liblinear-dbg


get-orig-source:
	test -x /usr/bin/unzip || { echo "Package 'unzip' required"; exit 1; }
	
	uscan \
		--noconf \
		--no-symlink \
		--force-download \
		--download-version $(UPVER) \
		--check-dirname-level=0 \
		--destdir=$(CURDIR) \
		$(PKGDIR)
	
	tar -xzf liblinear-$(UPVER).tar.gz
	rm -f liblinear-$(UPVER).tar.gz
	
	# Remove Windows binaries
	rm -rf liblinear-$(UPVER)/windows/
	
	# Remove BLAS implementation with unknown license
	rm -rf liblinear-$(UPVER)/blas/
	# matlab Makefile
	sed -i -e '/^\.\.\/blas\/blas\.a:.*/,+2d' liblinear-$(UPVER)/matlab/Makefile
	sed -i -e 's| ../blas/blas.a||' liblinear-$(UPVER)/matlab/Makefile
	sed -i -e '/blas/d' liblinear-$(UPVER)/matlab/Makefile
	# main Makefile
	sed -i -e '/^blas\/blas\.a:.*/,+2d' liblinear-$(UPVER)/Makefile
	sed -i -e 's| blas/blas.a||' liblinear-$(UPVER)/Makefile
	sed -i -e '/blas/d' liblinear-$(UPVER)/Makefile
	
	mv liblinear-$(UPVER) liblinear-$(UPVER)+dfsg
	tar \
		--owner=root \
		--group=root \
		--mode=a+rX \
		--xz \
		-cf liblinear_$(UPVER)+dfsg.orig.tar.xz \
		liblinear-$(UPVER)+dfsg
	
	rm -rf liblinear-$(UPVER)+dfsg
