#!/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

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
	find */ -name "*.py" | while read F; do chmod --verbose 644 "$$F"; done
	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) ; \
		CFLAGS="$(CFLAGS) $(CPPFLAGS)" \
		LDFLAGS="$(LDFLAGS) $(LDFLAGS2)" \
		cd module && python$* setup.py build
	touch $@

clean: configure
	dh_testdir
	dh_testroot

	rm -rf module/build/

	rm -f build-stamp configure-stamp
	rm -f build-python* install-python*

	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"`

	rm -fv module/gen/renpy.style*.c
	rm -fv module/gen/renpy.style*.html
	rm -fv module/gen/style*.html

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

	rm -fv \
		module/gen/styleconstants.pxi \
		module/gen/stylepropertyfunctions.pxi \
		module/gen/stylesets.pxi \
		module/gen/styleproperties.pxi

	dh_clean 

install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs

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

	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

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/game/Roboto-Light.ttf
	dh_link -prenpy /usr/share/fonts/truetype/roboto/Roboto-Light.ttf \
		usr/share/games/renpy/launcher/game/Roboto-Light.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/game/Roboto-Regular.ttf
	dh_link -prenpy /usr/share/fonts/truetype/roboto/Roboto-Regular.ttf \
		usr/share/games/renpy/launcher/game/Roboto-Regular.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/game/tl/japanese/MTLc3m.ttf
	dh_link -prenpy /usr/share/fonts/truetype/motoya-l-cedar/MTLc3m.ttf \
		usr/share/games/renpy/launcher/game/tl/japanese/MTLc3m.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/launcher/game/tl/korean/NanumGothic.ttf
	dh_link -prenpy /usr/share/fonts/truetype/nanum/NanumGothic.ttf \
		usr/share/games/renpy/launcher/game/tl/korean/NanumGothic.ttf

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

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/templates/japanese/game/tl/None/MTLc3m.ttf
	dh_link -prenpy /usr/share/fonts/truetype/motoya-l-cedar/MTLc3m.ttf \
		usr/share/games/renpy/templates/japanese/game/tl/None/MTLc3m.ttf

	rm -f $(CURDIR)/debian/renpy/usr/share/games/renpy/templates/korean/game/tl/None/NanumGothic.ttf
	dh_link -prenpy /usr/share/fonts/truetype/nanum/NanumGothic.ttf \
		usr/share/games/renpy/templates/korean/game/tl/None/NanumGothic.ttf

	rm -f $(CURDIR)/debian/renpy-demo/usr/share/games/renpy/demo/game/tl/japanese/MTLc3m.ttf
	dh_link -prenpy-demo /usr/share/fonts/truetype/motoya-l-cedar/MTLc3m.ttf \
		usr/share/games/renpy/demo/game/tl/japanese/MTLc3m.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/

	#Fix Privacy Issues
	cd $(CURDIR)/debian/renpy-doc/usr/share/doc/renpy/html/ && \
		ls *.html | while read f; do \
			cp "$$f" "$$f.orig"; \
			cat "$$f.orig" \
				| awk -vnum=10 '/http:\/\/www.google.com\/jsapi/{for(i=0;i<=num;i++)getline}1' > "$$f"; \
		done; rm *.orig

	dh_compress -i --exclude=.js --exclude=.pdf --exclude=.py --exclude=.txt
	dh_fixperms -i
	dh_python2 -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_python2 -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
