#!/usr/bin/make -f

VERSION = 2.0
#REVISION = 32731

#Cannot include hardening flags as jam segfaults with -foo=bar type switches
# so manually add what we can for now.
#DPKG_EXPORT_BUILDFLAGS = 1
#include /usr/share/dpkg/buildflags.mk
CFLAGS+=-Wall -g -O2 -fstack-protector

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH),powerpc)
	CFLAGS+=-Wa,-mregnames
endif

COMPILER_FLAGS = CFLAGS='$(CFLAGS)'

%:
	dh $@

override_dh_auto_configure:
#	sh bin/autogen.sh # This can be removed in crystalspace 1.9
	sh configure $(COMPILER_FLAGS) --prefix=/usr --sysconfdir=/etc --enable-cpu-specific-optimizations=no --enable-shared

override_dh_auto_install:
	$(MAKE) DESTDIR=$(CURDIR)/debian/libcrystalspace-2.0 install_lib install_plugin
	$(MAKE) DESTDIR=$(CURDIR)/debian/libcrystalspace-dev install_include
	$(MAKE) DESTDIR=$(CURDIR)/debian/crystalspace install_bin
	$(MAKE) DESTDIR=$(CURDIR)/debian/crystalspace-data install_data install_config
	$(MAKE) DESTDIR=$(CURDIR)/debian/crystalspace-doc install_doc

	# Move /usr/bin/cs-config* files to libcrystalspace-dev package
	mkdir -p $(CURDIR)/debian/libcrystalspace-dev/usr/bin && \
		mv $(CURDIR)/debian/crystalspace/usr/bin/cs-config* \
		$(CURDIR)/debian/libcrystalspace-dev/usr/bin

	# Remove useless files from crystalspace-data package
	-rm -rf $(CURDIR)/debian/crystalspace-data/usr/share/crystalspace-*/build

	# Move .dbg files from /usr/lib to libcrystalspace-dbg package
	mkdir -p $(CURDIR)/debian/libcrystalspace-dbg/usr/lib/debug/usr/lib && \
		mv $(CURDIR)/debian/libcrystalspace-2.0/usr/lib/*.dbg \
		$(CURDIR)/debian/libcrystalspace-dbg/usr/lib/debug/usr/lib

	# Remove .dbg files generated during build
	rm $(CURDIR)/debian/crystalspace/usr/bin/*.dbg
	rm $(CURDIR)/debian/libcrystalspace-2.0/usr/lib/crystalspace-2.0/*.dbg

	# Include other krystal data files in crystalspace-data package
	mkdir -p $(CURDIR)/debian/crystalspace-data/usr/share/crystalspace-2.0/data/krystal/mocap 
	cp $(CURDIR)/data/krystal/mocap/* \
		$(CURDIR)/debian/crystalspace-data/usr/share/crystalspace-2.0/data/krystal/mocap/

	# Fix extra-license-file lintian warnings:
	-rm $(CURDIR)/debian/crystalspace-doc/usr/share/doc/crystalspace-*/LICENSE

	# Fix script-not-executable lintian warning:
	#chmod 755 $(CURDIR)/debian/crystalspace-data/usr/share/crystalspace-*/bindings/perl5/perlsimp.pl

override_dh_auto_test:
	# skipping tests

override_dh_makeshlibs:
	dh_makeshlibs -V 'libcrystalspace-2.0 (>= 1.4.0+dfsg-4)'

#get-orig-source:
#	@cd $(CURDIR); \
#	svn co -r ${REVISION} https://crystal.svn.sourceforge.net/svnroot/crystal/CS/branches/release/V1.4 \
#		crystalspace-${VERSION}~svn${REVISION}; \
#	tar --exclude=*.svn -cf ../crystalspace_${VERSION}~svn${REVISION}.orig.tar \
#		crystalspace-${VERSION}~svn${REVISION}; \
#	rm -rf crystalspace-${VERSION}~svn${REVISION}; \
#	gzip -9nf ../crystalspace_${VERSION}~svn${REVISION}.orig.tar;

get-orig-source:
	@cd $(CURDIR); \
	wget http://www.crystalspace3d.org/downloads/release/crystalspace-src-${VERSION}.tar.bz2; \
	tar fjx crystalspace-src-${VERSION}.tar.bz2; \
	rm -f crystalspace-src-${VERSION}.tar.bz2; \
	mv crystalspace-src-${VERSION} crystalspace-${VERSION}+dfsg; \
	tar --exclude=data/castle* -cf ../crystalspace_${VERSION}+dfsg.orig.tar crystalspace-${VERSION}+dfsg; \
	rm -fr crystalspace-${VERSION}+dfsg; \
	gzip -9fn ../crystalspace_${VERSION}+dfsg.orig.tar
