#!/usr/bin/make -f

DPKG_EXPORT_BUILDTOOLS=1
include /usr/share/dpkg/buildtools.mk

testdir   = test -f src/faes.cpp && test -f debian/rules
testroot  = test x`whoami` = xroot

# FOR AUTOCONF 2.13 ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONFFLAGS += $(DEB_HOST_GNU_TYPE)
else
	CONFFLAGS += --host $(DEB_BUILD_GNU_TYPE) --build $(DEB_HOST_GNU_TYPE)
endif

CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS  = $(shell dpkg-buildflags --get LDFLAGS)
export CPPFLAGS
export CXXFLAGS
export LDFLAGS

BUILD_DATE=$(shell dpkg-parsechangelog --show-field Date)

clean:
	$(testdir)
	rm -f build-stamp debian/files debian/substvars
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf debian/pachi debian/pachi-data

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: configure
	$(testdir)
	SCOREDIR=/var/games/pachi ./configure $(CONFFLAGS) --prefix=/usr --bindir=/usr/games
	$(MAKE)
	touch $@

binary: binary-arch binary-indep

binary-arch: build
	$(testdir)
	$(testroot)
	rm -rf debian/pachi

	$(MAKE) install DESTDIR=$(CURDIR)/debian/pachi
	rm -rf debian/pachi/var/games/pachi/data/scores.dat \
		debian/pachi/usr/share/pachi \
		debian/pachi/usr/share/doc/pachi/ChangeLog \
		debian/pachi/usr/share/doc/pachi/License.txt

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	$(STRIP) --strip-unneeded -R .comment -R .note debian/pachi/usr/games/pachi
endif

	install -D -p -m 0644 debian/menu      debian/pachi/usr/share/menu/pachi
	install -D -p -m 0644 debian/pachi.6   debian/pachi/usr/share/man/man6/pachi.6
	install -D -p -m 0644 debian/desktop   debian/cgoban/usr/share/applications/pachi.desktop
	install -D -p -m 0644 debian/changelog debian/pachi/usr/share/doc/pachi/changelog.Debian
	install -D -p -m 0644 ChangeLog        debian/pachi/usr/share/doc/pachi/changelog
	install    -p -m 0644 debian/copyright debian/pachi/usr/share/doc/pachi
	install -D -p -m 0644 debian/control   debian/pachi/DEBIAN/control
	install    -p -m 0755 debian/postinst debian/postrm debian/pachi/DEBIAN

	gzip -9n debian/pachi/usr/share/doc/pachi/changelog* debian/pachi/usr/share/man/man6/pachi.6
	cd debian/pachi && find * -type f ! -regex "^DEBIAN/.*" -print0 | LC_ALL=C sort -z | xargs -0r md5sum > DEBIAN/md5sums

	dpkg-shlibdeps debian/pachi/usr/games/pachi
	dpkg-gencontrol -ppachi -Pdebian/pachi
	chown -R root:root debian/pachi
	chmod -R go=u-w debian/pachi
	chown root:games debian/pachi/usr/games/pachi
	chmod 2755 debian/pachi/usr/games/pachi
	find debian/pachi -depth -newermt '$(BUILD_DATE)' -print0 | xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build debian/pachi ..

binary-indep: build
	$(testdir)
	$(testroot)
	rm -rf debian/pachi-data

	$(MAKE) install DESTDIR=$(CURDIR)/debian/pachi-data
	rm -rf debian/pachi-data/var \
		debian/pachi-data/usr/games \
		debian/pachi-data/usr/share/doc

	install -D -p -m 0644 debian/changelog debian/pachi-data/usr/share/doc/pachi-data/changelog.Debian
	install -D -p -m 0644 ChangeLog        debian/pachi-data/usr/share/doc/pachi-data/changelog
	install    -p -m 0644 debian/copyright debian/pachi-data/usr/share/doc/pachi-data
	install -D -p -m 0644 debian/control   debian/pachi-data/DEBIAN/control

	gzip -9n debian/pachi-data/usr/share/doc/pachi-data/changelog*
	cd debian/pachi-data && find usr -type f -print0 | xargs -0 md5sum > DEBIAN/md5sums

	dpkg-gencontrol -ppachi-data -Pdebian/pachi-data
	chown -R root:root debian/pachi-data
	chmod -R go=u-w debian/pachi-data
	find debian/pachi-data -depth -newermt '$(BUILD_DATE)' -print0 | xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build debian/pachi-data ..

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