#!/usr/bin/make -f

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

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS2:=-Wl,-z,defs  -Wl,-as-needed -Wl,--no-undefined

config: config-stamp
config-stamp: 
	dh_testdir
	[ ! -d debian/patches ] || $(MAKE) -f /usr/share/quilt/quilt.make patch

#	# Look for unpatched relative directories
#	grep -r \"models/ * | grep -v debian/
#	grep -r \"textures/ * | grep -v debian/
#	grep -r \"levels/ * | grep -v debian/
#	grep -r \"GLSL/ * | grep -v debian/
#	grep -r \"fonts/ * | grep -v debian/
#	grep -r \"music/ * | grep -v debian/
#	grep -r \"sounds/ * | grep -v debian/
#	grep -r \"pony.options\" * | grep -v debian/
#	grep -r \"levels.xml\" * | grep -v debian/

	sed -e 's|:CFLAGS:|$(CFLAGS) $(CPPFLAGS)|g' \
		-e 's|:LDFLAGS:|$(LDFLAGS) $(LDFLAGS2)|g' \
		< debian/SConstruct > SConstruct.Debian
	mkdir -p lib
	touch $@

build-stamp: config
	dh_testdir
	protoc src/mesh.proto --cpp_out=lib/
	scons -f SConstruct.Debian \
		prefix=/usr \
		resources_dir=/usr/share/pink-pony \
		lib_dir="$(CURDIR)"
	touch $@

build-indep: build-stamp

build-arch: build-stamp

build: build-arch build-indep

clean: 
	dh_testdir
	dh_testroot
	rm -fv build-stamp config-stamp
	-scons -c -f SConstruct.Debian
	rm -fv SConstruct.Debian
	rm -fv config.log options.cache
	rm -fv lib/mesh.pb.cc lib/mesh.pb.h
	rm -fv lib/src/mesh.pb.cc lib/src/mesh.pb.h
	rm -rfv .sconf_temp build
	rm -rfv .sconsign.dblite screen0.tga
	rm -fv `find . -name "*.o"`

	[ ! -d debian/patches ] || $(MAKE) -f /usr/share/quilt/quilt.make unpatch
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	mkdir -p "$(CURDIR)"/debian/pink-pony/usr/lib/pink-pony/
	cp Pony "$(CURDIR)"/debian/pink-pony/usr/lib/pink-pony/pink-pony.bin
	mkdir -p "$(CURDIR)"/debian/pink-pony/usr/games/
	cp install/pink-pony "$(CURDIR)"/debian/pink-pony/usr/games/
	chmod +x "$(CURDIR)"/debian/pink-pony/usr/games/pink-pony


# Build architecture-independent files here.
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_install -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	[ ! -e /usr/bin/dh_buildinfo ] || dh_buildinfo -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_installmenu -a
	dh_installman -a
	dh_link -a
	dh_strip --dbg-package=pink-pony-dbg -a
	dh_compress -a
	dh_fixperms -a
	[ ! -e /usr/bin/dh_buildinfo ] || dh_buildinfo -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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