#!/usr/bin/make -f
# -*- makefile -*-
# Copyright 2004, 2006 Yann Dirson.
# Based on debhelper sample by Joey Hess.

export LC_ALL=C
export DH_OPTIONS

CFLAGS = -Wall -g

# let's do debugging
#export DEB_BUILD_OPTIONS=nostrip

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

MAINVER := $(shell dpkg-parsechangelog | sed -ne 's/^Version: *\([^~-]*\).*/\1/p' )
SVNREV := $(shell dpkg-parsechangelog | sed -ne 's/^Version: .*~svn\([^-]*\).*/\1/p' )
SVNBRANCH = branches/ufoai_2.3
TMPDIR = /tmp/
ORIGDIR = ufoai_$(MAINVER)~svn$(SVNREV).orig
ORIGTGZ = ufoai_$(MAINVER)~svn$(SVNREV).orig.tar.gz
DEBTGZ  = ufoai_$(MAINVER)~svn$(SVNREV).debian.tar.gz

../$(ORIGTGZ):
	svn export -r $(SVNREV) \
	https://ufoai.svn.sourceforge.net/svnroot/ufoai/ufoai/$(SVNBRANCH) \
	$(TMPDIR)/$(ORIGDIR)
	-rm -rf $(TMPDIR)/$(ORIGDIR)/contrib
	-rm -rf $(TMPDIR)/$(ORIGDIR)/debian
	cd $(TMPDIR) ; tar -zcf $(ORIGTGZ) $(ORIGDIR) ; cd $(CURDIR)
	rm -rf $(TMPDIR)/$(ORIGDIR)
	mv $(TMPDIR)/$(ORIGTGZ) $(CURDIR)/..

get-orig-source: ../$(ORIGTGZ)

debtar: clean
	dh_testdir
	tar -zcf ../$(DEBTGZ) debian/

config.status:
	dh_testdir
	./configure --with-curses=no --datadir=/usr/share/games --libdir=/usr/lib/games --prefix=/usr

build-arch: stamps/build-arch
stamps/build-arch: config.status
	dh_testdir

	$(MAKE)
	$(MAKE) uforadiant

	mkdir -p stamps
	touch $@

build-indep: stamps/build-indep
stamps/build-indep: config.status
	dh_testdir

	$(MAKE) installer-pre pdf-manual

	mkdir -p stamps
	touch $@

patch: patch-stamp
stamps/patch-stamp:
	dpatch apply-all

unpatch:
	dpatch deapply-all
	rm -rf stampts/patch-stamp

really_clean:
	dh_testdir
	dh_testroot
	rm -rf stamps

	[ ! -f Makefile ] || $(MAKE) clean
	$(MAKE) -C src/docs/tex clean
	# WARNING: building maps takes ages
	# find base/maps -name '*.bsp' | xargs --no-run-if-empty rm
	-rm -f base/*.pk3
	-rm -f Makefile config.h config.log config.status
	-rm -rf base/i18n
	-rm -rf debian/files debian/substvars

	dh_clean

clean: really_clean unpatch

install-arch: stamps/build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_install

install-indep: stamps/build-indep
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_install

# No architecture-independent files here.
binary-indep: DH_OPTIONS=-i
binary-indep: install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_link
	dh_compress -X.pdf
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS=-a
binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installmime
	dh_installman
	dh_link
	dh_strip
	dh_compress -X.pdf
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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