#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
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)

export deb_udevdir = $(shell pkg-config --variable=udevdir udev | sed s,^/,,)

ICON_SIZES=128x128 64x64 48x48 32x32 24x24 16x16

# out of tree build folder for application
DEB_BUILD_DIR=debian/build
# default install folder
INSTDIR=$(CURDIR)/debian/tmp

DEB_PONYPROG_CMAKE_OPTS := \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DUDEV_INSTALL_DIR="/$(deb_udevdir)/rules.d" \
	-DUSE_DEBUGGER=ON \
	-DUSE_QT5=ON \
	$(NULL)

%:
	dh $@

override_dh_auto_clean:
	dh_clean
	rm -f SrcPony/version.h
	rm -rf debian/build

override_dh_auto_configure:
	mkdir -p $(DEB_BUILD_DIR)
	####################################
	# configuring PonyProg application #
	####################################
	dh_auto_configure --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR) -- $(DEB_PONYPROG_CMAKE_OPTS)

override_dh_auto_build:
	dh_auto_build --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)

override_dh_auto_install:
	###################################
	# installing PonyProg application #
	###################################
	dh_auto_install --destdir=$(INSTDIR) --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)
	# remove the shipped icon and create the needed graphic by ourself
	rm -f $(INSTDIR)/usr/share/icons/ponyprog.png
	for size in ${ICON_SIZES}; do \
		mkdir -p $(INSTDIR)/usr/share/icons/hicolor/"$${size}"; \
		convert +antialias -background transparent -resize "$${size}!" $(CURDIR)/icons/ponyprog.svg $(INSTDIR)/usr/share/icons/hicolor/"$${size}"/ponyprog.png; \
	done
	# convert ponyprog.html to utf-8 if needed
	@PONYPROG_HTML_FILE_ENCODING=`file -b --mime-encoding debian/tmp/usr/share/doc/ponyprog/ponyprog.html` ;\
	if [ "$${PONYPROG_HTML_FILE_ENCODING}" != "utf-8" ]; then \
		cp debian/tmp/usr/share/doc/ponyprog/ponyprog.html debian/tmp/ponyprog.html.in ;\
		iconv -f "$${PONYPROG_HTML_FILE_ENCODING}" -t utf8 debian/tmp/ponyprog.html.in > debian/tmp/usr/share/doc/ponyprog/ponyprog.html ;\
		echo "usr/share/doc/ponyprog/ponyprog.html did had mime-encoding typ: \"$${PONYPROG_HTML_FILE_ENCODING}\" ... corrected to utf8!" ;\
		rm debian/tmp/ponyprog.html.in ;\
	fi
