#!/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=4
export LC_ALL=C

MANUAL1 := reference
MANUAL2 := quick-reference
## Build language list
LANGS1 := en fr it pt-br es de pl zh-tw zh-cn ja
LANGS2 := en fr it pt-br es de pl zh-tw zh-cn ja
# For testing use reduced set instead for speed.
#LANGS1 := zh-tw
#LANGS2 := zh-tw
BUILD_TYPE := package
ENTFILES1 := $(foreach lang,$(LANGS1),$(MANUAL1).$(lang).ent)
ENTFILES2 := $(foreach lang,$(LANGS2),$(MANUAL2).$(lang).ent)
ENTFILES := $(ENTFILES1) $(ENTFILES2)

$(MANUAL1).%.ent:
	echo "<!entity language \"$*\">"        > $@
	echo "<!entity % lang-$* \"INCLUDE\">" >> $@
	echo "<!entity docversion \"$(shell \
		LC_ALL=C \
		dpkg-parsechangelog | \
		sed -n 's/^Version: *//p')\">" >> $@
	echo "<!entity docdate \"$(shell \
		bin/getdocdate -p $*)\">"      >> $@
	echo "<!entity % f-ref   \"INCLUDE\">" >> $@ 
	echo "<!entity % q-ref   \"IGNORE\">"  >> $@ 


$(MANUAL2).%.ent:
	echo "<!entity language \"$*\">"        > $@
	echo "<!entity % lang-$* \"INCLUDE\">" >> $@
	echo "<!entity docversion \"$(shell \
		LC_ALL=C \
		dpkg-parsechangelog | \
		sed -n 's/^Version: *//p')\">" >> $@
	echo "<!entity docdate \"$(shell \
		bin/getdocdate -p $*)\">"      >> $@
	# if "Debian Quick Reference" (20 page max)
	echo "<!entity % f-ref   \"IGNORE\">"  >> $@ 
	echo "<!entity % q-ref   \"INCLUDE\">" >> $@


configure: configure-stamp
configure-stamp: $(ENTFILES)
	dh_testdir
	# Do nothing
	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) "MANUAL1=$(MANUAL1)" "LANGS1=$(LANGS1)" \
	        "MANUAL2=$(MANUAL2)" "LANGS2=$(LANGS2)" \
	        "BUILD_TYPE=$(BUILD_TYPE)" html txt pdf ps README
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	-$(MAKE) "MANUAL1=$(MANUAL1)" "LANGS1=$(LANGS1)" \
	 "MANUAL2=$(MANUAL2)" "LANGS2=$(LANGS2)" \
	 "BUILD_TYPE=$(BUILD_TYPE)" clean
	-set -e; cd $(CURDIR)/debian/ ; for xx in \
	debian-reference \
	debian-reference-common ; do \
	rm -f $${xx}.README.Debian ; done

	-cd $(CURDIR)/debian/ ; \
	rm -f debian-reference-common.postinst

	-set -e; cd $(CURDIR)/debian/ ; for xx in $(LANGS1) ; do \
	rm -f debian-reference-$${xx}.README.Debian \
	      debian-reference-$${xx}.dirs \
	      debian-reference-$${xx}.doc-base \
	      debian-reference-$${xx}.postrm \
	      debian-reference-$${xx}.postinst ; \
	done

	-set -e; cd $(CURDIR)/debian/ ; for xx in $(LANGS2) ; do \
	rm -f quick-reference-$${xx}.README.Debian \
	      quick-reference-$${xx}.dirs \
	      quick-reference-$${xx}.doc-base ; \
	done

	dh_clean

refresh: clean
	dh_testdir
	dh_testroot
# 	cvs -d :ext:osamu@cvs.sf.net:/cvsroot/qref up
#	cvs -d :pserver:anonymous@cvs.sf.net:/cvsroot/qref up

new:    clean
#	rm ../debian-refernce
#	cvs -d :ext:osamu@cvs.sf.net:/cvsroot/qref export -d ../debian-reference -D tomorrow qref
#	cvs -d :pserver:anonymous@cvs.sf.net:/cvsroot/qref up


copyfiles:
#	copy README.Debian for all the packages
	set -e; cd $(CURDIR)/debian/ ; for xx in \
	debian-reference debian-reference-common ; do \
	cp README.Debian.all $${xx}.README.Debian ; done

	cd $(CURDIR)/debian/ ; \
	cp postinst-full     debian-reference-common.postinst
	# debian-reference-common.prerm exists and removes everything

	cd $(CURDIR)/debian/ ; for xx in $(LANGS1) ; do \
	cp README.Debian.all debian-reference-$${xx}.README.Debian ; \
	cp postinst-full     debian-reference-$${xx}.postinst ; \
	cp postrm-full       debian-reference-$${xx}.postrm ; \
	done

	set -e; cd $(CURDIR)/debian/ ; for xx in $(LANGS2) ; do \
	cp README.Debian.all quick-reference-$${xx}.README.Debian ; \
	done

	set -e; cd $(CURDIR)/debian/ ; for xx in $(LANGS1) ; do \
	case $$xx in \
	en) LANG="English" ;;  \
	de) LANG="German" ;;   \
	fr) LANG="French" ;;   \
	fi) LANG="Finnish" ;;  \
	es) LANG="Spanish" ;;  \
	it) LANG="Italian" ;;  \
	ja) LANG="Japanese" ;; \
	pl) LANG="Polish" ;; \
	pt-br) LANG="Portuguese (Brazil)" ;; \
	zh-tw) LANG="Chinese (Traditional)" ;; \
	zh-cn) LANG="Chinese (Simplified)" ;; \
	esac ; \
	cp debian-reference.dirs.all debian-reference-$${xx}.dirs ; \
	sed  -e "s/@@/$$xx/" -e "s/@LANG@/$$LANG/" \
	<debian-reference.doc-base-all  >debian-reference-$${xx}.doc-base ; \
	done

	set -e; cd $(CURDIR)/debian/ ; for xx in $(LANGS2) ; do \
	case $$xx in \
	en) LANG="English" ;;  \
	de) LANG="German" ;;   \
	fr) LANG="French" ;;   \
	fi) LANG="Finnish" ;;  \
	es) LANG="Spanish" ;;  \
	it) LANG="Italian" ;;  \
	ja) LANG="Japanese" ;; \
	pl) LANG="Polish" ;; \
	pt-br) LANG="Portuguese (Brazil)" ;; \
	zh-tw) LANG="Chinese (Traditional)" ;; \
	zh-cn) LANG="Chinese (Simplified)" ;; \
	esac ; \
	cp quick-reference.dirs.all quick-reference-$${xx}.dirs ; \
	sed  -e "s/@@/$$xx/" -e "s/@LANG@/$$LANG/" \
	<quick-reference.doc-base-all  >quick-reference-$${xx}.doc-base ; \
	done

install: build copyfiles
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/debian-reference-??.
	$(MAKE) "LANGS1=" "LANGS2=" "MANUAL1=$(MANUAL1)" "MANUAL2=$(MANUAL2)" \
	    "BUILD_TYPE=$(BUILD_TYPE)" \
	    PUBLISHDIR=$(CURDIR)/debian/debian-reference-common/usr/share/doc/Debian \
	    publish-examples

	install -d $(CURDIR)/debian/debian-reference-common/usr/share/applications
	install -m 644 debian/debian-reference-common.desktop \
	    $(CURDIR)/debian/debian-reference-common/usr/share/applications/

	install -m 755 --preserve-timestamps debian-reference \
	    $(CURDIR)/debian/debian-reference-common/usr/bin/debian-reference

	install -m 755 --preserve-timestamps bin/mkindexhtml \
	    $(CURDIR)/debian/debian-reference-common/usr/share/debian-reference/mkindexhtml

	set -e; for xx in $(LANGS1); do \
	$(MAKE) "LANGS1=$${xx}" "LANGS2=" "MANUAL1=$(MANUAL1)" "MANUAL2=" "BUILD_TYPE=$(BUILD_TYPE)" \
	    PUBLISHDIR=$(CURDIR)/debian/debian-reference-$${xx}/usr/share/doc/Debian \
	    publish-html1 publish-txt1 publish-ps1 publish-pdf1; \
	done

	set -e; for xx in $(LANGS2); do \
	$(MAKE) "LANGS1=" "LANGS2=$${xx}" "MANUAL1=" "MANUAL2=$(MANUAL2)" "BUILD_TYPE=$(BUILD_TYPE)" \
	    PUBLISHDIR=$(CURDIR)/debian/quick-reference-$${xx}/usr/share/doc/Debian \
	    publish-html2 publish-txt2 publish-ps2 publish-pdf2; \
	done

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf	
#	dh_movefiles
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman -p debian-reference-common debian-reference.1 debian-reference.*.1 
	dh_installinfo
#	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 binary-indep binary-arch binary install configure refresh
