#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatability version to use.

package         = iesperanto
apackage        = aspell-eo
apackage_cx     = aspell-eo-cx7

ISPELLDIR       = $(CURDIR)/debian/${package}/usr/lib/ispell
ISPELLVARDIR    = $(CURDIR)/debian/${package}/var/lib/ispell
ISPELLSHAREDIR  = $(CURDIR)/debian/${package}/usr/share/ispell
ISPELLOVRDIR    = $(CURDIR)/debian/${package}/usr/share/lintian/overrides

ASPELLDATADIR   = $(CURDIR)/debian/${apackage}/usr/lib/aspell
ASPELLSHAREDIR  = $(CURDIR)/debian/${apackage}/usr/share/aspell
ASPELLOVRDIR    = $(CURDIR)/debian/${apackage}/usr/share/lintian/overrides
ASPELLVARDIR    = $(CURDIR)/debian/${apackage}/var/lib/aspell
ASPELL6BASENAME = aspell6-eo
ASPELL6DIR      = $(CURDIR)/debian/$(ASPELL6BASENAME)
ASPELLPROC      = /usr/share/aspell-lang/proc    # Location of proc script
ASPELLDATA      = eo.dat eo.multi

TMP_BUILD       = ooo-tmp

MUNCHED_WORDS   = kerno.txt \
  komputo.txt \
  etnoland.txt \
  kancero.txt \
  movado.txt \
  personoj.txt

# Make sure build is always done in a reproducible and working
# language environment
LC_ALL = C
export LC_ALL

include /usr/share/quilt/quilt.make


configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: $(QUILT_STAMPFN) configure-stamp
	dh_testdir
	mkdir -p $(TMP_BUILD)

	cp README debian/README.ispell
	cp legumin.l3 debian/legumin.l3.ispell
	cat $(MUNCHED_WORDS) | sort -uf > kune.txt
	gzip -9n -c kune.txt > $(TMP_BUILD)/esperanto.mwl.gz
	cp eo.aff $(TMP_BUILD)/esperanto.aff

	# Create ispell latin3 munched wordlist and affix file
	sed -f debian/cx2latin3.sed kune.txt > $(TMP_BUILD)/eo.wl
	sed -f debian/cx2latin3.sed eo.aff   > eo-latin3.aff

	# Create myspell latin3 munched wordlist and affix file
	( echo `cat $(TMP_BUILD)/eo.wl | wc -l`; cat $(TMP_BUILD)/eo.wl ) > $(TMP_BUILD)/eo.dic
	ispellaff2myspell --charset=latin3 --myheader=debian/eo_ALL.header eo-latin3.aff > $(TMP_BUILD)/eo.aff

	# Copy aspell affix file with preferred name and prepare .cwl
	cat $(TMP_BUILD)/eo.wl | prezip -s -c | gzip -9n -c > $(TMP_BUILD)/eo.cwl.gz
	cp  $(TMP_BUILD)/eo.aff $(TMP_BUILD)/eo_affix.dat
	install -m 0644 debian/aspell/eo.dat debian/aspell/eo.multi $(TMP_BUILD)

	# Build stuff for aspell cx pseudocharset
	buildhash kune.txt eo.aff eo.hash
	cat kune.txt | ispell -d ./eo -e | tr -s ' ' '\n' | sort -u > $(TMP_BUILD)/eo-cx.wl
	cat $(TMP_BUILD)/eo-cx.wl | grep -v -e "-'$$" | prezip -s -c | gzip -9n -c > $(TMP_BUILD)/eo-cx.cwl.gz
	install -m 0644 debian/aspell/eo-cx.dat debian/aspell/eo-cx.multi $(TMP_BUILD)

	touch $@

aspelltgz: clean build
	mkdir -p $(ASPELL6DIR)
	install -m 644 debian/aspell/Copyright $(ASPELL6DIR)
	install -m 644 debian/aspell/info $(ASPELL6DIR)
	install -m 644 debian/aspell/eo.dat $(ASPELL6DIR)
	install -m 644 debian/README.ispell debian/legumin.l3.ispell $(ASPELL6DIR)
	install -m 644 debian/README.aspell $(ASPELL6DIR)/README
	install -m 644 eo.aff.myspell $(ASPELL6DIR)/eo_affix.dat
	cat eo.dic | sed 1d | LC_COLLATE=C sort -u | \
		aspell --local-data-dir=$(ASPELL6DIR) --lang=eo  clean > $(ASPELL6DIR)/eo.wl
	( cd $(ASPELL6DIR) && \
		ln -sf $(ASPELLPROC) proc && \
		perl proc && \
		./configure && \
		make dist )
	mv $(ASPELL6DIR)/$(ASPELL6BASENAME)*.tar.bz2 ..

clean: clean-patched unpatch
clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -f eo.hash kune.txt kune.txt.cnt kune.txt.stat
	rm -f eo-latin3.aff
	rm -f debian/legumin.l3.ispell debian/README.ispell
	rm -rf $(TMP_BUILD)

	if [ -d $(ASPELL6DIR) ]; then \
		rm $(ASPELL6DIR)/* && rmdir $(ASPELL6DIR); fi

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/${package}.

	installdeb-ispell -piesperanto
	installdeb-myspell --srcdir=$(TMP_BUILD) -pmyspell-eo
	installdeb-hunspell -pmyspell-eo
	installdeb-aspell -p$(apackage)
	installdeb-aspell -p$(apackage_cx)

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_install --sourcedir=$(TMP_BUILD)
#	dh_installman
#	dh_undocumented
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install

binary: binary-indep binary-arch
.PHONY: build clean clean-patched binary-indep binary-arch binary install patch unpatch configure
