#!/usr/bin/make -f

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

CFLAGS = -Wall -g

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

# Use hardware floating point
ifneq (,$(findstring vfp,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -mfpu=vfp -mfloat-abi=softfp -mcpu=arm1136jf-s -mtune=arm1136jf-s
endif


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

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	$(MAKE) -f /usr/share/quilt/quilt.make patch
	cat debian/options.cache | sed -e 's/:CFLAGS:/$(CFLAGS)/g' > options.cache
	touch $@

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	CXXFLAGS="${CFLAGS}" scons prefix=/usr resources_dir=/usr/share/games/pingus lib_dir=$(CURDIR) with_opengl=no
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-scons -c
	rm -f `find . -name "*.o"`
	rm -f `find . -name "*.os"`
	rm -f `find . -name "*.so"`
	rm -f config.log options.cache bt.xml .sconsign.dblite
	rm -rf .sconf_temp build/release
	$(MAKE) -f /usr/share/quilt/quilt.make unpatch
	find -type f -name .sconsign -exec rm -f '{}' \;
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs

# Build architecture-independent files here.
binary-indep:
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i --exclude=pingus.6

	mkdir -p "$(CURDIR)/debian/pingus-data/usr/share/games/pingus/"
	find data/ \
		-type f -a  \( \
		-name "*.png" -o \
		-name "*.jpg" -o \
		-name "*.wav" -o \
		-name "*.scm" -o \
		-name "*.font" -o \
		-name "*.it" -o \
		-name "*.ogg" -o \
		-name "*.s3m" -o \
		-name "*.po" -o \
		-name "*.worldmap" -o \
		-name "*.res" -o \
		-name "*.pingus" -o \
		-name "*.levelset" \
		\) -exec cp --parents -v {} "$(CURDIR)/debian/pingus-data/usr/share/games/pingus/" \;

	dh_install -i
	chown -R user.root debian/pingus-data/usr/share/games/pingus/*
	dh_compress -i
	dh_fixperms -i --exclude /usr/share/games/
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i


# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installchangelogs -a ChangeLog
#	dh_installdocs -a --exclude=pingus.6
#	dh_installexamples -a
	dh_install -a
#	dh_installmenu -a
#	dh_installman -a doc/pingus.6
	dh_desktop -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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