#!/usr/bin/make -f

#export DH_VERBOSE=1

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

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	CROSS = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
	CROSS = --build $(DEB_BUILD_GNU_TYPE)
endif

CFLAGS  = -Wall -g
LDFLAGS = -Wl,-z,defs

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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif


# Configure the package
# -------------------------------------
configure: config.status
config.status:
	dh_testdir
	[ -d backup ] || ( mkdir backup && cp -f po/*.po backup )
#	sed -i -e 's_automake[^/" ]*_automake-1.10_g' autogen.sh
#	sed -i -e 's_aclocal[^/" ]*_aclocal-1.10_g' autogen.sh
#	./autogen.sh
	./configure \
	    CFLAGS="$(CFLAGS)" \
	    LDFLAGS="$(LDFLAGS)" \
	    $(CROSS) \
	    --prefix=/usr \
	    --bindir=\$${prefix}/games \
	    --datadir=\$${prefix}/share/games \
	    --mandir=\$${prefix}/share/man \
	    --with-icondir=\$${prefix}/share/icons \
	    --with-applicationdir=\$${prefix}/share/applications \
	    --with-distributor="Warzone 2100 Project" \
	    --disable-debug
# -------------------------------------


# Compile the source
# -------------------------------------
build: build-stamp 

build-stamp: config.status
	dh_testdir
	$(MAKE)
	touch $@
# -------------------------------------


# Clean source directory
# -------------------------------------
clean: clean-really

clean-really:
	dh_testdir
	dh_testroot
	dh_clean
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	-mv -f backup/*.po po/
#	find -iname Makefile.in -print0 | xargs -0 rm -f
#	rm -rf depcomp aclocal.m4 configure config.guess \
#	backup install-sh config.sub missing config.h.in po/warzone2100.pot
#	sed -i -e 's_automake[^/" ]*_automake_g' autogen.sh
#	sed -i -e 's_aclocal[^/" ]*_aclocal_g' autogen.sh
# -------------------------------------


# Install all files
# -------------------------------------
install: build
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
# -------------------------------------


# Build architecture-independent files
# -------------------------------------
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
# -------------------------------------


# Build architecture-dependent files
# -------------------------------------
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_install -a --sourcedir=debian/tmp --list-missing
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installmenu -a
	dh_icons -a
	dh_installman -a doc/warzone2100.6
	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: configure build clean clean-really binary-indep binary-arch binary install
