#!/usr/bin/make -f

# Debian Package Building Makefile

# Package maintainer: John Goerzen <jgoerzen@complete.org>

# Configuration and compilation

PACKAGENAME=netmaze
BASE=debian/tmp
BINARIES=$(PACKAGENAME)
AUXBINARIES=netserv betterbot dummy follower
LIBDIR=$(BASE)/usr/lib/games/$(PACKAGENAME)
STRIP=strip
SHELL=/bin/bash

# use -02 on ia64 to avoid a ftbfs
DEB_BUILD_ARCH_CPU ?=$(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)
ifeq ($(DEB_BUILD_ARCH_CPU),ia64)
	OPTLEVEL=-O2
else
	OPTLEVEL=-O3
endif

export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog --count 1 -SDate)" +%s)

build:
	xmkmf
# Force compilation using the desired optimisation level....
	cp Makefile Makefile~
	sed s/-O2/$(OPTLEVEL)/ Makefile~ > Makefile
	make
	touch build

binary: binary-arch binary-indep

binary-indep: build

# This one needs to be root.

binary-arch: build
	rm -rf debian/tmp
	install -d debian/tmp debian/tmp/DEBIAN debian/tmp/usr/games \
		debian/tmp/usr/share/doc/$(PACKAGENAME) debian/tmp/usr/lib/menu \
		$(LIBDIR)
# Netmaze goes into bin, everything else to the lib dir.
	install -m 0755 $(BINARIES) debian/tmp/usr/games
	install -m 0755 xnetserv debian/tmp/usr/games
	install -m 0755 $(AUXBINARIES) $(LIBDIR)/
	cp -r mazes lib *.tcl $(LIBDIR)
	chmod 0755 $(LIBDIR)/{mazes,lib,*.tcl}
	uudecode nmlogo.gif.uue
	cp nmlogo.gif $(LIBDIR)
	cp debian/changelog debian/tmp/usr/share/doc/$(PACKAGENAME)/changelog.Debian
	cp README.Debian CREDITS README TODO debian/tmp/usr/share/doc/$(PACKAGENAME)
	cp CHANGES debian/tmp/usr/share/doc/$(PACKAGENAME)/changelog
	gzip -9nvr debian/tmp/usr/share/doc/$(PACKAGENAME)
	cp debian/copyright debian/tmp/usr/share/doc/$(PACKAGENAME)/copyright
	cp debian/menu debian/tmp/usr/lib/menu/netmaze
	dpkg-shlibdeps debian/tmp/usr/games/netmaze \
		$(LIBDIR)/{netserv,betterbot,follower,dummy}
	install -m 755 debian/prerm debian/tmp/DEBIAN/
	install -m 755 debian/postinst debian/tmp/DEBIAN/
	install -m 755 debian/postrm debian/tmp/DEBIAN/
	dh_strip -Pdebian/tmp
	dpkg-gencontrol -isp
	chmod -R go-ws,go+r debian/tmp
	find debian/tmp -newermt "@$$SOURCE_DATE_EPOCH" -print0 | \
		xargs -0r touch --no-dereference --date="@$$SOURCE_DATE_EPOCH"
	dpkg-deb --root-owner-group --build debian/tmp ..

clean:
	-make clean
	-rm -rf build debian/tmp debian/files debian/substvars debian/*~ \
		*.o *~ Makefile *.bak
	-rm nmlogo.gif

build-arch: build
build-indep: build

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