#!/usr/bin/make -f

export DEB_LDFLAGS_MAINT_APPEND := -lm
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

%:
	dh $@

CONFIGURE_FLAGS ?=--disable-mmx

ifeq ($(DEB_HOST_ARCH), i386)
	CONFIGURE_FLAGS =--enable-mmx
endif
ifeq ($(DEB_HOST_ARCH), amd64)
	CONFIGURE_FLAGS =--enable-mmx
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf Docs/html

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE_FLAGS)
	# test/configure.in uses AC_PATH_X, so we need to pass in
	# --x-includes and --x-libraries to avoid it wanting to use
	# xmkmf to discover the right values.
	# test/Makefile.in doesn't respect CPPFLAGS or LDFLAGS.
	dh_auto_configure \
		--buildsystem=autoconf \
		--sourcedirectory=$(CURDIR)/test \
		--builddirectory=$(CURDIR)/debian/build-tests \
		-- \
		--x-includes=/usr/include \
		--x-libraries=/usr/lib/$(DEB_HOST_MULTIARCH) \
		CFLAGS="$(CPPFLAGS) $(CFLAGS) -I$(CURDIR) -L$(CURDIR)/.libs $(LDFLAGS)" \
		ac_cv_lib_SDL2_gfx_pixelColor_=yes \
		$(NULL)

override_dh_auto_build-arch:
	dh_auto_build -- V=1
	dh_auto_build \
		--buildsystem=autoconf \
		--sourcedirectory=$(CURDIR)/test \
		--builddirectory=$(CURDIR)/debian/build-tests \
		-- \
		V=1

override_dh_auto_build-indep:
	rm -fr test/autom4te.cache
	rm -f test/config.guess~
	rm -f test/config.sub~
	rm -f test/configure~
	tar -cvz --transform='s,^test,examples,' -f debian/examples.tar.gz --clamp-mtime --mtime="@$(SOURCE_DATE_EPOCH)" --sort=name --mode=go=rX,u+rw,a-s --owner=root --group=root --numeric-owner test
	(cd Docs && doxygen html.doxyfile)

override_dh_auto_install-arch:
	dh_auto_install -- V=1
	rm -f debian/tmp/usr/lib/*/libSDL2_gfx.la
	dh_auto_install \
		--buildsystem=autoconf \
		--sourcedirectory=$(CURDIR)/test \
		--builddirectory=$(CURDIR)/debian/build-tests \
		-- \
		installedtestsdir='/usr/lib/$(DEB_HOST_MULTIARCH)/installed-tests/SDL2_gfx' \
		V=1
override_dh_auto_install-indep:
	:

override_dh_installdocs-indep:
	dh_installdocs --doc-main-package=libsdl2-gfx-doc -plibsdl2-gfx-doc

override_dh_installdocs-arch:
	dh_installdocs --doc-main-package=libsdl2-gfx-doc -plibsdl2-gfx-dev
	dh_installdocs --doc-main-package=libsdl2-gfx-doc -plibsdl2-gfx-1.0-0

override_dh_missing:
	dh_missing --fail-missing
