#!/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

# 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)

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

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

M4=m4
M4DEFS=-DMAEMO_MAJOR=$(MAEMO_VERSION)

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
	ifeq ($(shell expr "$(MAEMO_VERSION)" "<" 5), 1)
		# Does not build on gcc 3.3 x86 without this
		CONFOPTS += --disable-network
	endif
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

ifeq ($(shell expr "$(MAEMO_VERSION)" "<" 5), 1)
	# Diablo and older have "Games" folder in MyDocs
	CONFOPTS += --personal-dir=.games/OpenTTD
else
	# But Nokia removed it on Fremantle. Too bad!
	CONFOPTS += --personal-dir=.openttd
endif

# We link with x11 due to XStoreName usage.
LDFLAGS += $(shell pkg-config --libs x11)

Makefile: configure
	dh_testdir
	
	./configure --prefix-dir=/ --binary-dir=usr/games --data-dir=usr/share/games/openttd --doc-dir=delete --icon-dir=delete --icon-theme-dir=delete --man-dir=delete --menu-dir=delete --install-dir=$(CURDIR)/debian/openttd/ --without-menu-entry --disable-strip $(CONFOPTS) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

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

build: build-stamp

build-stamp: Makefile $(QUILT_STAMPFN)
	dh_testdir

	$(MAKE) $(MAKEFLAGS)

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	-$(MAKE) clean
	rm -rf $(CURDIR)/bundle
	rm -rf $(CURDIR)/objs
	rm -rf $(CURDIR)/bin/lang
	rm -f $(CURDIR)/bin/openttd
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	rm -f Makefile

	dh_clean 

install: build 
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_clean -k -i
	dh_installdirs

	$(MAKE) install INSTALL_DATA_DIR=$(CURDIR)/debian/openttd-data/usr/share/games/openttd
	# Delete non-wanted files
	rm -r $(CURDIR)/debian/openttd/delete
	# Add icons
	install -m 0644 $(CURDIR)/debian/openttd.desktop $(CURDIR)/debian/openttd/usr/share/applications/hildon
	uudecode < $(CURDIR)/debian/icons/openttd26.png.uu > $(CURDIR)/debian/openttd/usr/share/icons/hicolor/26x26/hildon/openttd.png
	uudecode < $(CURDIR)/debian/icons/openttd40.png.uu > $(CURDIR)/debian/openttd/usr/share/icons/hicolor/40x40/hildon/openttd.png
	uudecode < $(CURDIR)/debian/icons/openttd64.png.uu > $(CURDIR)/debian/openttd/usr/share/icons/hicolor/scalable/hildon/openttd.png
	
	# Update debian control files
	$(M4) $(M4DEFS) debian/control.m4 > debian/control
	$(M4) $(M4DEFS) debian/openttd.postinst.m4 > debian/openttd.postinst
	$(M4) $(M4DEFS) debian/openttd-data.preinst.m4 > debian/openttd-data.preinst
	$(M4) $(M4DEFS) debian/openttd-data.prerm.m4 > debian/openttd-data.prerm

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot

ifeq ($(shell expr "$(MAEMO_VERSION)" "<" 5), 1)
#	Install to SD in pre-Fremantle versions
#	Create a list of all "system" files (postrm / prerm will use that)
	( cd $(CURDIR)/debian/openttd-data/usr/share/games/openttd ; \
		find -type f | grep -v ".pkg-" > .pkg-filelist ; \
		find -type d | grep -v "^.$$" > .pkg-dirlist)
		
#	Fixing permissions manually, since they're going to a FAT32 volume.
	chown -R user:root $(CURDIR)/debian/openttd-data/usr/share/games/openttd/*
	chown user:root $(CURDIR)/debian/openttd-data/usr/share/games/openttd/.pkg-*
else
#	Manual optification in Fremantle.
	dh_fixperms -i
#	Data files optified by preinst script, which will be preinstalled and
#	create links appropiately.
endif
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i	
	

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installchangelogs changelog.txt
#	dh_installdocs
#	dh_installexamples
#	dh_installman
#	dh_install --sourcedir=$(CURDIR)/debian/tmp
	dh_desktop -a
	dh_link -a
	dh_strip -a --dbg-package=openttd-dbg
	dh_compress -a
	dh_fixperms -a
ifeq ($(shell expr "$(MAEMO_VERSION)" ">=" 5), 1)	
#	Binary optified here (moved to /opt/openttd)
	dh_installdirs -p openttd usr/share/games/openttd
	mv $(CURDIR)/debian/openttd/usr/games/openttd $(CURDIR)/debian/openttd/usr/share/games/openttd/
#	We require an absolute symlink, so don't use dh_link here.
	ln -sf /usr/share/games/openttd/openttd $(CURDIR)/debian/openttd/usr/games/openttd
#	Debug package optified here
	dh_installdirs -p openttd-dbg usr/share/games/openttd
	mv $(CURDIR)/debian/openttd-dbg/usr/lib/debug/usr/games/openttd $(CURDIR)/debian/openttd-dbg/usr/share/games/openttd/openttd.debug
	ln -sf /usr/share/games/openttd/openttd.debug $(CURDIR)/debian/openttd-dbg/usr/lib/debug/usr/games/openttd
endif
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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