#!/usr/bin/make -f
#export DH_VERBOSE=1

DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXOPTFLAGS+=
else
	CXXOPTFLAGS+=-O2 -fomit-frame-pointer
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif


build: build-stamp
build-stamp:
ifeq ($(DEB_BUILD_GNU_TYPE), powerpc-linux-gnu)
	$(error powerpc builds disabled. See http://bugs.debian.org/432666 for details)
endif
	dh_testdir
	convert debian/sauerbraten.png debian/sauerbraten.xpm
	$(MAKE) CXXOPTFLAGS="$(CXXOPTFLAGS)"
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	$(MAKE) clean
	rm -f debian/sauerbraten.xpm
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install

binary-indep: build install
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	# Uhm... yes, it's a hack to strip \.dfsg.* from the version... Improvements
	# appreciated! It's used by dh_gencontrol
	echo "sauerbraten-data-ver=`head -n1 debian/changelog | egrep -o '\(.*\)' | sed -e 's/(//' | sed -e 's/\.dfsg.*//'`" \
			>> debian/sauerbraten.substvars
	dh_installdocs
	dh_installdocs --all debian/README.Debian
	dh_installdocs -psauerbraten debian/NEWS.Debian
	dh_install
	dh_installmenu -psauerbraten
	dh_installman -psauerbraten debian/sauerbraten.6
	dh_installman -psauerbraten-server debian/sauerbraten-server.6
	dh_icons -psauerbraten
	dh_link
	dh_strip -psauerbraten --dbg-package=sauerbraten-dbg
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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