#!/usr/bin/make -f

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

PYVERS=$(shell pyversions -vs)
RENPY_DEPS_INSTALL="/usr::$(shell dirname "`find -O2 /usr/lib -name libfreetype.so 2>/dev/null`")"

patch:
	dh_testdir
	$(MAKE) -f /usr/share/quilt/quilt.make patch

configure: configure-stamp
configure-stamp:
	dh_testdir
	$(MAKE) -f /usr/share/quilt/quilt.make patch
	chmod 644 `find */ -name "*.py"`
	chmod +x renpy.py
	touch configure-stamp

build-arch: build-stamp

build-indep: build-stamp

build: build-arch build-indep

build: build-stamp
build-stamp: configure $(PYVERS:%=build-python%)
	dh_testdir
	touch build-stamp

build-python%: configure-stamp
	dh_testdir
	export RENPY_DEPS_INSTALL=$(RENPY_DEPS_INSTALL) ; \
		cd module && python$* setup.py build
	touch $@

clean: configure
	dh_testdir
	dh_testroot
	rm -rf `find . -name "*.rpyc"`
	rm -f build-stamp configure-stamp
	rm -f build-python* install-python*
	-export RENPY_DEPS_INSTALL=$(RENPY_DEPS_INSTALL); cd module && python setup.py clean
	rm -rf module/build/

	rm -rfv renpy.app lib jedit
	rm -fv `find . -name "*.exe"`
	rm -fv `find . -name "*.dll"`
	rm -fv `find . -name "*.lib"`
	rm -fv `find . -name "*.pyc"`
	rm -fv `find . -name "*.rpyc"`
	rm -fv `find . -name "*.rpymc"`
	rm -fv `find . -name "*.pyo"`
	rm -fv `find . -name "*.so"`

	$(MAKE) -f /usr/share/quilt/quilt.make unpatch
	dh_clean 

install-indep:
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# common/ - A directory containing common script files that help Ren'Py work.
	dh_install -prenpy common/* usr/share/games/renpy/common/

	# renpy/ - Contains the python source code for Ren'Py.
	dh_install -prenpy  renpy/* usr/share/games/renpy/renpy/
	# data/ - Contains the data for the launcher.
	dh_install -prenpy  launcher/* usr/share/games/renpy/launcher/
	# Scripts that give additional features.
	dh_install -prenpy  template/* usr/share/games/renpy/template/

	dh_install -prenpy-demo tutorial/* usr/share/games/renpy/demo/
	chmod +x debian/renpy-demo.sh
	dh_install -prenpy-demo debian/renpy-demo.sh usr/share/games/renpy/

	dh_install -prenpy-thequestion the_question/* usr/share/games/renpy/the_question/
	chmod +x debian/renpy-the_question.sh
	dh_install -prenpy-thequestion debian/renpy-the_question.sh usr/share/games/renpy/

	dh_install -prenpy  renpy.py usr/share/games/renpy/
	dh_link -prenpy  usr/share/games/renpy/renpy.py usr/games/renpy
	dh_link -prenpy-demo usr/share/games/renpy/renpy-demo.sh usr/games/renpy-demo
	dh_link -prenpy-thequestion usr/share/games/renpy/renpy-the_question.sh usr/games/the_question

	dh_install -prenpy debian/renpy.xpm usr/share/pixmaps
	dh_install -prenpy debian/renpy.desktop usr/share/applications
	dh_install -prenpy-demo debian/renpy-demo.xpm usr/share/pixmaps
	dh_install -prenpy-demo debian/renpy-demo.desktop usr/share/applications
	dh_install -prenpy-thequestion debian/renpy-thequestion.xpm usr/share/pixmaps
	dh_install -prenpy-thequestion debian/renpy-thequestion.desktop usr/share/applications

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/common/DejaVuSans.ttf
	dh_link -prenpy /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf \
		usr/share/games/renpy/common/DejaVuSans.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/DejaVuSans.ttf
	dh_link -prenpy /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf \
		usr/share/games/renpy/launcher/DejaVuSans.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/DejaVuSerif.ttf
	dh_link -prenpy /usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf \
		usr/share/games/renpy/launcher/DejaVuSerif.ttf

	dh_install -prenpy-doc doc/* usr/share/doc/renpy/html

install-arch: build $(PYVERS:%=install-python%)

install-python%:
	dh_testdir
	dh_testroot
	export RENPY_DEPS_INSTALL=$(RENPY_DEPS_INSTALL) ; \
		cd module && python$* setup.py install \
			--no-compile \
			--root=$(CURDIR)/debian/python-renpy \
			;
	touch $@

install: install-arch install-indep

# Build architecture-independent files here.
binary-indep: install-indep
	dh_testdir
	dh_testroot
	rm -fv `find debian -name license.txt`
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installman -prenpy debian/renpy.6
	dh_installman -prenpy-demo debian/renpy-demo.6
	dh_installman -prenpy-thequestion debian/the_question.6
	#dh_installdirs -prenpy usr/share/lintian/overrides/
	#dh_install -prenpy debian/overrides/renpy usr/share/lintian/overrides/
	dh_compress -i --exclude=.js --exclude=.pdf --exclude=.py --exclude=.txt
	dh_fixperms -i
	dh_pysupport -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_pysupport -a
	dh_strip -a
	dh_compress -a --exclude=.js --exclude=.pdf --exclude=.py --exclude=.txt
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build-indep build-arch build binary-indep binary-arch binary install-indep install-arch install
.PHONY: clean configure get-doc get-orig-source
