#!/usr/bin/make -f

PRG_SUFFIX=0.9

# 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) -Wl,-z,defs -Wl,-as-needed

CONFIGURE_OPTIONS:= \
		$(CROSS) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		CPPFLAGS="$(CPPFLAGS)" \
		CFLAGS="$(CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS)" \
		LDFLAGS="$(LDFLAGS)"

configure: configure-stamp
configure-stamp: 
	dh_testdir

	libtoolize
	dh_autoreconf

	if pkg-config luajit ; then echo + Using luagit ; \
		./configure $(CONFIGURE_OPTIONS) --with-lua=luajit ; \
	else echo + Using lua ; \
		./configure $(CONFIGURE_OPTIONS) --with-lua=lua ; \
	fi

	cd src/scripts/ && lua auto.lua boot graphics
	touch $@

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	[ ! -f demos/Makefile ] || $(MAKE) -C demos
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp  
	dh_testdir
	touch $@

build: build-indep build-arch

clean: 
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f demos/Makefile ] || $(MAKE) -C demos clean
	#rm -fv src/modules/graphics/opengl/GLee.*
	rm -fv config.sub config.guess config.log config.cache
	rm -fv *-stamp
	rm -fv bin/*
	rm -rfv obj/*
	rm -fv `find . -name "*.o"`
	rm -fv configure Makefile.in src/Makefile.in aclocal.m4
	rm -fv src/scripts/boot.lua.h src/scripts/graphics.lua.h
	#rm -rfv platform/
	dh_autoreconf_clean
	dh_clean 

install-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installdirs -a
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	mv debian/tmp/usr/bin/love debian/tmp/usr/bin/love-$(PRG_SUFFIX)

install-indep: build-indep-stamp
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs -i

install: install-indep install-arch

binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i
#	dh_installmenu -i
	dh_installmime -i
#	dh_installinfo -i
	dh_lintian -i
	dh_link -i
	dh_compress -i --exclude=.love --exclude=.lua
	dh_fixperms -i
	test -e /usr/bin/dh_buildinfo && dh_buildinfo -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs changes.txt -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	mkdir -p debian/love/usr/share/binfmts/
	cp debian/love.binfmt debian/love/usr/share/binfmts/love
#	dh_installmenu -a
	dh_installmime -a
#	dh_installinfo -a
	dh_installman debian/love-$(PRG_SUFFIX).1 -a
	dh_lintian -a
	dh_link -a
#	dh_link -plove-dev usr/lib/$(DEB_HOST_MULTIARCH)/liblove.so.0.0.0 usr/lib/$(DEB_HOST_MULTIARCH)/liblove.so
	dh_strip --dbg-package=love-dbg -a
	dh_compress -a --exclude=.love --exclude=.lua
	dh_fixperms -a
#	dh_makeshlibs -a
	test -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: build build-indep build-arch
.PHONY: binary binary-indep binary-arch
.PHONY: install install-indep inatall-arch configure
.PHONY: clean configure
