#!/usr/bin/make -f

include /usr/share/postgresql-common/pgxs_debian_control.mk

export PYTHON=python3
export PYBUILD_NAME=omnidb
export PYBUILD_DISABLE_python2=1

override_dh_install-arch:
	dh_install -a
	+pg_buildext loop omnidb_plugin postgresql-%v-omnidb

override_dh_install-indep:
	dh_install -i
	echo "fonts:Depends=" >> debian/omnidb-common.substvars
	find debian -name '*.eot' -or -name '*.svg' -or -name '*.ttf' -or -name '*.woff*' | LC_ALL=C sort | \
		while read path; do \
			file="$${path##*/}" ; \
			dpkg -S "*/$$file" | grep '^fonts' | LC_ALL=C sort | head -1 | while read pkg pkgpath; do \
				rm $$path; \
				ln -vs $$pkgpath $$path; \
				sed -i -e "/fonts:Depends/ s/$$/$${pkg%:},/" debian/omnidb-common.substvars; \
			done \
		done
	echo "js:Depends=" >> debian/omnidb-common.substvars
	find debian -regextype egrep -regex '.*/(jq|excanvas).*\.(css|js)$$' | LC_ALL=C sort | \
		while read path; do \
			file="$$(echo $${path##*/} | sed -e 's/-[0-9.]*\././')" ; \
			dpkg -S "*/$$file" | grep '^libjs' | LC_ALL=C sort | head -1 | while read pkg pkgpath; do \
				rm $$path; \
				ln -vs $$pkgpath $$path; \
				sed -i -e "/js:Depends/ s/$$/$${pkg%:},/" debian/omnidb-common.substvars; \
			done \
		done
	find debian/omnidb-common/usr/lib \( -name .directory -or -name Thumbs.db \) -delete

override_dh_fixperms-indep:
	dh_fixperms -i
	find debian/omnidb-common/usr/lib -type f -perm /100 | xargs chmod -v -x

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -C omnidb_plugin clean

%:
	dh $@ --with python3
