#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

installdoc = install -g root -o root -m 644

TARGET = luola

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# The autotools target adds forced build-time dependencies on
# autotools-dev (for /usr/share/misc/config.*) and devscripts (for dch)
# It's also a .PHONY make target.
autotools:
	-chmod +x config.sub config.guess
	OLDDATESUB=`./config.sub -t | tr -d -` ;\
	OLDDATEGUESS=`./config.guess -t | tr -d -` ;\
	NEWDATESUB=`/usr/share/misc/config.sub -t | tr -d -` ;\
	NEWDATEGUESS=`/usr/share/misc/config.guess -t | tr -d -` ;\
	if [ $$OLDDATESUB -lt $$NEWDATESUB -o \
	     $$OLDDATEGUESS -lt $$NEWDATEGUESS ]; then \
	   echo dch -a -p "GNU config automated update: config.sub\
	     ($$OLDDATESUB to $$NEWDATESUB), config.guess\
	     ($$OLDDATEGUESS to $$NEWDATEGUESS)" ;\
	   cp -f /usr/share/misc/config.sub config.sub ;\
	   cp -f /usr/share/misc/config.guess config.guess ;\
	   echo WARNING: GNU config scripts updated from master copies 1>&2 ;\
	fi

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	./configure $(confflags) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--datadir=/usr/share/games \
		--bindir=/usr/games \
		--enable-sound \
		--enable-sdl-gfx \
		LIBS=-lm 

	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

#build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

# manpage has been added to the diff
	touch build-stamp

# does not work on m68k, so add it to the diff and run this only when needed
manpage:
	/usr/bin/docbook-to-man debian/luola.sgml > luola.6
	nroff -man luola.6 | less

clean: autotools
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f Makefile ] || $(MAKE) -C src distclean

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(installdoc) $(CURDIR)/debian/$(TARGET)-icon.xpm \
		$(CURDIR)/debian/$(TARGET)/usr/share/pixmaps/
	$(installdoc) $(CURDIR)/luola.desktop \
		$(CURDIR)/debian/$(TARGET)/usr/share/applications

	# Add here commands to install the package into debian/luola.
	$(MAKE) install prefix=$(CURDIR)/debian/$(TARGET)/usr \
		bindir=$(CURDIR)/debian/$(TARGET)/usr/games \
		datadir=$(CURDIR)/debian/$(TARGET)/usr/share/games

	# do not ship font, link to fonts-freefont-ttf package instead (falls back to built-in font if not found)
	cd $(CURDIR)/debian/$(TARGET)/usr/share/games/$(TARGET)/font && \
	rm freesans.ttf && \
	ln -s /usr/share/fonts/truetype/freefont/FreeSansBold.ttf freesans.ttf

	$(installdoc) debian/$(TARGET).desktop $(CURDIR)/debian/$(TARGET)/usr/share/applications

	dh_movefiles --sourcedir=debian/$(TARGET)
	# remove leftover directory from the main package
	rm -rf $(CURDIR)/debian/$(TARGET)/usr/share/games

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i 
	dh_installdocs -i
	dh_installmenu -i
	dh_installman -i
	dh_installinfo -i
	dh_installchangelogs ChangeLog -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installmenu -a
	dh_installman -a luola.6
	dh_installinfo -a
	dh_installchangelogs ChangeLog -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	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 configure
