#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)
LDFLAGS += -Wl,-z,defs -Wl,-O1 -Wl,--as-needed `$*-config --libs`

CFLAGS += -Wno-error
CXXFLAGS += -Wno-error

PY_DEFAULT := $(shell pyversions --default)
PY_VERSIONS = $(sort $(shell pyversions --requested) $(PY_DEFAULT))

include /usr/share/pycentral-data/pycentral.mk

configure_flags := --prefix=/usr

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	configure_flags += --build=$(DEB_BUILD_GNU_TYPE)
else
	configure_flags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

patch: patch-stamp
patch-stamp:
	dpatch apply-all
	dpatch cat-all >patch-stamp

unpatch:
	dpatch deapply-all
	rm -rf patch-stamp debian/patched

build-stamp-%: patch
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean
	PYTHON=`which $*` PKG_CONFIG_PATH=/usr/lib/pkgconfig/$* CFLAGS="$(CFLAGS)" \
	LDFLAGS="$(LDFLAGS)" ./configure $(configure_flags)
	$(MAKE)
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp \
		PYEXECDIR=/usr/lib/$*/$(call sitedir,$*) \
		PYLIBDIR=/usr/lib/$*/$(call sitedir,$*) \
		pyexecdir=/usr/lib/$*/$(call sitedir,$*) \
		pylibdir=/usr/lib/$*/$(call sitedir,$*)
	touch $@

build-arch: $(addprefix build-stamp-, $(PY_VERSIONS))

build: build-arch

clean: unpatch
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build-stamp-* */*.pyc *.pyc
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_install
	# drop all *.la files
	find debian/* -name '*.la' -print0 | xargs -0r rm -f --

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_link -s
	dh_strip -s
	dh_compress -s -Xexample
	dh_fixperms -s
	DH_PYCENTRAL=include-links dh_pycentral -s
	dh_makeshlibs -V -- -c4
	mkdir -p $(CURDIR)/fake-home
	HOME=$(CURDIR)/fake-home \
	  dh_girepository -pgir1.2-gst-rtsp-server-0.10
	rm -rf $(CURDIR)/fake-home
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary-indep:

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