#!/usr/bin/make -f
#export DH_VERBOSE=1

export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

# out of tree build folder for kicad application
DEB_BUILD_DIR=debian/build
# default install folder
INSTDIR=$(CURDIR)/debian/tmp
# catching make jobs from DEB_BUILD_OPTIONS
PARALLEL_BUILD=$(echo $DEB_BUILD_OPTIONS | sed -e '/parallel=/!s/.*/1/;s/.*parallel=\([0-9]\+\).*/\1/g')

# other variables
include /usr/share/dpkg/pkg-info.mk
VERSION_EXTRA   = $(shell echo "$(DEB_VERSION)" | cut -d+ -f2-)
DATE            = $(shell date +'%Y-%m-%d' -d@"$(SOURCE_DATE_EPOCH)")
export REVDATE := $(shell TZ=UTC date -d@"$(SOURCE_DATE_EPOCH)" +%Y-%m-%d)

PREPROCESS_FILES := $(wildcard debian/*.in)

ASCIIDOCFILES := \
	doc/src/cvpcb/cvpcb.adoc \
	doc/src/doc_writing_style_policy/doc_writing_style_policy.adoc \
	doc/src/eeschema/eeschema.adoc \
	doc/src/gerbview/gerbview.adoc \
	doc/src/getting_started_in_kicad/getting_started_in_kicad.adoc \
	doc/src/gui_translation_howto/gui_translation_howto.adoc \
	doc/src/idf_exporter/idf_exporter.adoc \
	doc/src/kicad/kicad.adoc \
	doc/src/pcbnew/pcbnew.adoc \
	doc/src/pl_editor/pl_editor.adoc \
	doc/src/plugins/plugins.adoc \
	$(NULL)

DEB_KICAD_CMAKE_OPTS := \
	-DBUILD_GITHUB_PLUGIN=ON \
	-DCMAKE_CXX_FLAGS="$(CXXFLAGS) -DNDEBUG" \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DKICAD_SCRIPTING=ON \
	-DKICAD_SCRIPTING_ACTION_MENU=ON \
	-DKICAD_SCRIPTING_PYTHON3=ON \
	-DKICAD_SCRIPTING_MODULES=ON \
	-DKICAD_SCRIPTING_WXPYTHON=ON \
	-DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON \
	-DKICAD_SPICE=ON \
	-DKICAD_USE_OCC=ON \
	-DKICAD_USE_OCE=OFF \
	-DKICAD_VERSION_EXTRA="+$(VERSION_EXTRA)" \
	$(NULL)

DEB_DOC_CMAKE_OPTS := \
	-DCMAKE_INSTALL_PREFIX=/usr \
	$(NULL)

DEB_I18N_CMAKE_OPTS := \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DKICAD_I18N_UNIX_STRICT_PATH=ON \
	$(NULL)

$(PREPROCESS_FILES:.in=): %: %.in
	sed -e 's,/@DEB_HOST_MULTIARCH@,$(DEB_HOST_MULTIARCH:%=/%),g' $< > $@

%:
	dh $@ --with-python3

override_dh_auto_configure:
	sed -i 's/@REVDATE@/$(REVDATE)/' $(ASCIIDOCFILES)
	mkdir -p $(DEB_BUILD_DIR)
	#################################
	# configuring KiCad application #
	#################################
	dh_auto_configure --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR) -- $(DEB_KICAD_CMAKE_OPTS)
	###################################
	# configuring KiCad documentation #
	###################################
	dh_auto_configure --sourcedirectory=$(CURDIR)/doc --builddirectory=$(DEB_BUILD_DIR)/doc -- $(DEB_DOC_CMAKE_OPTS)
	################################
	# configuring KiCad i18n files #
	################################
	dh_auto_configure --sourcedirectory=$(CURDIR)/i18n --builddirectory=$(DEB_BUILD_DIR)/i18n -- $(DEB_I18N_CMAKE_OPTS)

override_dh_auto_build:
	# The other parts of KiCad like documentation, i18n files and the
	# libraries don't need to be build as arch depended, they even
	# don't need to be prebuild before calling dh_install. So ignoring
	# here. Only build the KiCad application here.
	dh_auto_build --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)

override_dh_auto_install: $(PREPROCESS_FILES:.in=)
	################################
	# installing KiCad application #
	################################
	dh_auto_install --destdir=$(INSTDIR) --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)
	# Removing a potentially added RUNPATH from the Pcbnew Python library.
	chrpath --delete $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/_pcbnew.so
	###############################################
	# building and installing KiCad documentation #
	###############################################
	dh_auto_install --destdir=$(INSTDIR) --sourcedirectory=$(CURDIR)/doc --builddirectory=$(DEB_BUILD_DIR)/doc
	###############################
	# installing KiCad i18n files #
	###############################
	dh_auto_install --destdir=$(INSTDIR) --sourcedirectory=$(CURDIR)/i18n --builddirectory=$(DEB_BUILD_DIR)/i18n
	# some tweaks before running the next dh sequencers
	# fixing icon sizes
	images=$$(find $(INSTDIR)/usr/share/icons/hicolor/48x48 -name "*.png"); \
	for f in $$images; do \
		size=$$(identify $$f | sed -n 's/.* PNG \(4[^ ]*\) .*/\1/ p'); \
		if [ "$$size" != "48x48" ]; then \
			echo "Resize $$(basename $$f) : $$size => 48x48"; \
			convert -resize '48x48!' $$f $$f.tmp && mv $$f.tmp $$f; \
		fi; \
	done
	# The kicad XPM icon comes with a to high resolution, fixing before install.
	convert -resize '32x32!' $(CURDIR)/bitmaps_png/icons/icon_kicad.xpm $(CURDIR)/bitmaps_png/icons/kicad.xpm

override_dh_install:
	dh_install
	dh_python3

override_dh_fixperms-arch:
	dh_fixperms
	# fixing file permissions for *.py files in kicad
	for i in `find $(CURDIR)/debian/kicad/usr -type f -name "*.py"`; do \
		FOUND_PYTHON=$$(head -n1 $$i | grep python); \
		if [ "$$FOUND_PYTHON" != "" ]; then \
			chmod +x $$i; \
		else \
			chmod -x $$i; \
		fi; \
	done

override_dh_strip-arch:
	dh_strip
	# strip unneeded symbols from the kicad specific libraries in /usr/lib/kicad/
	strip --strip-unneeded --remove-section=.comment $(CURDIR)/debian/kicad/usr/lib/kicad/_*.kiface

override_dh_shlibdeps:
	dh_shlibdeps -a -l $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
		-Xlibkicad_3dsg \
		-Xlibs3d_plugin_idf \
		-Xlibs3d_plugin_vrml \
		-X_pcbnew.$(DEB_HOST_MULTIARCH)
