#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for the Praat

DEBDIR = $(CURDIR)/debian
INSTDIR = $(DEBDIR)/praat

PIXDIR = usr/share/pixmaps
SVGDIR = usr/share/icons/hicolor/scalable/apps
BINDIR = usr/bin
APPDIR = usr/share/applications

SUBSTCMD = sed 's:@BINDIR@:$(BINDIR):g;s:@PIXDIR@:$(PIXDIR):g;	\
                s:@SVGDIR@:$(SVGDIR):g;s:@APPDIR@:$(APPDIR):g'

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk
-include /usr/share/dpkg/buildtools.mk
PKG_CONFIG ?= pkg-config

ifneq (,$(filter $(DEB_HOST_ARCH), i386))
    export DEB_CXXFLAGS_MAINT_APPEND=-ffloat-store
endif

%:
	dh $@ --with=python3

get-upstream-changelog:
	# Get upstream changelog (in HTML format) and replace some characters
	# for correct display in the final ASCII file
	( cd debian ;							    \
	  wget -N http://www.fon.hum.uva.nl/praat/manual/What_s_new_.html ; \
	  perl -pi -e "s/&#712;/'/g;s/&#716;/,/g;" What_s_new_.html )

clean:
	dh clean
	# Fix the lacking cleaning command for the main/ directory
	[ ! -f makefile.defs ] || make -C main clean

execute_before_dh_auto_clean:
	touch makefile.defs

override_dh_auto_configure:
	# Generate the debhelper files
	for i in dirs install ; do 				\
		$(SUBSTCMD) < $(DEBDIR)/$$i.in > $(DEBDIR)/praat.$$i ;	\
	done
	# Get rid of the
	# dpkg-shlibdeps: warning: dependency on lib??? could be avoided if "debian/praat/usr/bin/praat" were not uselessly linked against it (they use none of its symbols).
	# warnings and useless dependencies
	cp makefiles/makefile.defs.linux.pulse makefile.defs
	# Add hardening flags
	cat debian/mk-vars-hardening >> makefile.defs

%.1: $(DEBDIR)/%.md
	# Generate the man page from the Markdown source
	pandoc --standalone --to man $< -o $@

sendpraat: sys/sendpraat.c
	# Build the sendpraat program
	$(CC) $(shell dpkg-buildflags --get CPPFLAGS)		\
		$(shell dpkg-buildflags --get CFLAGS)		\
		$(shell dpkg-buildflags --get LDFLAGS)		\
		-std=gnu99 -DSTAND_ALONE -DUNIX			\
		-o sendpraat sys/sendpraat.c -lX11		\
		$(shell $(PKG_CONFIG) --cflags --libs gtk+-3.0)

override_dh_auto_build: sendpraat praat.1 praat_nogui.1 sendpraat.1 praat-open-files.1
	dh_auto_build
	cp makefiles/makefile.defs.linux.nogui makefile.defs
	# Add hardening flags
	cat debian/mk-vars-hardening >> makefile.defs
	rm -f melder/*.o sys/*.o fon/SoundRecorder.o fon/praat_Sound.o	\
		fon/FunctionEditor.o external/portaudio/*.o
	dh_auto_build

override_dh_auto_install:
	# Install the upstream changelog
	dh_installchangelogs debian/What_s_new_.html
	dh_auto_install -- INSTALL=

ifeq (,$(filter $(DEB_HOST_ARCH),armel armhf mipsel))
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	PRAAT=$(CURDIR)/praat_nogui debian/tests/run-tests
endif
endif
