#!/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
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

RENPY_DEPS_INSTALL="/usr::/usr/lib/$(DEB_HOST_MULTIARCH)"

%:
	dh $@ --parallel --with python2

override_dh_install-indep:
	chmod +x renpy.py
	dh_install
	#Fix Privacy Issues
	cd $(CURDIR)/debian/renpy-doc/usr/share/doc/renpy/html/ && \
		find . -type f -name "*.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

override_dh_auto_build:
	find */ -name "*.py" | while read F; do chmod --verbose 644 "$$F"; done
	export RENPY_DEPS_INSTALL=$(RENPY_DEPS_INSTALL) ; \
		CFLAGS="$(CFLAGS) $(CPPFLAGS)" \
		LDFLAGS="$(LDFLAGS) $(LDFLAGS2)" \
		cd module && python$* setup.py build

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

override_dh_compress:
	dh_compress --exclude=.js --exclude=.pdf --exclude=.py --exclude=.txt

override_dh_clean:
	dh_clean
	rm -fv `find . -name "*.pyc"`
	rm -fv `find . -name "*.so"`
	rm -fv `find . -name "*.o"`
	rm -rf module/build/
	rm -fv module/gen/renpy.style*.c
	rm -fv module/gen/renpy.style*.html
	rm -fv module/gen/style*.html
	rm -fv module/gen/*.cc
	rm -fv module/gen/*.pyx
	rm -fv module/gen/*.pxi

get-orig-source:
	uscan --download-current-version --force-download --repack --compression xz
