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

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# This has to be exported to make some magic below work.
export DH_OPTIONS

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

build3vers := $(shell py3versions -sv)

# Build
build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	NO_INTERNET=yes $(MAKE) -C src
	NO_INTERNET=yes $(MAKE) -C src pymod
	set -e && for i in $(build3vers); do \
	  NO_INTERNET=yes $(MAKE) -C src pymod PYTHON=python$$i; \
	done
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -rf src/pymod/build/
	$(MAKE) -C src clean
	dh_clean 

install:
	dh_testdir
	dh_testroot
	dh_clean -k -s 
	dh_installdirs -s

	# Install to debian/tmp
	NO_INTERNET=yes $(MAKE) -C src DESTDIR=$(CURDIR)/debian/tmp install
	NO_INTERNET=yes $(MAKE) -C src DESTDIR=$(CURDIR)/debian/tmp install-pymod EXTRA_ARGS=--install-layout=deb
	set -e && for i in $(build3vers); do \
	  NO_INTERNET=yes $(MAKE) -C src DESTDIR=$(CURDIR)/debian/tmp install-pymod EXTRA_ARGS=--install-layout=deb PYTHON=python$$i; \
	done

	dh_install -s --autodest
	dh_python2 -s
	dh_python3 -s

# Must not depend on anything. This is to be called by
# binary in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a -XREADME.win32
	dh_installexamples -plibpacparser1 -X*.py
	dh_installexamples -ppython-pacparser -X*.c
	dh_installexamples -ppython3-pacparser -X*.c
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -X_pacparser.so
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-arch: binary
binary-indep: binary
binary: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

.PHONY: build build-indep build-arch install binary binary-common binary-indep binary-arch clean
