#!/usr/bin/make -f
#export DH_VERBOSE=1

define rename_hunspell_files
cd $(CURDIR)/debian/hunspell-$(strip $(1))/usr/share/hunspell; \
mv -v $(strip $(2)).aff $(strip $(3)).aff; mv $(strip $(2)).dic $(strip $(3)).dic
endef

define rename_hyphen_files
cd $(CURDIR)/debian/hyphen-$(strip $(1))/usr/share/hyphen; \
mv -v hyph_$(strip $(2)).dic hyph_$(strip $(3)).dic
endef

define rename_mythes_files
cd $(CURDIR)/debian/mythes-$(strip $(1))/usr/share/mythes; \
mv -v $(strip $(2)).dat $(strip $(3)).dat; mv $(strip $(2)).idx $(strip $(3)).idx
endef

define link_hyphen
dh_link -phyphen-$(strip $(1)) usr/share/hyphen/hyph_$(strip $(3)).dic usr/share/hyphen/hyph_$(strip $(2)).dic
endef

include debian/rules.install

%:
	dh $@

execute_before_dh_auto_build:
	# make sure we don't overwrite a past backup
	[ ! -f dictionaries/cs_CZ/thes_cs_CZ.dat.BAK ]
	cd dictionaries/cs_CZ && \
		mv thes_cs_CZ.dat thes_cs_CZ.dat.BAK
	cd dictionaries/cs_CZ/thesaurus && \
		./dictionary-to-thesaurus.py en-cs.txt blacklist.txt > ../thes_cs_CZ.dat
	for dat in `find dictionaries -type f -name "*.dat"`; do \
		/usr/share/mythes/th_gen_idx.pl -o $${dat%dat}idx < $${dat}; \
	done

execute_before_dh_install: install_files

execute_after_dh_install:
	$(call rename_hunspell_files,  gug, gug,           gug_PY )
	$(call rename_hunspell_files,  is,  is,            is_IS )
	$(call rename_hunspell_files,  lt,  lt,            lt_LT )
	$(call rename_hunspell_files,  sr,  sr,            sr_RS )
	$(call rename_hunspell_files,  sr,  sr-Latn,       sr_Latn_RS )

	$(call rename_hyphen_files,    ca,  ca,            ca_ES )
	$(call rename_hyphen_files,    es,  es,            es_ES )
	$(call rename_hyphen_files,    gl,  gl,            gl_ES )
	$(call rename_hyphen_files,    is,  is,            is_IS )
	$(call rename_hyphen_files,    lt,  lt,            lt_LT )
	$(call rename_hyphen_files,    sr,  sr,            sr_RS )
	$(call rename_hyphen_files,    sr,  sr-Latn,       sr-Latn_RS )
	$(call rename_hyphen_files,    sv,  sv,            sv_SE )

	$(call rename_mythes_files,    ar,     th_ar,         th_ar_EG_v2 )
	$(call rename_mythes_files,    cs,     thes_cs_CZ,    th_cs_CZ_v2 )
	$(call rename_mythes_files,    es,     th_es_v2,      th_es_ES_v2 )
	$(call rename_mythes_files,    gl,     thesaurus_gl,  th_gl_ES_v2 )
	$(call rename_mythes_files,    gug,    th_gug_PY,     th_gug_PY_v2 )
	$(call rename_mythes_files,    is,     th_is,         th_is_IS_v2 )
	$(call rename_mythes_files,    pt-pt,  th_pt_PT,   th_pt_PT_v2 )
	$(call rename_mythes_files,    sv,     th_sv_SE,      th_sv_SE_v2 )
	$(call rename_mythes_files,    uk,     th_uk_UA,      th_uk_UA_v2 )
	$(call rename_mythes_files,    ru,     th_ru_RU_M_aot_and_v2,      th_ru_RU_v2 )

	# here are only links doing "xx → xx_XX", more links are done with
	# .links files as well.
	$(call link_hyphen, af,    af,         af_ZA )
	$(call link_hyphen, cs,    cs,         cs_CZ )
	$(call link_hyphen, da,    da,         da_DK )
	$(call link_hyphen, de,    de,         de_DE )
	$(call link_hyphen, el,    el,         el_GR )
	$(call link_hyphen, en-gb, en_Latn_GB, en_GB )
	$(call link_hyphen, hr,    hr,         hr_HR )
	$(call link_hyphen, hu,    hu,         hu_HU )
	$(call link_hyphen, it,    it,         it_IT )
	$(call link_hyphen, lt,    lt,         lt_LT )
	$(call link_hyphen, nl,    nl,         nl_NL )
	$(call link_hyphen, pt-br, pt_Latn_BR, pt_BR )
	$(call link_hyphen, pt-pt, pt,         pt_PT )
	$(call link_hyphen, pt-pt, pt_Latn_PT, pt_PT )
	$(call link_hyphen, ro,    ro,         ro_RO )
	$(call link_hyphen, sk,    sk,         sk_SK )
	$(call link_hyphen, sl,    sl,         sl_SI )
	$(call link_hyphen, uk,    uk,         uk_UA )
	$(call link_hyphen, zu,    zu,         zu_ZA )

	# Build the .bdic dictionaries that don't need special treatment.
	# The ones that do need special treatment will fail, but their hunspell-bdic directories will be created.
	for d in `find debian/hunspell-* -name "*.dic"`; do \
		mkdir -p `echo $$d | sed -e "s/share\/hunspell.*dic/share\/hunspell-bdic/"` && \
		convert-bdic $$d `echo $$d | sed -e "s/share\/hunspell/share\/hunspell-bdic/" | sed -e "s/dic$$/bdic/"`; \
	done

	# Copy the dictionaries that need special treatment to their corresponding hunspell-bdic directories.
	cp debian/hunspell-an/usr/share/hunspell/an_ES.* debian/hunspell-an/usr/share/hunspell-bdic
	cp debian/hunspell-da/usr/share/hunspell/da_DK.* debian/hunspell-da/usr/share/hunspell-bdic
	cp debian/hunspell-gl/usr/share/hunspell/gl_ES.* debian/hunspell-gl/usr/share/hunspell-bdic
	cp debian/hunspell-gu/usr/share/hunspell/gu_IN.* debian/hunspell-gu/usr/share/hunspell-bdic
	cp debian/hunspell-hu/usr/share/hunspell/hu_HU.* debian/hunspell-hu/usr/share/hunspell-bdic
	cp debian/hunspell-th/usr/share/hunspell/th_TH.* debian/hunspell-th/usr/share/hunspell-bdic
	cp debian/hunspell-uk/usr/share/hunspell/uk_UA.* debian/hunspell-uk/usr/share/hunspell-bdic

	# Convert all tabs in an_ES.aff into spaces before building the .bdic, which doesn't like tabs in most fields.
	sed -i "s/\t/ /g" debian/hunspell-an/usr/share/hunspell-bdic/an_ES.aff

	# Remove any lines greater than or equal to 109 characters in da_DK.dic.  The exact maximum line length in the .bdic binary format depends on how many UTF-16 characters there are.
	sed -i "/.\{109\}/d" debian/hunspell-da/usr/share/hunspell-bdic/da_DK.dic

	# Remove any lines greater than or equal to 110 characters in gl_ES.dic.  The exact maximum line length in the .bdic binary format depends on how many UTF-16 characters there are.
	sed -i "/.\{110\}/d" debian/hunspell-gl/usr/share/hunspell-bdic/gl_ES.dic

	# Remove any lines greater than or equal to 21 characters in gu_IN.dic.  The exact maximum line length in the .bdic binary format depends on how many UTF-16 characters there are.
	sed -i "/.\{21\}/d" debian/hunspell-gu/usr/share/hunspell-bdic/gu_IN.dic

	# Remove any lines that contain the IGNORE command in hu_HU.aff, which isn't supported by .bdic.
	sed -i "/IGNORE/d" debian/hunspell-hu/usr/share/hunspell-bdic/hu_HU.aff

	# Remove any lines greater than or equal to 33 characters in th_TH.dic.  The exact maximum line length in the .bdic binary format depends on how many UTF-16 characters there are.
	sed -i "/.\{33\}/d" debian/hunspell-th/usr/share/hunspell-bdic/th_TH.dic

	# Remove any lines that contain the IGNORE command in uk_UA.aff, which isn't supported by .bdic.
	sed -i "/IGNORE/d" debian/hunspell-uk/usr/share/hunspell-bdic/uk_UA.aff

	# Build the dictionaries that need special treatment.
	convert-bdic debian/hunspell-an/usr/share/hunspell-bdic/an_ES.dic debian/hunspell-an/usr/share/hunspell-bdic/an_ES.bdic
	convert-bdic debian/hunspell-da/usr/share/hunspell-bdic/da_DK.dic debian/hunspell-da/usr/share/hunspell-bdic/da_DK.bdic
	convert-bdic debian/hunspell-gl/usr/share/hunspell-bdic/gl_ES.dic debian/hunspell-gl/usr/share/hunspell-bdic/gl_ES.bdic
	convert-bdic debian/hunspell-gu/usr/share/hunspell-bdic/gu_IN.dic debian/hunspell-gu/usr/share/hunspell-bdic/gu_IN.bdic
	convert-bdic debian/hunspell-hu/usr/share/hunspell-bdic/hu_HU.dic debian/hunspell-hu/usr/share/hunspell-bdic/hu_HU.bdic
	convert-bdic debian/hunspell-th/usr/share/hunspell-bdic/th_TH.dic debian/hunspell-th/usr/share/hunspell-bdic/th_TH.bdic
	convert-bdic debian/hunspell-uk/usr/share/hunspell-bdic/uk_UA.dic debian/hunspell-uk/usr/share/hunspell-bdic/uk_UA.bdic

	# Remove the temporary copies of the .dic and .aff in the hunspell-bdic directory.
	rm debian/hunspell-an/usr/share/hunspell-bdic/an_ES.dic debian/hunspell-an/usr/share/hunspell-bdic/an_ES.aff
	rm debian/hunspell-da/usr/share/hunspell-bdic/da_DK.dic debian/hunspell-da/usr/share/hunspell-bdic/da_DK.aff
	rm debian/hunspell-gl/usr/share/hunspell-bdic/gl_ES.dic debian/hunspell-gl/usr/share/hunspell-bdic/gl_ES.aff
	rm debian/hunspell-gu/usr/share/hunspell-bdic/gu_IN.dic debian/hunspell-gu/usr/share/hunspell-bdic/gu_IN.aff
	rm debian/hunspell-hu/usr/share/hunspell-bdic/hu_HU.dic debian/hunspell-hu/usr/share/hunspell-bdic/hu_HU.aff
	rm debian/hunspell-th/usr/share/hunspell-bdic/th_TH.dic debian/hunspell-th/usr/share/hunspell-bdic/th_TH.aff
	rm debian/hunspell-uk/usr/share/hunspell-bdic/uk_UA.dic debian/hunspell-uk/usr/share/hunspell-bdic/uk_UA.aff

execute_before_dh_clean:
	find dictionaries/ -type f -name '*.idx' -delete
	cd dictionaries/cs_CZ && \
		[ ! -f thes_cs_CZ.dat.BAK ] || \
		mv -v thes_cs_CZ.dat.BAK thes_cs_CZ.dat

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog-dictionaries
