#!/usr/bin/make -f

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

# Include architecture variables to detect i386 vs amd64
# Include architecture variables to detect the host CPU name from Debian
include /usr/share/dpkg/architecture.mk

# Map Debian architecture names to what the Free Pascal Compiler (FPC) expects:
# - amd64 -> x86_64
# - arm64 -> aarch64
# - armhf / armel -> arm
# - i386 stays i386
FPC_ARCH := $(shell echo $(DEB_HOST_ARCH_CPU) | sed -e 's/amd64/x86_64/' -e 's/arm64/aarch64/' -e 's/arm.*/arm/')

#export DH_VERBOSE = 1

export HOME=$(CURDIR)/.fakeHome

# 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


%:
	dh $@


# 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_clean:
	find $(CURDIR) \( -name '*.o' -o -name '*.ppu' -o -name '*.a' -o -name '*.compiled' -o -name '*.or' \) -delete -print
	rm -Rfv .fakeHome peazip-sources/dev/pea peazip-sources/dev/peazip
	dh_auto_clean

override_dh_auto_build:
	# Register the dark style package
	lazbuild --add-package $(CURDIR)/peazip-sources/dev/metadarkstyle/metadarkstyle.lpk
	
	# Build the project using the mapped FPC architecture name
	lazbuild --cpu=$(FPC_ARCH) \
	         --widgetset=qt6 \
	         --build-all \
	         --max-process-count=1 \
	         $(CURDIR)/peazip-sources/dev/project_pea.lpi \
	         $(CURDIR)/peazip-sources/dev/project_peach.lpi

execute_after_dh_install:
	find $(CURDIR)/debian/peazip -name 'place * binary here.txt' -delete -print
	rm -Rfv $(CURDIR)/debian/peazip/usr/share/peazip/batch/Windows
	rm -Rfv $(CURDIR)/debian/peazip/usr/share/peazip/batch/bat
	rm -Rfv $(CURDIR)/debian/peazip/usr/share/peazip/batch/macOS*
	chmod +x $(CURDIR)/debian/peazip/usr/share/peazip/batch/freedesktop_integration/Nautilus-scripts/PeaZip/*