#!/usr/bin/make -f

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

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-video-kmsdrm --disable-kmsdrm-shared \
            --enable-hidapi

# SteamRT: Disable obscure audio APIs
confflags += --disable-sndio

# SteamRT: Disable kmsdrm, which requires the host libgbm to be >= the
# version libsdl2 was built with, and is not interesting in an X11/Wayland
# environment anyway
confflags += --disable-video-kmsdrm

# 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

# 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


%:
	dh $@

override_dh_auto_configure:
	sed -e "s/@DEB_VERSION_UPSTREAM@/$(DEB_VERSION_UPSTREAM)/" \
		< debian/libsdl2-2.0-0.symbols.in \
		> debian/libsdl2-2.0-0.symbols
	dh_auto_configure -- $(confflags)
	# test/configure.ac 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.
	dh_auto_configure \
		--buildsystem=autoconf \
		--sourcedirectory=$(CURDIR)/test \
		--builddirectory=$(CURDIR)/debian/build-tests \
		-- \
		--x-includes=/usr/include \
		--x-libraries=/usr/lib/$(DEB_HOST_MULTIARCH) \
		SDL_CFLAGS=-I$(CURDIR)/include \
		SDL_LIBS="-L$(CURDIR)/build/.libs -lSDL2" \
		ac_cv_lib_SDL2_ttf_TTF_Init=no \
		$(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_clean-indep:
	dh_auto_clean
	rm -f debian/examples.tar.gz
	rm -rf output

override_dh_auto_install-arch:
	dh_auto_install -- V=1
	dh_auto_install \
		--buildsystem=autoconf \
		--sourcedirectory=$(CURDIR)/test \
		--builddirectory=$(CURDIR)/debian/build-tests \
		-- \
		installedtestsdir='/usr/lib/$(DEB_HOST_MULTIARCH)/installed-tests/SDL2' \
		V=1

override_dh_install:
	mkdir -p debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/SDL2
	mv debian/tmp/usr/include/SDL2/SDL_config.h debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/SDL2/_real_SDL_config.h
	ln -s ../../SDL2/SDL_platform.h debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/SDL2/
	ln -s ../../SDL2/begin_code.h debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/SDL2/
	ln -s ../../SDL2/close_code.h debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/SDL2/
	dh_install

override_dh_missing:
	dh_missing --fail-missing

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_installchangelogs:
	dh_installchangelogs -- WhatsNew.txt
