#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# disable lto, otherwise PPSSPPQt will crash on 25.04
export DEB_BUILD_MAINT_OPTIONS=optimize=-lto

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed

export DEB_BUILD_OPTIONS=nocheck

export QT_SELECT := qt5

ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(ARCH), armhf)
    GLES_ENABLE = -DUSING_GLES2=ON -DUSING_EGL=ON
    # See issue #7817
    #export CC=clang
    #export CXX=clang++
else ifeq ($(ARCH), arm64)
    GLES_ENABLE = -DUSING_GLES2=ON -DUSING_EGL=ON
endif

COMMON_FLAGS = -DUSE_FFMPEG=ON -DUSE_SYSTEM_FFMPEG=OFF -DUSE_SYSTEM_LIBZIP=ON -DUSE_DISCORD=ON -DUSE_WAYLAND_WSI=ON


checkver = $(shell dpkg --compare-versions "$$(dpkg-query --show --showformat '$${Version}' '$(1)')" ge '$(2)' && echo yes || echo no)

ifeq (yes, $(call checkver,libsnappy-dev,1.1.8))
    COMMON_FLAGS += -DUSE_SYSTEM_SNAPPY=ON
endif
#ifeq (yes, $(call checkver,libzip-dev,1.7.3))
#    COMMON_FLAGS += -DUSE_SYSTEM_LIBZIP=ON
#endif
ifeq (yes, $(call checkver,libzstd-dev,1.5.6))
    COMMON_FLAGS += -DUSE_SYSTEM_ZSTD=ON
endif
ifeq (yes, $(call checkver,libminiupnpc-dev,2.2.7))
    COMMON_FLAGS += -DUSE_SYSTEM_MINIUPNPC=ON
endif

%:
	dh $@ --buildsystem=cmake --parallel


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_auto_configure:
ifeq ($(ARCH), i386)
	cd ffmpeg && ./linux_x86.sh
else ifeq ($(ARCH), amd64)
	cd ffmpeg && ./linux_x86-64.sh
else ifeq ($(ARCH), armhf)
	cd ffmpeg && ./linux_armhf.sh
else ifeq ($(ARCH), arm64)
	cd ffmpeg && ./linux_arm64.sh
endif
	dh_auto_configure $@ --builddirectory=obj_qt -- $(GLES_ENABLE) $(COMMON_FLAGS) -DUSING_QT_UI=ON
	dh_auto_configure $@ --builddirectory=obj_sdl -- $(GLES_ENABLE) $(COMMON_FLAGS)
	dh_auto_configure $@ --builddirectory=obj_libretro -- $(GLES_ENABLE) $(COMMON_FLAGS) -DLIBRETRO=ON

override_dh_auto_build:
	dh_auto_build $@ --builddirectory=obj_qt
	dh_auto_build $@ --builddirectory=obj_sdl
	dh_auto_build $@ --builddirectory=obj_libretro

override_dh_auto_install:
	dh_auto_install $@ --builddirectory=obj_qt
	dh_auto_install $@ --builddirectory=obj_sdl
#	dh_auto_install $@ --builddirectory=obj_libretro

#override_dh_auto_test:
#	dh_auto_test $@ --builddirectory=obj_qt
#	dh_auto_test $@ --builddirectory=obj_sdl
#	dh_auto_test $@ --builddirectory=obj_libretro

override_dh_auto_clean:
	dh_auto_clean $@ --builddirectory=obj_qt
	dh_auto_clean $@ --builddirectory=obj_sdl
	dh_auto_clean $@ --builddirectory=obj_libretro

#override_dh_strip:
#	dh_strip -pppsspp --dbg-package=ppsspp-dbg
#	dh_strip -pppsspp-sdl --dbg-package=ppsspp-sdl-dbg
#	dh_strip -pppsspp-libretro --dbg-package=ppsspp-libretro-dbg