#!/usr/bin/make -f
#

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

package=iitalian

%:
	dh $@

override_dh_auto_build:
	buildhash italian.words italian.aff italian.hash

override_dh_auto_clean:
	-rm -f build
	-rm -f italian.hash italian.stat
	-rm -f `find . -name "*~"`

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.txt

override_dh_installdebconf:
	installdeb-ispell  # This calls internally dh_installdebconf

HUNSPELL=it_IT
LANGUAGE=italian

.ONESHELL:
get-orig-source:
	# Idea taken from https://anoncvs.postgresql.org/cvsweb.cgi/pgsql/contrib/tsearch2/my2ispell/Attic/Makefile?rev=1.1.4.1;content-type=text%2Fplain
	if ! which unmunch >/dev/null ; then
	    echo "You need to 'apt install hunspell-tools' to create the ispell database"
	    exit 1
	fi
	mkdir tmp
	cp -a debian/$(LANGUAGE).aff tmp
	cd tmp
	apt source libreoffice-dictionaries
	# This .aff file is too complex for ispell
	# cp -a libreoffice-dictionaries*/dictionaries/it_IT/it_IT.aff .
	cp -a libreoffice-dictionaries*/dictionaries/it_IT/it_IT.dic .
	cp -a libreoffice-dictionaries*/dictionaries/it_IT/CHANGELOG.txt .
	cp -a libreoffice-dictionaries*/dictionaries/it_IT/README_it_IT.txt .
	rm -rf libreoffice-dictionaries*
	grep -v -E '^[[:digit:]]+$$' $(HUNSPELL).dic \
	 | grep -v '\.' \
	 | sed -e 's/"//g' \
	 | perl -pi -e 's|/(\S+)| $$q=$$1; ( $$q=~/[\\_`]/ && $$q!~/z/ ) ? "/$${q}z" : "/$${q}"|e' \
	 | sort \
	> $(LANGUAGE).dict
	# This is too complex
	#grep -v -i zyzyzy $(HUNSPELL).aff \
	# | grep -v -i zyzyzy \
	# | perl -pi \
	#	-e 's/^COMPOUNDFLAG\s+(\S+)/compoundwords controlled $$1/;' \
	#        -e 's/^COMPOUNDMIN\s+(\d+)/compoundmin $$1/;' \
	#        -e 's/^PFX\s+(\S+)\s+([YN])\s+\d+.*$$/ if ( !$$wasprf ) { $$wasprf=1; "prefixes\n\nflag $$1:" } else { "flag $$1:" } /e;' \
	#        -e 's/^PFX\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)/ uc("   $$3    > $$2")/e;' \
	#        -e 's/^(.*)SFX\s+(\S+)\s+([YN])\s+\d+.*$$/ $$flg=($$3 eq "Y") ? "*" : ""; $$flg="~$$flg" if length $$1; $$q=$$2; $$q="\\$$q" if $$q!~m#[a-zA-Z]#; if ( !$$wassfx ) { $$wassfx=1; "suffixes\n\nflag $$flg$$q:" } else { "flag $$flg$$q:" } /e;' \
	#        -e 's/^.*SFX\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)/ uc("   $$3    > ".( ($$1 eq "0") ? "" : "-$$1,").( ($$2 eq "0") ? "" : "$$2") )/e;' \
	#	-e 's/^(SET|TRY)/#$$1/' \
	#> $(LANGUAGE).aff
	LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 /usr/bin/unmunch $(LANGUAGE).dict $(LANGUAGE).aff > $(LANGUAGE).words 2>stderr.log || (echo "Failed"; exit 1)
	if test ! -s "$(LANGUAGE).words"; then
	    echo "$(LANGUAGE).words is empty or does not exist."
	    exit 1
	fi
	mv $(LANGUAGE).words $(LANGUAGE)-unsorted.words
	sort -u $(LANGUAGE)-unsorted.words -o $(LANGUAGE).words
	rm $(LANGUAGE)-unsorted.words
	VERSION=`grep Version README_it_IT.txt | head -n1 | sed 's/Version \+\([^ ]\+\), \+.*/\1/'` && \
	mkdir $(package)-$${VERSION} && \
	mv $(LANGUAGE).* *.txt $(package)-$${VERSION} && \
	tar -caf $(package)_$${VERSION}.orig.tar.xz $(package)-$${VERSION} && \
	mv $(package)_$${VERSION}.orig.tar.xz .. && \
	rm -rf *
	cd ..
	rmdir tmp
