#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

LDFLAGS2:=-Wl,-z,defs  -Wl,-as-needed -Wl,--no-undefined
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

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

%:
	dh $@ --with sphinxdoc


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_install-indep:
	chmod +x renpy.py
	# Convert hicolor MacOS icons to png
	icns2png -x -s 512x512 -d 32 launcher/icon.icns
	mv icon_512x512x32.png renpy.png
	icns2png -x -s 256x256 -d 32 the_question/icon.icns
	mv icon_256x256x32.png renpy-thequestion.png
	dh_install

override_dh_auto_build:
	find */ -name "*.py" ! -perm 644 | while read F; do chmod --verbose 644 "$$F"; done
	export RENPY_DEPS_INSTALL=$(RENPY_DEPS_INSTALL) ; \
		CFLAGS="$(CFLAGS) $(CPPFLAGS)" \
		LDFLAGS="$(LDFLAGS) $(LDFLAGS2)" \
		cd module && python3 setup.py build
	export http_proxy=127.0.0.1:9
	PYTHONPATH=. sphinx-build -N -bhtml sphinx/source/ build/html # HTML generator

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

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

execute_after_dh_clean:
	find . -name "*.pyc" -delete
	find . -name __pycache__ -empty -delete
	find . -name "*.so" -delete
	find . -name "*.o" -delete
	rm -rf module/build/
	rm -fv module/gen/*
	rm -fv renpy.png renpy-thequestion.png