#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DICT_LANG = $(lastword $(subst -, ,$(DEB_SOURCE)))
DEB_DESTDIR = debian/$(DEB_SOURCE)

%:
	dh $@

override_dh_auto_configure:
	LC_ALL=C sed \
		-e 's/[ \t][a-z][a-z]:.*//' \
		/usr/share/hunspell/sk_SK.aff > sk_affix.dat
	# remove the word count (the first line)
	# remove words with spaces, but not possible hunspell morphological data
	# remove all tabulation characters
	# remove hunspell morphological data
	# remove words with dashes, aspell doesn’t support those
	# remove words with French accents, fall outside of the charset
	LC_ALL=C sed \
		-e 1d \
		-e 's/[ \t][a-z][a-z]:.*//' \
		-e 's/\t//g' \
		-e '/ /d' \
		-e '/-/d' \
		-e '/à/d' \
		-e '/ê/d' \
		/usr/share/hunspell/sk_SK.dic > sk.wl
	prezip -sz sk.wl

override_dh_md5sums:
	dh_md5sums -Xvar/lib/aspell

override_dh_auto_install:
	for f in *.cwl; do \
	    gzip -9 -c "$$f" > "$(DEB_DESTDIR)/usr/share/aspell/"$$f".gz"; \
	    WL=`echo $$f | sed 's/\.cwl$$//'`; \
	    touch "$(DEB_DESTDIR)/var/lib/aspell/$$WL.rws"; \
	    dh_link "var/lib/aspell/$$WL.rws" "usr/lib/aspell/$$WL.rws"; \
	    echo "$$WL" >> "$(DEB_DESTDIR)/usr/share/aspell/$(DICT_LANG).contents"; \
	done
	
	LC_ALL=C sort -o $(DEB_DESTDIR)/usr/share/aspell/$(DICT_LANG).contents \
		$(DEB_DESTDIR)/usr/share/aspell/$(DICT_LANG).contents
	
	touch $(DEB_DESTDIR)/var/lib/aspell/$(DICT_LANG).compat
	
	installdeb-aspell


override_dh_gencontrol: package=hunspell-sk
override_dh_gencontrol: pkgname=$(shell dpkg-query -W -f='$${source:Package}' $(package))
override_dh_gencontrol: pkgver=$(shell dpkg-query -W -f='$${source:Version}' $(package))
override_dh_gencontrol:
	echo "$(package):Built-Using=$(pkgname) (= $(pkgver))" >> debian/$(DEB_SOURCE).substvars
	dh_gencontrol -- "-v$(pkgver)+$(DEB_VERSION_UPSTREAM_REVISION)"
