#!/usr/bin/make -f

shellescape='$(subst ','\'',$(1))'
shellexport=$(1)=$(call shellescape,${$(1)})

CC?=			gcc
EXTRA_CFLAGS=		-Wall -Wextra -Wformat
EXTRA_CPPFLAGS=
EXTRA_LDFLAGS=		-Wl,--as-needed

# for now
EXTRA_CFLAGS+=		-Wno-unused-variable -Wno-unused-parameter
EXTRA_CFLAGS+=		-Wno-unused-but-set-variable

ifneq (,$(wildcard /usr/share/dpkg/buildflags.mk))
# dpkg-dev (>= 1.16.1~)
DEB_CFLAGS_MAINT_APPEND=${EXTRA_CFLAGS}
DEB_CPPFLAGS_MAINT_APPEND=${EXTRA_CPPFLAGS}
DEB_LDFLAGS_MAINT_APPEND=${EXTRA_LDFLAGS}
DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/buildflags.mk
else
# old-fashioned way to determine build flags
CFLAGS=			-O$(if $(findstring noopt,${DEB_BUILD_OPTIONS}),0,2) -g
CFLAGS+=		${EXTRA_CFLAGS}
CPPFLAGS+=		${EXTRA_CPPFLAGS}
LDFLAGS+=		${EXTRA_LDFLAGS}
endif

build build-arch: builddir/xlax
build-indep:

builddir/xlax:
	dh_testdir
	-rm -rf builddir
	mkdir builddir
	cd builddir && ln -s ../* . && xmkmf && exec make \
	    DEFINES=$(call shellescape,${CPPFLAGS}) \
	    CDEBUGFLAGS=$(call shellescape,${CFLAGS}) \
	    CXXDEBUGFLAGS=$(call shellescape,${CXXFLAGS}) \
	    EXTRA_LDOPTIONS=$(call shellescape,${LDFLAGS})

clean:
	dh_testdir
	-rm -rf builddir
	dh_clean

binary-indep: build-indep

binary-arch: build-arch
	dh_testdir
	dh_testroot
	if test -x "$$(which dh_prep)"; then dh_prep; else dh_clean -k; fi
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installmenu
	dh_installman
	dh_lintian
	dh_link
	dh_strip
	dh_compress -X.htm
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: binary binary-arch binary-indep build build-arch build-indep clean
