#! /usr/bin/make -f
# -*- makefile -*-

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

# Use this variable to define the particular version of cegui we're building.
CEGUI_VERSION=0.7.2

# These are from the autotools-dev package documentation.
# /usr/share/doc/autotools-dev/README.Debian.gz
# from the section titled "Calling GNU configure properly"
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Default CXXFLAGS to use.
DEB_CXXFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
   DEB_CXXFLAGS += -O0
else
   DEB_CXXFLAGS += -O2
endif

# Include a CEGUI_CONFIG_OPTIONS variable that has the default configure options
# used to build this package. This variable can be overridden.
CEGUI_CONFIG_OPTIONS ?=  $(confflags) \
	--prefix=/usr \
	--host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--enable-lua-module=yes \
	--enable-toluacegui=yes \
	--with-default-xml-parser=TinyXMLParser \
	--disable-ogre-renderer \
	--disable-samples \
	CXXFLAGS="$(DEB_CXXFLAGS)"

build-setup: build-setup-stamp
build-setup-stamp:
	dh_testdir

patch: patch-stamp
patch-stamp:
	dh_testdir
	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	touch patch-stamp

configure: build-setup patch configure-stamp
configure-stamp:
	dh_testdir
	./bootstrap
	./configure $(CEGUI_CONFIG_OPTIONS)
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE)
	$(MAKE) -C doc/doxygen html
	touch build-stamp

clean: clean-stamp unpatch reverse-build-setup
clean-stamp:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) clean distclean
	rm -f config.log
	rm -f configure-stamp build-stamp
	dh_clean

unpatch:
	dh_testdir
	QUILT_PATCHES=debian/patches quilt pop -a -R || test $$? = 2
	rm -f patch-stamp

reverse-build-setup:
	dh_testdir
	rm -f debian/libcegui-mk2-$(CEGUI_VERSION).install \
		debian/libcegui-mk2-$(CEGUI_VERSION).manpages

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	install -d debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

# Build architecture-independent files here.
binary-indep: install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_install -i
	dh_fixperms -i
	dh_compress -i -plibcegui-mk2-1
	dh_compress -plibcegui-mk2-doc -X.txt
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=debian/tmp
	dh_strip -plibcegui-mk2-1 --dbg-package=libcegui-mk2-1-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

get-orig-source:
#	$(dir $_)cegui-get-orig-source

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