#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

SHELL := /bin/bash

include /usr/share/GNUstep/debian/config.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

p_app    := cenon.app
p_com    := cenon.app-common
d_app    := $(CURDIR)/debian/tmp
d_com    := $(CURDIR)/debian/$(p_com)
resdir   := $(GNUSTEP_SYSTEM_APPS)/Cenon.app/Resources
usrshare := /usr/share/GNUstep/Cenon.app

%:
	dh $@

override_dh_auto_build:
	dh_auto_build -- $(optim) $(verbose) \
	  $(shell dpkg-buildflags --export=cmdline)
	convert Icons/Cenon.tiff -resize 64x64 Cenon.png
# Construct something that resembles a ChangeLog.
	cd ChangeLog; for f in $$(ls -r *.txt); do cat $$f >> ChangeLog; done

override_dh_clean:
	dh_clean ChangeLog/ChangeLog Cenon.png

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog/ChangeLog

execute_before_dh_link:
	gsdh_gnustep
	mkdir -p $(d_com)$(usrshare)
	rm $(d_app)$(resdir)/{*.desktop,InspectorPanel.nib/._*}
# The Resources directory of the main app bundle contains
# arch-dependent files, so it can't be moved as usual.
	for i in $(d_app)$(resdir)/*.{tiff,lproj,nib,xib,plist,prolog,trailer}; do \
	  mv $$i $(d_com)$(usrshare) \
	  && dh_link -p$(p_app) $(usrshare)/$${i##*/} $(resdir)/$${i##*/}; \
	done
	for i in CharConversion CropMarks ; do \
	  mv $(d_app)$(resdir)/$$i $(d_com)$(usrshare) \
	  && dh_link -p$(p_app) $(usrshare)/$$i $(resdir)/$$i; \
	done
# Now move the Resources of the arch-dependent modules.
	for i in $(d_app)$(resdir)/*.prefs; do \
	  mv $$i/Resources $(d_com)$(usrshare)/$${i##*/} \
	  && dh_link -p$(p_app) $(usrshare)/$${i##*/} $(resdir)/$${i##*/}/Resources; \
	done
# Finally, move the whole Cenon Library -- it is entirely
# arch-independent, as misleading that is.
	mv $(d_app)$(GNUSTEP_SYSTEM_LIBRARY)/Cenon $(d_com)$(usrshare)
	dh_link -p$(p_app) $(usrshare)/Cenon $(GNUSTEP_SYSTEM_LIBRARY)/Cenon
	rm $(d_com)$(usrshare)/Cenon/._*
# Fixes lintian W: national-encoding.
	iconv -f ISO-8859-1 -t UTF-8 < Library/Cenon/Devices/din/sm_3000.dev \
	  > $(d_com)$(usrshare)/Cenon/Devices/din/sm_3000.dev
