#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

SHLIBVER = 2.0.9

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE2=yes
else
   DEBVE2=no
endif

confflags = --disable-rpath --enable-sdl-dlopen \
            --disable-nas --disable-esd --disable-arts \
            --disable-alsa-shared --disable-pulseaudio-shared \
            --enable-ibus \
            --disable-x11-shared --disable-video-directfb \
            --enable-video-opengles \
            --disable-video-opengles1 \
            --enable-video-wayland --disable-wayland-shared \
            --enable-hidapi

# disable autoheader (invoked automatically by autoreconf), necessary in order
# to use debhelper compat level v10 without overriding dh-autoreconf calls
export AUTOHEADER := /bin/true


ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
  confflags += --disable-altivec
endif

ifeq ($(DEB_HOST_ARCH_CPU),ppc64el)
  confflags += --disable-altivec
endif

# disable Wayland and Vulkan on non-Linux, they do not support other kernels at the moment
ifeq (hurd,$(findstring hurd,$(DEB_HOST_ARCH_CPU)))
  confflags += --disable-video-vulkan
  confflags += --disable-video-wayland
endif
ifeq (kfreefsd,$(findstring kfreebsd,$(DEB_HOST_ARCH_CPU)))
  confflags += --disable-video-vulkan
  confflags += --disable-video-wayland
endif
ifeq (no,$(findstring no,$(DEBVE2)))
  confflags += --disable-video-vulkan
  confflags += --disable-video-wayland
endif

# disable OpenGLES on Hurd, it does not support it at the moment
ifeq (hurd,$(findstring hurd,$(DEB_HOST_ARCH_CPU)))
  confflags += --disable--video-opengles
endif

# don't use libunwind even if it happens to be installed
confflags += ac_cv_header_libunwind_h=no

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- $(confflags)

override_dh_auto_build-indep:
	GZIP="-9n" tar czf debian/examples.tar.gz test --owner=0 --group=0 --mode=go=rX,u+rw,a-s
	doxygen docs/doxyfile
	# useless files
	find output -name "*.md5" -delete
	find output -type d -empty -delete
#
#	dh_link -plibsdl2-doc usr/share/javascript/jquery/jquery.js usr/share/doc/libsdl2-doc/html/jquery.js

## Force examples to be installed in libsdl2-doc, it does not happen with compat
## level v11 despite having the file debian/libsdl2-doc.examples (it gets
## installed as part of libsdl2-dev instead)
#override_dh_installexamples-indep:
#	dh_installexamples -i --doc-main-package=libsdl2-doc

override_dh_auto_build-arch:
	dh_auto_build -- V=1

override_dh_auto_clean-indep:
	dh_auto_clean
	rm -f debian/examples.tar.gz
	rm -rf output

override_dh_missing:
	dh_missing --fail-missing -XlibSDL2.la -XlibSDL2main.la -XlibSDL2_test.la

override_dh_link:
	# to address lintian warning
	# W: libsdl2-2.0-0: dev-pkg-without-shlib-symlink usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.0.0 usr/lib/x86_64-linux-gnu/libSDL2-2.0.so
	dh_link -plibsdl2-dev usr/lib/$(DEB_HOST_MULTIARCH)/libSDL2-2.0.so.0 usr/lib/$(DEB_HOST_MULTIARCH)/libSDL2-2.0.so
	dh_link --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -V"libsdl2-2.0-0 (>= $(SHLIBVER))"

#override_dh_strip:
#	dh_strip --dbgsym-migration='libsdl2-dbg (<< 2.0.4+dfsg2-1~)'

override_dh_installchangelogs:
	dh_installchangelogs -- WhatsNew.txt
