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

define rename_hunspell_files
cd $(CURDIR)/debian/hunspell-$(strip $(1))/usr/share/hunspell; \
mv $(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 hyph_$(strip $(2)).dic hyph_$(strip $(3)).dic
endef

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

%:
	dh $@

override_dh_auto_build:
	for dat in `find dictionaries -type f -name "*.dat"`; do \
		/usr/share/mythes/th_gen_idx.pl -o $${dat%dat}idx < $${dat}; \
	done
	dh_auto_build $@

override_dh_install:
	dh_install

	$(call rename_hunspell_files,  es,  es_ANY,        es_ES )
	$(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_ANY,        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,    es,  th_es_ANY_v2,  th_es_ES_v2 )
	$(call rename_mythes_files,    gl,  thesaurus_gl,  th_gl_ES_v2 )
	$(call rename_mythes_files,    is,  th_is,         th_is_IS_v2 )
	$(call rename_mythes_files,    uk,  th_uk_UA,      th_uk_UA_v2 )

override_dh_clean:
	find dictionaries/ -type f -name '*.idx' -delete
	dh_clean

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog-dictionaries
