#!/usr/bin/make -f

#export DH_VERBOSE=1

export DH_OPTIONS

define install_copyright_file
install -d $(CURDIR)/debian/$(strip $(1))/usr/share/doc/$(strip $(1))
install -m644 $(CURDIR)/dictionaries/$(strip $(2)) \
	$(CURDIR)/debian/$(strip $(1))/usr/share/doc/$(strip $(1))/README
endef

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 
	touch build-arch-stamp	

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp 
	for dat in `find dictionaries -type f -name "*.dat"`; do \
		/usr/share/mythes/th_gen_idx.pl -o $${dat%dat}idx < $${dat}; \
	done
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	for pkg in `dh_listpackages`; do \
		rm -f debian/$$pkg.postinst; rm -f debian/$$pkg.postrm; \
	done
	find dictionaries/ -type f -name '*.idx' -delete
	dh_clean

install: install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs -i

	mkdir -p $(CURDIR)/debian/tmp/usr/share/myspell/dicts
	mkdir -p $(CURDIR)/debian/tmp/usr/share/hunspell
	mkdir -p $(CURDIR)/debian/tmp/usr/share/mythes
	mkdir -p $(CURDIR)/debian/tmp/usr/share/hyphen
	mkdir -p $(CURDIR)/debian/tmp/usr/share/myspell/infos/ooo

	for dic in `find dictionaries -type f -name "hyph*.dic"`; do \
		install -m644 $$dic \
			$(CURDIR)/debian/tmp/usr/share/hyphen; \
	done
	for dic in `find dictionaries -type f -name "*.dic" ! -name "hyph*"`; do \
		install -m644 $$dic \
			$(CURDIR)/debian/tmp/usr/share/hunspell; \
	done
	for aff in `find dictionaries -type f -name "*.aff"`; do \
		install -m644 $$aff \
			$(CURDIR)/debian/tmp/usr/share/hunspell; \
	done
	for dat in `find dictionaries -type f -name "*.dat"`; do \
		install -m644 $$dat \
			$(CURDIR)/debian/tmp/usr/share/mythes; \
	done
	for idx in `find dictionaries -type f -name "*.idx"`; do \
		install -m644 $$idx \
			$(CURDIR)/debian/tmp/usr/share/mythes; \
	done
	$(call install_copyright_file, hunspell-da,           da_DK/HYPH_da_DK_README.txt)
	$(call install_copyright_file, hunspell-de-at-frami,  de/README_de_DE_frami.txt)
	$(call install_copyright_file, hunspell-de-ch-frami,  de/README_de_DE_frami.txt)
	$(call install_copyright_file, hunspell-de-de-frami,  de/README_de_DE_frami.txt)
	$(call install_copyright_file, hunspell-hu,           hu_HU/README_hu_HU.txt)
	$(call install_copyright_file, hunspell-ne,           ne_NP/README_ne_NP.txt)
	$(call install_copyright_file, hunspell-ro,           ro/README_EN.txt)
	$(call install_copyright_file, hunspell-sr,           sr/README.txt)
	$(call install_copyright_file, hunspell-sv-se,        sv_SE/LICENSE_en_US.txt)
	$(call install_copyright_file, hunspell-vi,           vi/LICENSES-en.txt)
	$(call install_copyright_file, hyphen-af,             af_ZA/README_af_ZA.txt)
	$(call install_copyright_file, hyphen-ca,             ca/LICENSES-en.txt)
	$(call install_copyright_file, hyphen-de,             de/README_hyph_de.txt)
	$(call install_copyright_file, hyphen-fr,             fr_FR/README_hyph_fr.txt)
	$(call install_copyright_file, hyphen-hu,             hu_HU/README_hyph_hu_HU.txt)
	$(call install_copyright_file, hyphen-it,             it_IT/README_hyph_it_IT.txt)
	$(call install_copyright_file, hyphen-ro,             ro/README_EN.txt)
	$(call install_copyright_file, hyphen-sl,             sl_SI/README_hyph_sl_SI.txt)
	$(call install_copyright_file, hyphen-sr,             sr/README.txt)
	$(call install_copyright_file, hyphen-sv,             sv_SE/README_hyph_sv.txt)
	$(call install_copyright_file, mythes-af,             af_ZA/README_af_ZA.txt)
	$(call install_copyright_file, mythes-ca,             ca/LICENSES-en.txt)
	$(call install_copyright_file, mythes-cs,             cs_CZ/README_cs_CZ.txt)
	$(call install_copyright_file, mythes-en-GB,          en/license.txt)
	$(call install_copyright_file, mythes-en-US,          en/license.txt)
	$(call install_copyright_file, mythes-en-ZA,          en/license.txt)
	$(call install_copyright_file, mythes-en-ca,          en/license.txt)
	$(call install_copyright_file, mythes-en-us,          en/license.txt)
	$(call install_copyright_file, mythes-fr,             fr_FR/README_thes_fr.txt)
	$(call install_copyright_file, mythes-hu,             hu_HU/README_th_hu_HU_v2.txt)
	$(call install_copyright_file, mythes-it,             it_IT/README_th_it_IT.txt)
	$(call install_copyright_file, mythes-ne,             ne_NP/README_th_ne_NP_v2.txt)
	$(call install_copyright_file, mythes-ne,             ne_NP/README_th_ne_NP_v2.txt)
	$(call install_copyright_file, mythes-ro,             ro/README_EN.txt)
	$(call install_copyright_file, mythes-ru,             ru_RU/README_ru_RU.txt)
	$(call install_copyright_file, mythes-sk,             sk_SK/README_th_sk_SK_v2.txt)
	$(call install_copyright_file, mythes-sw,             sw_TZ/README_sw_TZ.txt)
	$(call install_copyright_file, mythes-th,             th_TH/README_th_TH.txt)
	install -d $(CURDIR)/debian/usr/share/doc/mythes-en-us/
	install -m644 $(CURDIR)/dictionaries/en/license.txt \
		$(CURDIR)/debian/usr/share/doc/mythes-en-us/copyright

	dh_install --sourcedir=$(CURDIR)/debian/tmp -i
	
	for pkg in `dh_listpackages`; do \
		if [ -e debian/$$pkg.dictlistinfo ]; then \
			mkdir -p $(CURDIR)/debian/$$pkg/usr/share/myspell/infos/ooo; \
			install -m644 debian/$$pkg.dictlistinfo \
		  	  $(CURDIR)/debian/$$pkg/usr/share/myspell/infos/ooo/$$pkg; \
		fi; \
	done

install-arch:

# Must not depend on anything. This is to be called by
# binary-arch/binary-multi
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	for pkg in `dh_listpackages`; do \
		cp debian/postrm.in debian/$$pkg.postrm; \
		cp debian/postinst.in debian/$$pkg.postinst; \
	done
	dh_installchangelogs 
	dh_installdocs
	for pkg in `dh_listpackages`; do \
		if [ ! -e debian/$$pkg.copyright ]; then \
			install -m644 debian/std-copyright \
			 $(CURDIR)/debian/$$pkg/usr/share/doc/$$pkg/copyright; \
		fi; \
	done
	dh_link
	dh_compress 
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v1:4.2.1-`head -n 1 debian/changelog | awk '{ print $$2 }' | sed -e s/"("// -e s/")"// | cut -d"-" -f2`
#	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary-arch: build-arch install-arch

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure
