#!/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 compatibility version to use.
#export DH_COMPAT=3

#  -- ispell dict package name and dirs
IPACKAGE        = igalician-minimos
IDIR            = $(CURDIR)/debian/$(IPACKAGE)/usr/lib/ispell
ISHAREDIR       = $(CURDIR)/debian/$(IPACKAGE)/usr/share/ispell
IVARDIR         = $(CURDIR)/debian/$(IPACKAGE)/var/lib/ispell
# -- wordlist package name and dirs
WPACKAGE        = wgalician-minimos
WDIR            = $(CURDIR)/debian/$(WPACKAGE)/usr/share/dict
# -- aspell dict package name and dirs
APACKAGE        = aspell-gl-minimos
ADIR            = $(CURDIR)/debian/$(APACKAGE)/usr/lib/aspell
ADATADIR        = $(CURDIR)/debian/$(APACKAGE)/usr/lib/aspell
AVARDIR         = $(CURDIR)/debian/$(APACKAGE)/var/lib/aspell
ASHAREDIR       = $(CURDIR)/debian/$(APACKAGE)/usr/share/aspell
# -- aspell official dict builddir
ASPELL6BASENAME = aspell6-gl
ASPELL6DIR      = $(CURDIR)/debian/$(ASPELL6BASENAME)
ASPELLPROC      = /usr/share/aspell-lang/proc    # Location of proc script

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

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

	touch configure-stamp


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir

# 	Add here commands to compile the package.
#	$(MAKE)
#	/usr/bin/docbook-to-man debian/ispell-gl.sgml > ispell-gl.1
	gzip -9n -c galician.words > galician-minimos.mwl.gz
	buildhash galician.words galician.aff galician.hash
#
	cat galician.words | ispell -d ./galician -e | \
		tr -s ' ' '\n' | LC_ALL=POSIX sort -u | \
		iconv -f latin1 -t utf-8 > galician-minimos.debian-plain-wordlist

# -- Creating myspell aff file for aspell-gl-es
	ispellaff2myspell --charset=latin1 \
		--myheader=debian/gl_ES.myheader \
		--replacements=debian/gl_ES.replaces \
		galician.aff > gl_ES.myaff

# -- Dealing with aspell
	cat galician.words | prezip -s -c | gzip -9n -c > gl-minimos.cwl.gz

# -- Done

	touch build-stamp

aspelltgz: clean build
	mkdir -p $(ASPELL6DIR)/doc
	install -m 0644 debian/aspell/Copyright $(ASPELL6DIR)
	install -m 0644 debian/aspell/info $(ASPELL6DIR)
	install -m 0644 /usr/share/common-licenses/GPL-2 $(ASPELL6DIR)/COPYING
	sed 's/gl-minimos/gl/' debian/galician-minimos.dat > debian/aspell/gl.dat
	install -m 0644 debian/aspell/gl.dat $(ASPELL6DIR)/gl.dat
	install -m 0644 debian/galician_phonet.dat  $(ASPELL6DIR)/gl_phonet.dat
	install -m 0644 gl_ES.myaff $(ASPELL6DIR)/gl_affix.dat
	install -m 0644 debian/README.minimos normas.txt $(ASPELL6DIR)/doc
	cat gl_ES.mydict | sed 1d | LC_COLLATE=C sort -u > $(ASPELL6DIR)/gl.wl
	( cd $(ASPELL6DIR) && \
		ln -sf $(ASPELLPROC) && \
		perl proc && \
		./configure && \
		make dist )
	mv $(ASPELL6DIR)/$(ASPELL6BASENAME)*.tar.bz2 ..

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

#	Add here commands to clean up after the build process.
#	-$(MAKE) clean
	-rm galician.hash galician.words.cnt galician.words.stat galician-minimos.mwl.gz
	-rm galician-minimos.debian-plain-wordlist
#
	rm -f gl_ES.mydict gl_ES.myaff
	rm -f gl-minimos.cwl.gz
	rm -f debian/aspell/gl.dat
	-rm -f $(ASPELL6DIR)/doc/* && rmdir $(ASPELL6DIR)/doc
	-rm -f $(ASPELL6DIR)/* && rmdir $(ASPELL6DIR)
#
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

# 	Add here commands to install the package into debian/ispell-gl.
#	$(MAKE) install DESTDIR=$(CURDIR)/debian/ispell-gl
# -- ispell
#	install -m 644 galician.hash $(IDIR)/galician-minimos.hash
	install -m 644 galician-minimos.mwl.gz $(ISHAREDIR)
	install -m 644 galician.aff $(IDIR)/galician-minimos.aff
# -- wordlist
	install -m 644 galician-minimos.debian-plain-wordlist $(WDIR)/galician-minimos
# -- aspell
#	install -m 644 galician-minimos.debian-aspell-dict $(ADIR)/galician-minimos.rws
	install -m 644 gl-minimos.cwl.gz             $(ASHAREDIR)
	install -m 644 debian/galician-minimos.dat   $(ADATADIR)/gl-minimos.dat
	install -m 644 debian/galician_phonet.dat    $(ADATADIR)/gl-minimos_phonet.dat
	install -m 644 debian/gl-minimos.multi       $(ADIR)
	install -m 644 debian/galician-minimos.alias $(ADIR)
	install -m 644 gl_ES.myaff                   $(ADATADIR)/gl-minimos_affix.dat

#	This is really more appropiate for a binary-common target, but for
#	simplicity this is also a good place

	installdeb-wordlist -p$(WPACKAGE) # Calls internally dh_installdebconf
	installdeb-ispell   -p$(IPACKAGE) # Calls internally dh_installdebconf
	installdeb-aspell   -p$(APACKAGE)
        #
	dh_installdocs -A


# Build architecture-independent files here.
#binary-indep: DH_OPTIONS=--package=$(IPACKAGE) --package=$(WPACKAGE) --package=$(MPACKAGE) --package=$(APACKAGE)
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_installchangelogs
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb --package=$(WPACKAGE)
	dh_builddeb --package=$(IPACKAGE)
	dh_builddeb --package=$(APACKAGE)

binary-arch:

binary: binary-indep

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