#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess. 

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CPPFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CPPFLAGS += -O0
else
	CPPFLAGS += -O3
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CPPFLAGS += -g -O0 -DDEBUG
else
	CPPFLAGS += -DNDEBUG
endif




config.status: configure
	dh_testdir
	# Add here commands to configure the package.

configure:

build: build-stamp
build-stamp: config.status
	dh_testdir


	patch < debian/patches/102_fix-pearl-min-dimensions.diff
	patch < debian/patches/104_flood_signed_char.diff
	patch < debian/patches/206_translate-docs.diff
	patch < debian/patches/208_rename-rectangles-chapter.patch
	#patch < debian/patches/201_make-more-docs.diff
	patch < debian/patches/205_add_hildon_recipe.diff
	patch < debian/patches/207_slant-shade-filled.diff
	#patch < debian/patches/303_show-debian-version-number.diff
	patch < debian/patches/306_fix_input_text.diff
	patch < debian/patches/400_save_dir.diff
	patch < debian/patches/404_ingame_help.diff

	#These might encounter non-fatal errors
	./mkfiles.pl 
	./mkauto.sh || true
	automake || true

	#Use fixed libgtkhtml-3.14
	cp -r debian/include .

	# Add here commands to compile the package.
	CPPFLAGS="-DSTYLUS_BASED=1" ./configure  --bindir=/opt/sgt-puzzles --program-prefix=sgt-
	make
	make -f Makefile.doc BINPREFIX=sgt-

	mkdir -p doc_html
	cd doc_html && halibut --html ../preprocessed.but

	touch build-stamp

clean: config.status
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	#make clean #broken
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	make DESTDIR=debian/sgt-puzzles install

	#tune binaries to the correct folder
	mkdir -p -m755 debian/sgt-puzzles/opt/sgt-puzzles/puzzles
	mv debian/sgt-puzzles/opt/sgt-puzzles/sgt-* debian/sgt-puzzles/opt/sgt-puzzles/puzzles

	#Launcher
	install -m755 debian/sgt-launcher debian/sgt-puzzles/opt/sgt-puzzles/sgt-launcher

	# .desktop
	mkdir -p -m755  debian/sgt-puzzles/usr/share/applications/hildon
	install -m644 debian/sgt-puzzles.desktop debian/sgt-puzzles/usr/share/applications/hildon/

	# icon
	mkdir -p -m755 debian/sgt-puzzles/usr/share/icons/hicolor/48x48/apps/
	install -m644 debian/sgt-icon.png debian/sgt-puzzles/usr/share/icons/hicolor/48x48/apps/sgt-puzzles.png

	#help files
	sed -i '/Contents/d' doc_html/*.html  #drop 'navigation' from html-files
	mkdir -p -m755 debian/sgt-puzzles/opt/sgt-puzzles/help
	cp doc_html/*.html debian/sgt-puzzles/opt/sgt-puzzles/help

	# icons
	mkdir -p -m755 debian/sgt-puzzles/opt/sgt-puzzles/icons/
	for icon in icons/*-48d24.png; do \
	    base="$$(basename $$icon -48d24.png)" && \
	    install -m644 $$icon debian/sgt-puzzles/opt/sgt-puzzles/icons/sgt-$$base.png \
	    || exit; \
	done

	#CleanUp. Do not put anything under these directories
	rm -rf debian/sgt-puzzles/usr/games
	rm -rf debian/sgt-puzzles/usr/share/sgt-puzzles/
	rm -rf debian/sgt-puzzles/usr/share/help
	rm -rf debian/sgt-puzzles/usr/share/man

# 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
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installwm
#	dh_installudev
#	dh_undocumented
#	dh_installman
#	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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