#!/usr/bin/make -f

export DH_VERBOSE=1

DEB_CFLAGS_MAINT_APPEND := -Wall -fno-strict-aliasing

%:
	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.
	# GCC >= 15 defaults to C23, where `bool' is a keyword; keep the
	# historical `typedef int bool' in src/sawfish.h valid by compiling
	# with the C17 standard (behaviour unchanged).
	# rep/rep_subrs.h (librep-dev) contains a K&R-era `#define inline'
	# which turns every `static inline' function of the glib headers
	# (included afterwards via gdk-pixbuf-xlib.h) into a plain `static'
	# function; without optimization these are all emitted and leave
	# dozens of undefined glib symbols (e.g. g_dir_close) in objects
	# like libclient_.lo, breaking the dlopen of client.so at build
	# time.  Pre-defining `inline' as itself makes rep's `#ifndef
	# inline' guard skip the harmful definition.
	dh_auto_build -O--parallel -- CFLAGS="$(CFLAGS) -std=gnu17 -Dinline=inline"

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
