#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS = yes
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

XCFLAGS = -Wall -Wunused -Wno-format-y2k -fPIE -fno-strict-aliasing

PKG_CONFIG = /usr/bin/$(DEB_HOST_GNU_TYPE)-pkg-config
libdir = /usr/lib/$(DEB_HOST_MULTIARCH)
GAMES = blocks checkers sudoku
EXTRA_MFLAGS = OPTIM="$(CFLAGS) $(XCFLAGS)" STRIP=@:

%:
	dh $@

override_dh_autoreconf:
	-mv configure debian/configure.saved
# FLTK uses a hand-maintained configh.in and an autoheader-unfriendly
# configure.ac.
	AUTOHEADER=/bin/true dh_autoreconf

override_dh_autoreconf_clean:
	dh_autoreconf_clean
	[ -e configure ] || mv debian/configure.saved configure

override_dh_auto_configure:
	mv fltk.spec fltk.spec.saved
	dh_auto_configure -- FTCONFIG="$(PKG_CONFIG) freetype2" \
            DSOFLAGS="$(filter-out -fPIE -pie,$(LDFLAGS))" \
	    --enable-shared --enable-threads --enable-xft \
	    --enable-xinerama --without-links --libdir=$(libdir) \
	    --with-optim=$(filter -O%,$(CXXFLAGS))
	mv fltk.spec.saved fltk.spec

override_dh_auto_build-arch:
	dh_auto_build -- $(EXTRA_MFLAGS) DIRS='$$(IMAGEDIRS) src fluid'
	$(MAKE) -C test $(EXTRA_MFLAGS) $(GAMES)

override_dh_auto_build-indep:
ifneq "" "$(filter nodoc,$(DEB_BUILD_OPTIONS))"
	dh_auto_build -- $(EXTRA_MFLAGS) DIRS='$$(IMAGEDIRS) src fluid test'
else
	dh_auto_build -- $(EXTRA_MFLAGS)
	$(MAKE) -C documentation fltk.pdf fltk.ps
endif

override_dh_auto_clean:
	mv fltk.spec fltk.spec.saved
# Avoid reconfigure.  Removed by distclean.  >> deliberate -- keeping
# existing contents allows for proper cleaning between builds.
	echo 'RMDIR = rm -Rf' >> makeinclude
	dh_auto_clean
	mv fltk.spec.saved fltk.spec

override_dh_clean:
	dh_clean debian/libfltk1.1.symbols.$(DEB_HOST_ARCH)

override_dh_auto_install-arch:
	dh_auto_install -a -- $(EXTRA_MFLAGS) DIRS='$$(IMAGEDIRS) src'
	mkdir -p debian/tmp/usr/games
	mkdir -p debian/tmp/usr/lib/fltk
	mkdir -p debian/tmp/usr/share/applications
	mkdir -p debian/tmp/usr/share/icons/hicolor/32x32/apps
	mkdir -p debian/tmp/usr/share/icons/hicolor/128x128/apps
	mkdir -p debian/tmp/usr/share/man/man6
	mkdir -p debian/tmp/usr/share/pixmaps
## fltk1.1-games (installed by hand to be able to add fl prefixes.)
	for game in $(GAMES); do \
	    cp test/$$game debian/tmp/usr/games/fl$$game && \
	    sed -e 's,Exec=,&/usr/games/fl,; s,Icon=,&fl,; s,Name=,&FLTK ,' \
	      test/desktop/$$game.desktop \
	      >debian/tmp/usr/share/applications/fl$$game.desktop && \
	    cp test/desktop/$$game-32.png \
	      debian/tmp/usr/share/icons/hicolor/32x32/apps/fl$$game.png && \
	    cp test/desktop/$$game-128.png \
	      debian/tmp/usr/share/icons/hicolor/128x128/apps/fl$$game.png && \
	    sed -e '1s,^\.TH ,&fl,; 3s,^,fl,' documentation/$$game.man \
	      >debian/tmp/usr/share/man/man6/fl$$game.6 \
	    || exit 1; \
	done
## libfltk1.1-dev
	cp CMake/FLTKUse.cmake debian/CMakeCache.txt debian/tmp/usr/lib/fltk
	sed 's,@libdir@,$(libdir),' debian/FLTKConfig.cmake.in \
	    >debian/tmp/usr/lib/fltk/FLTKConfig.cmake

override_dh_auto_install-indep:
	dh_auto_install -i -- $(EXTRA_MFLAGS) DIRS='documentation test'

override_dh_install-arch:
## libfltk1.1-dev
	sed -e 's/ -f[a-z]*-prefix-map=[^ ]*/ /g' fltk-config \
	    > debian/tmp/usr/bin/fltk-config
	chmod +x debian/tmp/usr/bin/fltk-config
	dh_install

override_dh_installdocs:
	dh_installdocs -A CREDITS README

override_dh_makeshlibs-arch:
# On most Debian architectures, 1.1.9-3 was the first version to build
# with GCC 4.3, which proceeded to introduce a few new symbols.  On
# alpha, however, 4.2 remained the default through lenny's release.
ifeq ($(DEB_HOST_ARCH),alpha)
	sed -e 's/1\.1\.9-3~/1.1.10~rc2/' debian/libfltk1.1.symbols \
	  > debian/libfltk1.1.symbols.$(DEB_HOST_ARCH)
endif
	dh_makeshlibs  -V

override_dh_installchangelogs:
	dh_installchangelogs
	for x in debian/*/usr/share/doc/*/changelog; do \
	    mv -v $$x `dirname $$x`/NEWS; \
	done
