#!/usr/bin/make -f

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

export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS=optimize=-lto
export DEB_CFLAGS_MAINT_APPEND  = -fdebug-default-version=4
export DEB_CXXFLAGS_MAINT_APPEND  = -fdebug-default-version=4

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
# Detect the exact Qt version for the header paths
QT_VERSION := $(shell pkg-config --modversion Qt6Core)

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

# 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


%:
	DEB_BUILD_MAINT_OPTIONS=optimize=-lto dh $@ --buildsystem=cmake+ninja --builddirectory=build


# 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:
	# --- Fix CMake Version Bullshit ---
	sed -i 's/6.10.0/6.8.0/g' cmake/SearchForStuff.cmake
	sed -i 's/COMPONENTS CorePrivate GuiPrivate WidgetsPrivate REQUIRED//g' cmake/SearchForStuff.cmake
	sed -i 's/KDDockWidgets-qt6 2.3.0/KDDockWidgets-qt6 2.0.0/g' cmake/SearchForStuff.cmake

	# --- Fix Code Incompatibility (Qt 6.10 vs 6.8.2) ---
	# 1. Fix GameListWidget filtering (the beginFilterChange/Direction::Rows errors)
	sed -i 's/beginFilterChange();/\/\/ Qt 6.8 workaround/g' pcsx2-qt/GameList/GameListWidget.cpp
	sed -i 's/endFilterChange(Direction::Rows);/invalidateFilter();/g' pcsx2-qt/GameList/GameListWidget.cpp

	# 2. Comment out calls to 'nativeGeometry' which doesn't exist in 6.8.2
	grep -rll "nativeGeometry" pcsx2-qt/ | xargs -r sed -i 's/.*nativeGeometry.*/\/\/ Qt 6.10 hack/g'
	
	# 3. Fix potential 'devicePixelRatio' issues in newer PCSX2 code
	sed -i 's/screen->devicePixelRatio()/1.0f/g' pcsx2-qt/QtUtils.cpp || true

	LDFLAGS="-Wl,--gc-sections" \
	dh_auto_configure -- \
		-DCMAKE_CXX_FLAGS="-I/usr/include/$(DEB_HOST_MULTIARCH)/qt6/QtCore/$(QT_VERSION)/QtCore -I/usr/include/$(DEB_HOST_MULTIARCH)/qt6/QtGui/$(QT_VERSION)/QtGui -I/usr/include/$(DEB_HOST_MULTIARCH)/qt6/QtWidgets/$(QT_VERSION)/QtWidgets" \
		-DSHADERC_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libshaderc_shared.so \
		-DSHADERC_INCLUDE_DIR=/usr/include \
		-DPCSX2_GIT_REV_OVERRIDE="$(shell dpkg-parsechangelog --show-field Version | sed "s/^[0-9]://")" \
		-DPACKAGE_MODE=ON \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_BUILD_STRIP=FALSE \
		-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
		-DDISABLE_ADVANCE_SIMD=TRUE \
		-DWAYLAND_API=ON \
		-DUSE_BACKTRACE=OFF \
		-DUSE_LINKED_FFMPEG=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_C_COMPILER=/usr/bin/clang \
		-DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
		-DCMAKE_EXE_LINKER_FLAGS_INIT="-fuse-ld=lld -Wl,--build-id=sha1" \
		-DCMAKE_MODULE_LINKER_FLAGS_INIT="-fuse-ld=lld -Wl,--build-id=sha1" \
		-DCMAKE_SHARED_LINKER_FLAGS_INIT="-fuse-ld=lld -Wl,--build-id=sha1"

override_dh_auto_install:
	dh_auto_install
	# DX11 shaders are irrelevant on Linux/Debian
	rm -rf debian/tmp/usr/share/PCSX2/resources/shaders/dx11
	
	# --- Fix Missing Doc Bullshit ---
	mkdir -p debian/tmp/usr/share/doc/PCSX2
	cp README.md debian/tmp/usr/share/doc/PCSX2/ || touch debian/tmp/usr/share/doc/PCSX2/README
	
	install -D .github/workflows/scripts/linux/pcsx2-qt.desktop debian/tmp/usr/share/applications/PCSX2.desktop
	install -D bin/resources/icons/AppIconLarge.png debian/tmp/usr/share/icons/hicolor/256x256/apps/PCSX2.png

override_dh_strip:
	dh_strip --package=pcsx2-stable --dbg-package=pcsx2-stable-dbg

override_dh_dwz:
	# Disable dwz to avoid ".debug_str_offsets" errors with Clang's DWARF 5 output
	:

override_dh_auto_test:
	# Tests usually require a GUI environment; skipping during build