#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# Using quilt
include /usr/share/quilt/quilt.make

# Determine Maemo version
MAEMO_VERSION:=$(shell cut -d"." -f1 /etc/maemo_version)

CFLAGS:=-Wall -g -DMAEMO -DMAEMO_VERSION=$(MAEMO_VERSION)
LDFLAGS:=-Wl,-z,defs

# Decide install prefix
ifeq ($(shell expr "$(MAEMO_VERSION)" "<" 5), 1)
	INSTALL_PREFIX=/usr/share/games
else
	INSTALL_PREFIX=/opt
endif

M4:=m4
M4DEFS:=-DMAEMO_MAJOR=$(MAEMO_VERSION) -DINSTALL_PREFIX=$(INSTALL_PREFIX)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS+=-O0
else ifneq (,$(findstring arm,$(DEB_BUILD_GNU_TYPE)))
	CFLAGS+=-Os
	ifneq (,$(findstring vfp,$(DEB_BUILD_OPTIONS)))
		CFLAGS+=-march=armv6j -mtune=arm1136jf-s -mfpu=vfp -mfloat-abi=softfp
	endif
else # x86
	CFLAGS+=-O2
endif

BUILDDIR=$(CURDIR)/build

debian/control: debian/control.m4
	$(M4) $(M4DEFS) debian/control.m4 > debian/control

configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	
	mkdir build
	cd $(BUILDDIR) && cmake \
		-DCMAKE_INSTALL_PREFIX="$(INSTALL_PREFIX)" \
		-DCMAKE_CXX_FLAGS:STRING="$(CFLAGS)" \
		-DCMAKE_CXX_LINK_FLAGS:STRING="$(LDFLAGS)" \
		$(CURDIR)
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir

	$(MAKE) -C $(BUILDDIR)/CorsixTH VERBOSE=1

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f $(CURDIR)/CorsixTH/Src/config.h
	rm -rf $(BUILDDIR)

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Create destination directories
	install -d $(CURDIR)/debian/corsixth$(INSTALL_PREFIX)/CorsixTH \
		$(CURDIR)/debian/corsixth-data$(INSTALL_PREFIX)/CorsixTH \
		$(CURDIR)/debian/corsixth/usr/bin

	# Put binary in prefix while installing data
	cp $(BUILDDIR)/CorsixTH/CorsixTH $(CURDIR)/debian/corsixth-data$(INSTALL_PREFIX)/CorsixTH
	$(MAKE) -C $(BUILDDIR)/CorsixTH DESTDIR=$(CURDIR)/debian/corsixth-data install
	
	# Remove binary from prefix and put it in arch-dep package
	mv $(CURDIR)/debian/corsixth-data$(INSTALL_PREFIX)/CorsixTH/CorsixTH \
		$(CURDIR)/debian/corsixth$(INSTALL_PREFIX)/CorsixTH/CorsixTH

	# Create and install launcher script
	$(M4) $(M4DEFS) debian/corsixth.sh.m4 > $(CURDIR)/debian/corsixth/usr/bin/corsixth
	chmod 0755 $(CURDIR)/debian/corsixth/usr/bin/corsixth
	chown root:root $(CURDIR)/debian/corsixth/usr/bin/corsixth

	# Add icons
	install -d $(CURDIR)/debian/corsixth/usr/share/applications/hildon \
		$(CURDIR)/debian/corsixth/usr/share/icons/hicolor/26x26/hildon \
		$(CURDIR)/debian/corsixth/usr/share/icons/hicolor/40x40/hildon \
		$(CURDIR)/debian/corsixth/usr/share/icons/hicolor/scalable/hildon	
	install -m 0644 $(CURDIR)/debian/corsixth.desktop $(CURDIR)/debian/corsixth/usr/share/applications/hildon
	uudecode < $(CURDIR)/debian/icons/26.png.uu > $(CURDIR)/debian/corsixth/usr/share/icons/hicolor/26x26/hildon/corsixth.png
	uudecode < $(CURDIR)/debian/icons/40.png.uu > $(CURDIR)/debian/corsixth/usr/share/icons/hicolor/40x40/hildon/corsixth.png
	uudecode < $(CURDIR)/debian/icons/64.png.uu > $(CURDIR)/debian/corsixth/usr/share/icons/hicolor/scalable/hildon/corsixth.png

	# Update debian control files
	$(M4) $(M4DEFS) debian/control.m4 > debian/control
	$(M4) $(M4DEFS) debian/corsixth.postinst.m4 > debian/corsixth.postinst


# 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_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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