#!/usr/bin/make -f
# You must remove unused comment lines for the released package.
export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

include /usr/share/dpkg/architecture.mk

include /usr/share/dpkg/buildtools.mk

# Produce .debug_info (used by dh_dwz)
CXXFLAGS += -g

# Enable PIE
#CXXFLAGS += -fpie
#LDFLAGS += -pie

# define APP_NAME_SUFFIX
DPKG_VENDOR = $(shell dpkg-vendor --query vendor)
CPPFLAGS += -DAPP_NAME_SUFFIX='L" for ${DPKG_VENDOR}"'

# It is sometimes required to build with a specific version of g++ because
# upstream sometimes used features that are available only in recent versions.
# This is therefore also required to build backports
# CXX := $(CXX)-12

# Check if we need to link to libatomic
# Used to be required on mipsel, sh4, armhf/raspbian
# but also on Ubuntu 20.04 (focal)
IS_ATOMIC_LINKING_REQUIRED = $(shell $(CXX) -std=c++2b -o /dev/null debian/material/libatomic_check.cpp > /dev/null 2>&1 || echo "yes")
ifeq ($(IS_ATOMIC_LINKING_REQUIRED),yes)
$(warning linking to libatomic is required)
LDFLAGS += -latomic
endif

%:
	dh $@ --sourcedirectory=FreeFileSync/Source

execute_after_dh_auto_clean:
	# Now, auto_clean RealTimeSync
	dh_auto_clean --sourcedirectory=FreeFileSync/Source/RealTimeSync

override_dh_auto_build:
	dh_auto_build --sourcedirectory=FreeFileSync/Source -- CXX=$(CXX)

execute_after_dh_auto_build:
	# Now, auto_build RealTimeSync
	dh_auto_build --sourcedirectory=FreeFileSync/Source/RealTimeSync -- CXX=$(CXX)

override_dh_auto_install:
	# desktop files
	# handled by dh_install through debian/install

	# mimetypes
	# handled by dh_installmime through debian/sharedmimeinfo

	# icons
	unzip -u FreeFileSync/Build/Resources/Icons.zip cfg_batch.png database.png start_sync.png FreeFileSync.png RealTimeSync.png
	for size in 16 24 32 48 64 128 256; do \
	  mkdir -p debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/apps ; \
	  convert FreeFileSync.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/apps/FreeFileSync.png ; \
	  convert RealTimeSync.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/apps/RealTimeSync.png ; \
	  mkdir -p debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/mimetypes ; \
	  convert cfg_batch.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/mimetypes/application-x-freefilesync-batch.png ; \
	  convert database.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/mimetypes/application-x-freefilesync-db.png ; \
	  convert start_sync.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/mimetypes/application-x-freefilesync-gui.png ; \
	  convert RealTimeSync.png -filter Lanczos -resize $${size}x$${size} debian/tmp/usr/share/icons/hicolor/$${size}x$${size}/mimetypes/application-x-freefilesync-real.png ; \
	done
