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

# Guess the OpenGFX Tar filename by looking at the package version number
VERSION=$(shell dpkg-parsechangelog | grep 'Version: ' | sed 's/Version: //' | sed 's/-.*$$//')

OPENTTD_DATA_DIR:=/usr/share/games/openttd/data
OPENGFX_TAR:=$(OPENTTD_DATA_DIR)/opengfx-$(VERSION).tar

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp $(QUILT_STAMPFN)
	dh_testdir
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
# Clean doesn't seem to clean enough, and mrproper seems to clean too much...
	rm -fr .renum
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	
	$(M4) $(M4DEFS) debian/prerm.m4 > debian/prerm

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	mkdir -p $(CURDIR)/debian/openttd-opengfx$(OPENTTD_DATA_DIR)
	cp opengfx-$(VERSION).tar $(CURDIR)/debian/openttd-opengfx$(OPENTTD_DATA_DIR)
	
ifeq ($(shell expr "$(MAEMO_VERSION)" "<" 5), 1)
# Here comes a horrible Diablo hack.
# On Diablo, we plan to unpack this on a FAT filesystem, and we have to do
# some dirty tricks to avoid dpkg from failing.
#	Fixing permissions manually
	chown user:root $(CURDIR)/debian/openttd-opengfx$(OPENGFX_TAR)
else
	dh_fixperms
endif
	
	dh_compress
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

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