#!/usr/bin/make -f
# -*- makefile -*-

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

export PYBUILD_NAME = pycountry
export PYBUILD_TEST_PYTEST = 1
export PYBUILD_TEST_ARGS = --no-cov $(CURDIR)/src/pycountry/tests


%:
	dh $@ --with python3 --buildsystem=pybuild

execute_before_dh_auto_build:
	# The data provided by the iso-codes package is not within this
	# source package; reassemble the sources
	# poetry insists on real files not strange out-of-module symlinks when
	# building, but we want to end up with symlinks back to iso-codes
	# in the package
	echo "# AUTOGENERATED FROM d/rules" >> debian/links

	# json databases
	mkdir -p src/pycountry/databases
	for f in /usr/share/iso-codes/json/iso*; do \
	  src="$$f"; \
	  part=`echo "$$f" | sed s@/usr/share/iso-codes/json/@@ | tr -d _`; \
	  srcdest=src/pycountry/databases/"$$part"; \
	  debdest=usr/lib/python3/dist-packages/pycountry/databases/"$$part"; \
	  cp "$$src" "$$srcdest"; \
	  echo "$$src" "$$debdest" >> debian/links; \
	done

	# locales data
	mkdir -p src/pycountry/locales
	dpkg -L iso-codes | \
	  grep /usr/share/locale/ | \
	  xargs dirname | \
	  uniq | \
	  sed "s@/usr/share/locale/@@" | \
	  xargs -I{} -n1 mkdir -p src/pycountry/locales/{}
	for f in `dpkg -L iso-codes | grep /usr/share/locale/`; do \
	  if [ -f "$$f" ]; then \
	    src="$$f"; \
	    part=`echo "$$f" | sed "s@/usr/share/locale/@@;s@iso_@iso@"`; \
	    srcdest=src/pycountry/locales/"$$part"; \
	    debdest=/usr/lib/python3/dist-packages/pycountry/locales/"$$part"; \
	    cp "$$src" "$$srcdest"; \
	    echo "$$src" "$$debdest" >> debian/links; \
	  fi; \
	done

	echo "RECONSTRUCTED PACKAGE:"
	find src/pycountry

execute_after_dh_auto_install:
	rm -vrf $(CURDIR)/debian/python*-pycountry/usr/lib/python*/*-packages/pycountry/*.txt
