#!/usr/bin/make -f
# -*- makefile -*-
# 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

## We add later the locale name to each TARGETDIR
TARGETDIR_LC=$(CURDIR)/debian/thunderbird-locale-
PACKVERSION=3.1.7

BUILDDIR = $(CURDIR)/debian/build
# Nightly builds:
# Only officially released XPIs
FETCHADDRESS=ftp://ftp.mozilla.org/pub/mozilla.org/thunderbird/releases/$(PACKVERSION)/linux-i686/xpi/

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir

	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) clean
	-rm -rf $(BUILDDIR)

	dh_clean

install:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# install xpi files
	set -e; \
	  for i in xpi/*.xpi ; do \
	    CURLOCALE=`echo $$i | sed --posix 's/\.xpi//' | sed --posix 's#xpi/##'` ; \
	    CURLOCALESMALL=`echo $${CURLOCALE} | tr 'A-Z' 'a-z'` ; \
	    install-xpi -i usr/share/thunderbird-locale-$${CURLOCALESMALL} -pthunderbird-locale-$${CURLOCALESMALL} $$i ; \
	  done;
	  dh_xul-ext
	    # No need to modify maxVersion after first point release
	    # This block goes above the done right above this
	    #PKGDIR=${TARGETDIR_LC}$${CURLOCALESMALL} \
	    #PATH_TO_RDF=usr/share/thunderbird-locale-$${CURLOCALESMALL}/install.rdf ; \
	    #sed 's/\(maxVersion>3.0\)/\1.*/' $${PKGDIR}/$${PATH_TO_RDF} >> $${PKGDIR}/$${PATH_TO_RDF}.new ; \
	    #mv $${PKGDIR}/$${PATH_TO_RDF}.new $${PKGDIR}/$${PATH_TO_RDF} ;
	 
# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

clean-xpi:
	dh_testdir
	rm -f $(CURDIR)/xpi/*

update-xpi: wget rebuild-debian-files
rebuild-debian-files: update-debian-files add-unavail

wget:
	wget -N -P $(CURDIR)/xpi '$(FETCHADDRESS)*.xpi'

update-debian-files:
	cp -f $(CURDIR)/debian/templates/control.template $(CURDIR)/debian/control
	-mv -f $(CURDIR)/debian/locales.list $(CURDIR)/debian/locales.list.old

	set -e; cd $(CURDIR)/xpi; \
	  for i in *.xpi ; do \
	    CURLOCALE=`echo $$i | sed --posix 's/\.xpi//'` ; \
	    CURLANG=`echo $${CURLOCALE} | sed --posix 's/-.*//'`; \
	    CURLOCALESMALL=`echo $${CURLOCALE} | tr 'A-Z' 'a-z'` ; \
	    CURLANGXPATH=`xpath -q -e "//*[@iso_639_1_code=\"$$CURLANG\"]/@name" /usr/share/xml/iso-codes/iso_639.xml` ; \
	    CURLANGNAME=`printf "%s" "$${CURLANGXPATH}" | sed 's/ name="// ; s/"//'` ; \
	    if [ -e $(CURDIR)/debian/templates/$${CURLOCALESMALL}/control ] ; \
	    then cat $(CURDIR)/debian/templates/$${CURLOCALESMALL}/control >> $(CURDIR)/debian/control ; \
	    else  \
	      sed "s/CURLOCALESMALL/$${CURLOCALESMALL}/g; s/CURLOCALE/$${CURLOCALE}/g; \
	        s/CURLANGNAME/$${CURLANGNAME}/g; s/CURLANG/$${CURLANG}/g" \
		$(CURDIR)/debian/templates/package.template >> $(CURDIR)/debian/control; \
	    fi; \
	    echo $${CURLOCALE} >> $(CURDIR)/debian/locales.list ; \
	    if [ -e $(CURDIR)/debian/templates/$${CURLOCALESMALL}/README.Debian ] ;\
	    then cp -f $(CURDIR)/debian/templates/$${CURLOCALESMALL}/README.Debian $(CURDIR)/debian/thunderbird-locale-$${CURLOCALESMALL}.README.Debian ; \
	    fi ;\
	  done

	# Add the current list to our existing list and don't lose any of
	# the previous pacakges so that we always have a stub package
	cat $(CURDIR)/debian/locales.list >> $(CURDIR)/debian/packages.list
	sort -u $(CURDIR)/debian/packages.list > $(CURDIR)/debian/packages.list.new
	mv $(CURDIR)/debian/packages.list.new $(CURDIR)/debian/packages.list

add-unavail: 
	rm $(CURDIR)/debian/unavail.list
	touch $(CURDIR)/debian/unavail.list
	diff $(CURDIR)/debian/locales.list $(CURDIR)/debian/packages.list | sed -n 's/> //gp' >> $(CURDIR)/debian/unavail.list
	if [ `cat $(CURDIR)/debian/unavail.list | wc -l` -gt 0 ]; then \
		while read -r line; do \
			CURLOCALESMALL=`echo $$line | tr 'A-Z' 'a-z'` ; \
			sed "s/NAME/$$CURLOCALESMALL/g" $(CURDIR)/debian/templates/unavail.template >> $(CURDIR)/debian/control; \
		done< $(CURDIR)/debian/unavail.list; \
	fi

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