#!/usr/bin/make -f

# build-hardening flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

includes=$(strip $(shell pkg-config --cflags poppler))
LIBS+=-lXm $(shell pkg-config --libs poppler --libs xt --libs x11) -pthread
CPPFLAGS+=$(includes) $(includes)/goo $(includes)/splash
CPPFLAGS+=-Wno-write-strings -Wno-format-extra-args
CPPFLAGS+=-DHAVE_DIRENT_H -DSYSTEM_XPDFRC=\"/etc/xpdf/xpdfrc\"

files=goo/parseargs goo/gfile xpdf/CoreOutputDev xpdf/XPDFParams xpdf/PDFCore
files+=xpdf/XPDFApp xpdf/XPDFCore xpdf/XPDFTree xpdf/XPDFViewer xpdf/xpdf
headers=xpdf/config.h xpdf/XPDFTreeP.h xpdf/about-text.h xpdf/*.xbm xpdf/xpdfIcon.xpm

sources=$(shell for file in $(files); do echo $$file.*; done)
objects=$(shell for file in $(files); do echo build/$$(basename $$file).o; done)

override_dh_clean:
	dh_clean
	rm -rf build

override_dh_auto_configure:
	mkdir -p build
	cp $(sources) $(headers) build
	sed -e s/GString/GooString/g -e s/GMutex/GooMutex/g -e s/GHash/GooHash/g \
	    -e s/GList/GooList/g -e s/\<aconf\.h\>/\<poppler-config\.h\>/g \
	    -i build/*
	mv build/parseargs.c build/parseargs.cc

override_dh_auto_build: $(objects)
	$(CXX) $(LDFLAGS) -o build/xpdf.real build/*.o $(LIBS)

%:
	dh ${@}
