#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

include /usr/share/quilt/quilt.make


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CXXFLAGS += -O0
#else
#	CXXFLAGS += -O0
#endif

ifeq ($(DEB_BUILD_ARCH), armel)
	CXXFLAGS += -march=armv7-a -mcpu=cortex-a8 -mtune=cortex-a8 -mfpu=neon -mfloat-abi=softfp
endif
export CXXFLAGS


## used for package name extension
BRANCH_VERSION = 1.10

CONFIGURE_SWITCHES = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--disable-strict-compilation --disable-maintainer-mode --disable-rpath \
		--prefix=/opt/wesnoth --mandir=\$${prefix}/share/man --bindir=\$${prefix}/bin \
		--with-datadir-name=wesnoth/$(BRANCH_VERSION) \
		--with-localedir=\$${prefix}/share/games/wesnoth/$(BRANCH_VERSION)/locale \
		--with-fifodir=/var/run/wesnothd --datadir=\$${prefix}/share/games \
		--enable-server --with-fribidi --enable-python-install


CMAKE_SWITCHES = -DCMAKE_INSTALL_PREFIX="/opt/wesnoth/" \
		-DBINDIR="/opt/wesnoth/bin" -DDATAROOTDIR="/opt/wesnoth/share/games" \
		-DDATADIRNAME="wesnoth/$(BRANCH_VERSION)" \
		-DDESKTOPDIR="/usr/share/applications" \
		-DDOCDIR="/opt/wesnoth/share/doc/wesnoth-$(BRANCH_VERSION)-data" \
		-DLOCALEDIR="locale" -DMANDIR="/opt/wesnoth/share/man" -DUSE_ANA_NETWORK=OFF \
		-DCMAKE_VERBOSE_MAKEFILE=ON -DENABLE_OMP=OFF -DCMAKE_BUILD_TYPE=release \
		-DENABLE_SERVER=OFF -DENABLE_STRICT_COMPILATION=OFF \
		-DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/debian/maemo.cmake


configure: debian/stamp-configure
debian/stamp-configure: patch
	dh_testdir
	# Add here commands to configure the package.
	#./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/opt/wesnoth --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info LDFLAGS="-Wl,-z,defs" CXXFLAGS="$(CXXFLAGS)" \
	#	--disable-editor #--enable-lowmem --enable-tinygui
	mkdir -p build
	cd build && cmake .. $(CMAKE_SWITCHES)
	touch $@

build: build-arch build-indep
build-arch: debian/stamp-build
build-indep: debian/stamp-build

debian/stamp-build: debian/stamp-configure
	dh_testdir

	# Add here commands to compile the package.
	#$(MAKE)
	$(MAKE) -C build
	#docbook-to-man debian/wesnoth.sgml > wesnoth.1

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f debian/stamp-*
	# Add here commands to clean up after the build process.
	rm -rf build locale # locale doesn't get generated inside build 

	dh_clean 

install: debian/stamp-install
debian/stamp-install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/wesnoth.
	#$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	$(MAKE) -C build DESTDIR=$(CURDIR)/debian/tmp install

	#mkdir -p "$(CURDIR)/debian/wesnoth/usr/share/applications/hildon/"
	#cp -a "$(CURDIR)/icons/wesnoth.desktop" "$(CURDIR)/debian/wesnoth/usr/share/applications/hildon/wesnoth.desktop"

	cd $(CURDIR)/debian/tmp/opt/wesnoth/share/games/wesnoth/$(BRANCH_VERSION)/data/tools \
		&& chmod +x extractbindings unit_tree/TeamColorizer \
		wesnoth/wescamp.py wesnoth/wmldata.py wesnoth/wmlparser.py \
		wmlindent wmlflip wmllint wmlscope wesnoth_addon_manager \
		wmlvalidator hexometer/hexometer wmlxgettext imgcheck


	dh_install

	touch $@ 

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installexamples
	dh_install
#	dh_installmenu
#	dh_lintian
#	dh_pysupport
#	dh_installinit -u 'stop 20 0 1 2 3 4 5 6 .'
#	dh_installman
#	dh_strip --dbg-package=wesnoth-$(BRANCH_VERSION)-dbg
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol 
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: install

	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common


# Build architecture dependant packages using the common target.
binary-arch: install

	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

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