#!/usr/bin/make -f

export DH_VERBOSE=1

# Flags applied to every compilation through dpkg-buildflags, so that both
# the build phase and the `make install' phase (which re-runs `make all' to
# regenerate src/build.h) use them.
# -std=gnu17: GCC >= 15 defaults to C23, where `bool' is a keyword and the
#   historical `typedef int bool' in src/sawfish.h is rejected.
# -Dinline=inline: rep/rep_subrs.h (librep-dev) contains a K&R-era
#   `#define inline' that erases the keyword, turning every `static inline'
#   function of the glib headers (included via gdk-pixbuf-xlib.h) into a
#   plain `static' one; without optimization those are emitted and leave
#   undefined glib symbols (e.g. g_dir_close) in objects like libclient_.lo,
#   breaking the dlopen of client.so.
# -fgnu89-inline: sawfish's `inline int cache_length()' (src/pixmap-cache.c)
#   relies on the old GNU89 rule that a non-static `inline' definition still
#   emits an out-of-line symbol; under C99 semantics it does not, and the
#   link fails with `undefined reference to cache_length'.
export DEB_CFLAGS_MAINT_APPEND := -Wall -fno-strict-aliasing -std=gnu17 -Dinline=inline -fgnu89-inline

%:
	dh $@ --parallel --with autoreconf --with autotools_dev

override_dh_auto_build:
	# Get rid of rpath
	set -e; \
	 tmpfile=`mktemp`; \
	 sed "s/^REP_LIBS=\(.*\)-Wl,--rpath -Wl,[^ ]* \(.*\)$$/REP_LIBS=\1 \2/" Makedefs \
	 | sed "s/^IMAGE_LIBS=\(.*\)/IMAGE_LIBS=\1 -lgio-2.0/" >$$tmpfile ;\
	 mv $$tmpfile Makedefs
	# gdk-pixbuf-xlib 2.42+ headers reference g_memory_monitor_get_type
	# (defined in libgio-2.0), but its .pc no longer lists gio-2.0 in
	# Libs (only in Libs.private), so add -lgio-2.0 to the link flags.
	dh_auto_build -O--parallel

override_dh_auto_install:
	dh_auto_install -O--parallel
	# Fixup libdir in .la files
	find debian/tmp -name \*.la | while read file; do \
	  libdir=`echo $$file | sed -e 's@debian/tmp\(.*\)/[^/]*la$$@\1@' `; \
	  tmpfile=`mktemp`; \
	  sed "s@^libdir=.*@libdir='$$libdir'@" $$file >$$tmpfile; \
	  mv $$tmpfile $$file; \
	done

	# Remove info files installed by Makefile. dh_installinfo will do it again without creating info.dir
	find debian -type d -name info | xargs rm -rf
	#empty dependency_libs file in la files
	find debian/tmp/usr/lib -name *.la | xargs \
		sed -i "/dependency_libs/ s/'.*'/''/"


override_dh_install:
	[ ! -f debian/sawfish-lisp-source.install ] || rm debian/sawfish-lisp-source.install
	find debian/tmp/usr/share/sawfish/lisp -name \*.jl | \
	 cut -d/ -f3- | grep -v 'autoload\|custom-defaults' > debian/sawfish-lisp-source.install

	cp debian/sawfish-data.install.in debian/sawfish-data.install
	find debian/tmp/usr/share/sawfish/lisp -name \*.jlc | \
	 cut -d/ -f3- | grep -v 'main' >> debian/sawfish-data.install

	#dh_install --fail-missing
	dh_install --list-missing
