#!/usr/bin/make -f
#
# Invoke each target with `./debian/rules <target>'.  All targets should be
# invoked with the package root as the current directory.
# 
# Marco Budde (Budde@tu-harburg.de)
# Colin Watson (cjwatson@debian.org)
# Pierre Machard (pmachard@debian.org)
# 
# The name of the package

R = debian/doc-linux-fr-html
R2 = debian/doc-linux-fr-text

HTML_BUILD = $(R)/usr/share/doc/HOWTO/fr-html
TEXT_BUILD = $(R2)/usr/share/doc/HOWTO/fr-txt

TGZ = tar -z


source = $(shell dpkg-parsechangelog | grep '^Source:' | sed 's/^Source: //')
upstream = $(shell dpkg-parsechangelog | grep '^Version:' | \
                   sed -e 's/^Version: //' -e 's/-.*//')

#DEB_BUILD_OPTIONS=noopt

export DH_COMPAT = 2
export DH_OPTIONS

$(DIRECTORIES):
	install -d -g root -o root -m 755 $@
	chmod g-s $@
		
# Override this if you want to use a mirror closer to home.
TRADUC = ftp://traduc.org/pub/traduc.org/doc-vf

LFO = ftp://ftp.linux-france.org/pub/

refresh: refresh-howto-html refresh-howto-text refresh-intro refresh-info-sheet refresh-meta-faq

refresh-howto-html:
	dh_testdir
	# Update HTML versions of the HOWTOs.
	# Until the severals-html versions get clean, I use one-html Howto
	[ -d HOWTO/HTML/ ] || mkdir -p HOWTO/HTML
	# Unpack the Howtos
	$(TGZ) -C HOWTO/HTML -xf HOWTO/Linux-html-HOWTOs.tar.gz

	#Backup the old html-HOWTOs.tar.gz
	cp HOWTO/Linux-html-HOWTOs.tar.gz HOWTO/Linux-html-HOWTOs.old.tar.gz
	
	#refresh 
	mirror -p doc-linux-fr-html -d debian/Mirror
	#fetch index
	wget -O HOWTO/index.html \
		"http://www.traduc.org/docs/HOWTO/nouvel_index.php?global.1"
		
	# Pack the Howtos
	$(TGZ) --exclude \.mirror -C HOWTO/HTML -cf HOWTO/Linux-html-HOWTOs.tar.gz .

refresh-howto-text:
	dh_testdir
	# Update text versions of the text HOWTOs.
	[ -d HOWTO/text/ ] || mkdir -p HOWTO/text
	
	# Unpack the text howtos.
	$(TGZ) -C HOWTO/text -xf HOWTO/Linux-text-HOWTOs.tar.gz

	#Backup the old text-HOWTOs.tar.gz
	cp HOWTO/Linux-text-HOWTOs.tar.gz HOWTO/Linux-text-HOWTOs.old.tar.gz
	
	# gunzip the content since the server provide plain text only
	cd HOWTO/text ; gunzip * ; cd ../../;
	
	# Refresh
	mirror -dd -p doc-linux-fr-text -d debian/Mirror
	
	# Pack the text Howtos
	cd  HOWTO/text ; gzip -9 *; cd ../../;
	$(TGZ) --exclude \.mirror -C HOWTO/text/ -cf HOWTO/Linux-text-HOWTOs.tar.gz .

# I do not use it since upstream did not yet provide html archive
refresh-faq:
	dh_testdir
	#Update the Linux FAQ.
	[ -d fcol-faq ] || mkdir fcol-faq
	wget --timestamping --no-directories --directory-prefix fcol-faq --passive \
		$(LFO)/article/fcol-faq/faq-xml.tar.gz

# I do not use it since it requires addons to produce html. I only include
# the text archive
refresh-fhs:
	dh_testdir
	[ -d fhs-fr ] || mkdir fhs-fr
	#Update the translation of the FHS
	wget --timestamping --no-directories --directory-prefix fhs-fr --passive \
		$(LFO)/article/sys/fhs-2.0.fr-nroff.tar.gz
refresh-intro:
	dh_testdir
	[ -d intro ] || mkdir intro
	#Update the linux intro
	wget --timestamping --no-directories --directory-prefix intro --passive \
		$(TRADUC)/documents/intro/intro-html.tar.gz
		
refresh-info-sheet:
	dh_testdir
	[ -d info-sheet ] || mkdir info-sheet
	#Update the info-sheet
	wget --timestamping --no-directories --directory-prefix info-sheet --passive \
		$(TRADUC)/documents/info-sheet.fr/info-sheet.fr-html.tar.gz

refresh-meta-faq:
	dh_testdir
	[ -d meta-faq ] || mkdir meta-faq
	#Update the meta-faq
	wget --timestamping --no-directories --directory-prefix meta-faq --passive \
	$(TRADUC)/documents/meta-faq.fr/meta-faq.fr-html.tar.gz

build: build-stamp

build-stamp:
	dh_testdir
	touch $@

# Undoes the effect of `make -f rules build'.
clean:
	dh_testdir
	dh_testroot
	rm -rf $(R) $(R2)
	rm -rf HOWTO/HTML HOWTO/text
	dh_clean build-stamp 

# Build a new .orig.tar.gz.
orig: clean
	 
	 $(TGZ) -C .. --exclude debian -cvf \
	 	../$(source)_$(upstream).orig.tar.gz $(source)-$(upstream)

# Construct the binary packages.

binary-doc-linux-fr-html: DH_OPTIONS=-pdoc-linux-fr-html
binary-doc-linux-fr-html: build
	dh_testdir
	dh_testroot
	dh_clean -d
	dh_installdirs
	
	$(TGZ) -C $(HTML_BUILD) -xf HOWTO/Linux-html-HOWTOs.tar.gz
	cp HOWTO/index*.html $(HTML_BUILD)/
	
	# Clean the index and remove reference to the RedHat-CD-HOWTO see below for
	# details 
	chmod +x debian/index-cleaner
	debian/index-cleaner $(HTML_BUILD)/index.html	
	# dhelp is too broken #193428 and since doc-base does not how to deal with 
	# multiple docid #114692, I cannot run the make-dhelp script
	
	#debian/make-dhelp -v dir=fr/HOWTO $(HTML_BUILD)/HOWTO-Index.html > $(HTML_BUILD)/.dhelp
	#debian/make-dhelp -v dir=fr/HOWTO/mini \
	#	$(HTML_BUILD)/mini/MINI-HOWTO-Index.html > $(HTML_BUILD)/mini/.dhelp

	# The HTML version of this HOWTO contains sample i386-only binaries.
	# Removed for now.
	rm -rf $(HTML_BUILD)/RedHat-CD-HOWTO*
	rm -rf $(HTML_BUILD)/outils/RedHat*

	$(TGZ) -C $(R)/usr/share/doc/LANG/fr/linux/fcol-faq/ -xvf fcol-faq/fcol-faq-html.tar.gz
	ln -s ../LANG/fr/linux/fcol-faq $(R)/usr/share/doc/doc-linux-fr-html/fcol-faq
	install -m 644 debian/doc-base.fcol-faq $(R)/usr/share/doc-base/fcol-faq

	$(TGZ) -C $(R)/usr/share/doc/LANG/fr/linux/intro/ -xvf intro/intro-html.tar.gz
	ln -s ../LANG/fr/linux/intro $(R)/usr/share/doc/doc-linux-fr-html/intro

	$(TGZ) -C $(R)/usr/share/doc/LANG/fr/linux/info-sheet/ -xvf info-sheet/info-sheet.fr-html.tar.gz
	ln -s ../LANG/fr/linux/info-sheet $(R)/usr/share/doc/doc-linux-fr-html/info-sheet

	$(TGZ) -C $(R)/usr/share/doc/LANG/fr/linux/meta-faq -xvf meta-faq/meta-faq.fr-html.tar.gz
	ln -s ../LANG/fr/linux/meta-faq $(R)/usr/share/doc/doc-linux-fr-html/meta-faq

	dh_installdocs -n debian/FDL-1.1
	dh_installchangelogs
	gzip -9 $(R)/usr/share/doc/doc-linux-fr-html/FDL-1.1 \
		$(R)/usr/share/doc/doc-linux-fr-html/changelog.Debian
	ln -s ../HOWTO/fr-html $(R)/usr/share/doc/doc-linux-fr-html/HOWTO
	
	install -m 644 debian/index.html $(R)/usr/share/doc/doc-linux-fr-html/

binary-doc-linux-fr-text: DH_OPTIONS=-pdoc-linux-fr-text
binary-doc-linux-fr-text: build
	dh_testdir
	dh_testroot
	dh_clean -d
	dh_installdirs

	$(TGZ) -C $(TEXT_BUILD) -xf HOWTO/Linux-text-HOWTOs.tar.gz
	rm -f $(TEXT_BUILD)/COPYRIGHT

	#this HOWTO contains sample i386-only binaries.
	# Removed for now. 
	rm -f $(TEXT_BUILD)/RedHat-CD-HOWTO-text.tar.gz
	
	dh_installdocs -n debian/FDL-1.1
	dh_installchangelogs
	gzip -9 $(R2)/usr/share/doc/doc-linux-fr-text/FDL-1.1 \
		$(R2)/usr/share/doc/doc-linux-fr-text/changelog.Debian
	ln -s ../HOWTO/fr-txt $(R2)/usr/share/doc/doc-linux-fr-text/HOWTO
	cp -a fhs-fr/fhs.txt $(R2)/usr/share/doc/LANG/fr/linux/fhs-fr/
	gzip -9 $(R2)/usr/share/doc/LANG/fr/linux/fhs-fr/fhs.txt
	ln -s ../LANG/fr/linux/fhs-fr $(R2)/usr/share/doc/doc-linux-fr-text/fhs-fr

binary-arch:

# Makes a binary package.
binary-indep: DH_OPTIONS=-i
binary-indep: binary-doc-linux-fr-html binary-doc-linux-fr-text
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: refresh refresh-howto-html refresh-howto-text refresh-faq
.PHONY: refresh-fhs refresh-intro refresh-info-sheet refresh-meta-faq
.PHONY: clean orig binary-doc-linux-fr-html binary-doc-linux-fr-text
.PHONY: binary-arch binary-indep binary
