#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

export DH_VERBOSE=1

export DPKG_GENSYMBOLS_CHECK_LEVEL = 1

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

export CFLAGS 

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

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
libdir = /usr/lib/


configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	dh_auto_configure -- CC="$(CC)" CXX="$(CXX)" --enable-shared --enable-cairo --enable-cp936 --libdir=$(libdir) $(shell DEB_BUILD_MAINT_OPTIONS=hardening=-all)
	mv fltk-config fltk-config.unhardened
	dh_auto_configure -- CC="$(CC)" CXX="$(CXX)" DSOFLAGS="$(filter-out -fPIE -pie,$(LDFLAGS))" --enable-shared --enable-cairo --enable-cp936 --with-links --libdir=$(libdir)
#	mv fltk.spec.saved fltk.spec
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	# Add here commands to compile the package.
	dh_auto_build -- OPTIM="$(CFLAGS) $(XCFLAGS)" STRIP=@: CAIROLIBS=-lcairo
#	-$(MAKE) -C documentation all html pdf
	$(MAKE) -C documentation all html
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	$(MAKE) clean
	dh_clean

	# Add here commands to clean up after the build process.
	rm -rf build
	rm -f debian/*.symbols

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp prefix=/usr install
	mkdir -p debian/tmp/usr/games
	mkdir -p debian/tmp/usr/lib/fltk
	mkdir -p debian/tmp/usr/share/applications/hildon
	mkdir -p debian/tmp/usr/share/doc/fltk1.3-doc
	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.3-doc
#	cp documentation/latex/refman.pdf debian/tmp/usr/share/doc/fltk1.3-doc/fltk.pdf
## fltk1.3-games (installed by hand to be able to add fl prefixes.)
	for game in blocks checkers sudoku; 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/hildon/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/src/$$game.man  > debian/tmp/usr/share/man/man6/fl$$game.6; \
	done
## fluid
	$(MAKE) -C fluid install-linux DESTDIR=$(CURDIR)/debian/tmp
	mv debian/tmp/usr/share/applications/fluid.desktop debian/tmp/usr/share/applications/hildon/fluid.desktop
	cp fluid/icons/fluid-32.xpm debian/tmp/usr/share/pixmaps
## libfltk1.3-devB
	cp debian/CMakeCache.txt debian/*.cmake debian/tmp/usr/lib/fltk
	for x in debian/*.cmake.in; do \
	    sed 's,@libdir@,$(libdir),g' $$x \
		> debian/tmp/usr/lib/fltk/`basename $$x .in`; \
	done
	install fltk-config.unhardened debian/tmp/usr/bin/fltk-config

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs CREDITS README
	dh_installexamples
	dh_install --sourcedir=debian/tmp
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip --dbg-package=libfltk1.3-dbg
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dpkg-gensymbols -plibfltk1.3 -Odebian/libfltk1.3.symbols
	dpkg-gensymbols -plibfltk-cairo1.3 -Odebian/libfltk-cairo1.3.symbols
	dpkg-gensymbols -plibfltk-forms1.3 -Odebian/libfltk-forms1.3.symbols
	dpkg-gensymbols -plibfltk-images1.3 -Odebian/libfltk-images1.3.symbols
	dpkg-gensymbols -plibfltk-gl1.3 -Odebian/libfltk-gl1.3.symbols
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
ifneq (,$(findstring armel,$(DEB_BUILD_ARCH)))
	maemo-optify
endif
	dh_md5sums
	dh_builddeb

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

