#!/usr/bin/make -f

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

CFOPTIMIZE = -O2
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFOPTIMIZE = -O0
endif
ifneq (,$(shell gcc --version|grep 'gcc (.*) 4\.4\.'))
  ifeq ($(DEB_HOST_ARCH),powerpc)
	# g++-4.4 has an ICE there.
	CFOPTIMIZE = -O0
  endif
endif

tree-stamp:
	dh_testdir
	mkdir build-console
	cp -ldpR docs settings source build-console/
	rm -rf build-console/rltiles/*
	mkdir build-tiles
	cp -ldpR docs settings source build-tiles/
	touch tree-stamp

# The makefile is unorthodox, requiring all options to be specified on every invocation.
ARGS_CONSOLE = prefix=/usr CFOPTIMIZE=$(CFOPTIMIZE) WIZARD=y
ARGS_TILES   = $(ARGS_CONSOLE) TILES=y GAME=crawl-tiles \
	PROPORTIONAL_FONT=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf \
	MONOSPACED_FONT=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf

build: build-stamp
build-stamp: tree-stamp
	dh_testdir

	cd build-console/source && $(MAKE) $(ARGS_CONSOLE)
	cd build-tiles/source && $(MAKE) $(ARGS_TILES)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f build-stamp tree-stamp
	rm -rf build-console build-tiles

	git clean -dfX || true

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	cd build-console/source && $(MAKE) $(ARGS_CONSOLE) install DESTDIR=../../debian/crawl-common
	#cd debian/crawl-common/usr/share && mkdir -p doc/crawl-common && \
	#	mv crawl/docs/* doc/crawl-common && \
	#	mv doc/crawl-common/crawl_manual.txt crawl/docs
	rm -rf debian/crawl-common/usr/share/crawl/docs/license
	mkdir -p debian/crawl/usr/
	mv debian/crawl-common/usr/games debian/crawl/usr/games
	cd build-tiles/source && $(MAKE) $(ARGS_TILES) install DESTDIR=../../debian/crawl-tiles
	rm -rf debian/crawl-tiles/var
	cd debian/crawl-tiles/usr/share/crawl/ && rm -rf docs settings
	cd debian/crawl-tiles/usr/share/crawl/dat && rm -rf des clua database descript lua
	
	mkdir -p debian/crawl/usr/share/man/man6
	cp docs/crawl.6 debian/crawl/usr/share/man/man6/
	mkdir -p debian/crawl-tiles/usr/share/man/man6
	cp docs/crawl.6 debian/crawl-tiles/usr/share/man/man6/crawl-tiles.6
	mkdir -p debian/crawl-common/usr/share/doc/crawl-common/examples
	cp settings/init.txt debian/crawl-common/usr/share/doc/crawl-common/examples/crawlrc
	ln -sf /usr/share/crawl/docs/*.txt debian/crawl-common/usr/share/doc/crawl-common/

	mkdir -p debian/crawl/usr/share/doc
	ln -sf crawl-common debian/crawl/usr/share/doc/crawl
	mkdir -p debian/crawl-tiles/usr/share/doc
	ln -sf crawl-common debian/crawl-tiles/usr/share/doc/crawl-tiles
	dh_install -pcrawl-tiles debian/crawl-tiles.desktop usr/share/applications
	dh_install -pcrawl-tiles debian/crawl.png usr/share/icons/hicolor/48x48/apps
	dh_link -pcrawl-tiles usr/share/games/crawl/dat/tiles/stone_soup_icon-32x32.png \
		usr/share/icons/hicolor/32x32/apps/crawl.png

# Build architecture-independent files here.
binary-indep:
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -pcrawl-common -Xcrawl_manual.txt -Xaptitudes.txt -Xquickstart.txt -Xmacros_guide.txt -Xoptions_guide.txt -Xtiles_help.txt
	dh_installchangelogs -pcrawl-common docs/changelog.txt
	dh_installmenu
	dh_link
	dh_strip
	dh_compress -Xaptitudes.txt
	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
