#!/usr/bin/make -f
# -*- makefile -*-

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

# location of datafiles
DATADIR=/usr/share/games/trigger-rally
# name of package
PACKAGE=trigger-rally
# what manpages to build?
MANPAGES=debian/trigger-rally.6

CFLAGS = -Wall -g

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

# rule to build manpages
$(MANPAGES): $(addsuffix .sgml,$(MANPAGES))
	docbook-to-man $< > $@

configure: configure.ac
	# make sure that the shipped glew is not used during build
	$(RM) -r src/glew
	# same goes for tinyxml
	$(RM) src/pengine/tinyxml.h
	$(RM) src/pengine/tinyxmlparser.cpp
	$(RM) src/pengine/tinyxml.cpp
	$(RM) src/pengine/tinystr.cpp
	$(RM) src/pengine/tinyxmlerror.cpp
	$(RM) src/pengine/tinystr.h
	./autogen.sh

config.status: configure
	dh_testdir
	CFLAGS="$(CFLAGS) -Wl,-z,defs" \
		./configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--datadir=$(DATADIR)


build: build-arch build-indep

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

clean::
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	if [ -f Jamconfig ]; then jam distclean; fi
	$(RM) $(MANPAGES)
	$(RM) configure mk/autoconf/sdl.m4 aclocal.m4 Jamconfig.in
	dh_clean 

install: build $(MANPAGES)
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# yay, we do it by hand :)
	cp trigger debian/$(PACKAGE)/usr/games/trigger-rally
	# icons
	cp debian/trigger-rally_32.xpm debian/$(PACKAGE)/usr/share/icons/hicolor/32x32/apps/trigger-rally.xpm
	cp debian/trigger-rally_64.xpm debian/$(PACKAGE)/usr/share/icons/hicolor/64x64/apps/trigger-rally.xpm

# no indep package
binary-indep:

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	# manually install desktop file
	cp debian/trigger-rally.desktop debian/$(PACKAGE)/usr/share/applications/trigger-rally.desktop
	dh_installmenu
	dh_installman $(MANPAGES)
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build-arch: build-stamp
# no indep package
build-indep:

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