#!/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
R3 = debian/doc-linux-fr-ps
R4 = debian/doc-linux-fr-pdf

HTML_BUILD = $(R)/usr/share/doc/HOWTO/fr-html
TEXT_BUILD = $(R2)/usr/share/doc/HOWTO/fr-txt
PS_BUILD = $(R3)/usr/share/doc/HOWTO/fr-ps
PDF_BUILD = $(R4)/usr/share/doc/HOWTO/fr-pdf

source = $(shell dpkg-parsechangelog | grep '^Source:' | sed 's/^Source: //')
upstream = $(shell dpkg-parsechangelog | grep '^Version:' | \
                   sed -e 's/^Version: //' -e '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

SUPP = fcol-faq fhs-fr info-sheet intro meta-faq ProductionGuidesPratiques

FILE_fhs_fr = fhs-2.0.fr-nroff.tar.gz
FILE_fcol_faq = faq-fcol.tar.gz
FILE_info_sheet = info-sheet.fr-html.tar.gz
FILE_intro = intro-html.tar.gz
FILE_meta_faq = meta-faq.fr-html.tar.gz
FILE_ProductionGuidesPratiques = master.tar.gz

URL_fhs_fr = $(LFO)/article/sys/$(FILE_fhs_fr)
URL_fcol_faq = $(LFO)/article/fcol-faq/$(FILE_fcol_faq)
URL_info_sheet = $(TRADUC)/documents/info-sheet.fr/$(FILE_info_sheet)
URL_intro = $(TRADUC)/documents/intro/$(FILE_intro)
URL_meta_faq = $(TRADUC)/documents/meta-faq.fr/$(FILE_meta_faq)
URL_ProductionGuidesPratiques = https://github.com/fevrier/ProductionGuidesPratiques/archive/$(FILE_ProductionGuidesPratiques)

OPTION_fcol_faq = --strip-components=1
OPTION_fhs_fr = --strip-components=1
OPTION_ProductionGuidesPratiques = --strip-components=1

IGNORE = ATA-RAID-HOWTO.sgml IRC.sgml Linux\+WinNT.sgml MP3-CD-Burning.sgml \
	 $(NONFREE)
NONFREE = Adv-Routing-HOWTO.sgml Astronomy-HOWTO.sgml \
	  C-C\+\+Beautifier-HOWTO.sgml CD-Distributions-EN-HOWTO.sgml \
	  DHCP.sgml FTP.xml HOWTO-HOWTO-sgml.tar.gz Installation-HOWTO.sgml \
	  IPMasquerading\+Napster.sgml Jaz-Drive-HOWTO.sgml \
	  KDE-GUI-Login-Configuration-HOWTO.xml lartc.xml Mail2News.sgml \
	  Online-Troubleshooting-HOWTO.sgml Plug-and-Play-HOWTO.xml \
	  Process-Accounting.sgml Quota.sgml Reading-List-HOWTO.sgml \
	  RPM-HOWTO.sgml SMB-HOWTO.sgml SquashFS-HOWTO-xml.tar.gz \
	  StarOffice.sgml Text-Terminal-HOWTO.sgml TransparentProxy.xml \
	  UPS-HOWTO.sgml User-Authentication-HOWTO.xml Vim-HOWTO.sgml \
	  Wacom-USB-mini-HOWTO.xml XFree86-HOWTO.sgml \
	  XFree86-Video-Timings-HOWTO.xml

SUBVERSION_images = HP-HOWTO/images/HP-HOWTO images/TimePrecision-HOWTO

WIKI_URL = http://traduc.org/Guides_pratiques/Suivi/
WIKI = EVMSUG

get-orig-source: howto $(SUBVERSION_images) $(SUPP) $(WIKI)

$(SUBVERSION_images):
	dh_testdir
	[ -d howto/images ] || mkdir howto/images
	cd howto/images && svn export svn://svn.traduc.org/guides_pratiques/trunk/$@

$(WIKI):
	dh_testdir
	[ -d howto ] || mkdir howto
	w3m "$(WIKI_URL)$@/Source?action=raw" > howto/$@.source
	for i in $$(sed -n -e '/\[\[\/.*|/p' howto/$@.source | \
		sed 's,^.*\[\[/\(.*\)|.*$$,\1,') ; do \
		w3m "$(WIKI_URL)$@/Source/$$i?action=raw" | \
		sed -e '1d' > howto/$$i && sleep 5 ; done
	[ -d howto/images/$@ ] || mkdir howto/images/$@
	for i in $$(sed -n -e '/\[\[attachment:.*\]\]/p' howto/$@.source | \
		sed 's,^.*\[\[attachment:\(.*\)\]\].*$$,\1,') ; do \
		wget -O howto/images/$@/$$i \
		"$(WIKI_URL)$@/Source?action=AttachFile&do=get&target=$$i" ; done
	rm howto/$@.source

howto:
	dh_testdir
	# Update sources of the HOWTOs.
	[ -d $@ ] || mkdir -p $@
	lftp -c 'mirror -nev $(patsubst %,--exclude %,$(IGNORE)) \
		$(TRADUC)/HOWTO/telechargement/source $@'
	# Fetch index and stylesheet
	wget -O index.html \
		"http://guidespratiques.traduc.org/nouvel_index.php?howto.1"
	lftp -c 'get -ce $(TRADUC)/HOWTO/telechargement/html-1page/style.css'

$(SUPP):
	dh_testdir
	# Download upstream tarball, preserving its timestamp
	lftp -c 'get $(URL_$(subst -,_,$@))'
	# Rename upstream tarball as proper supplementary one
	mv $(FILE_$(subst -,_,$@)) ../$(source)_$(upstream).orig-$@.tar$(suffix $(FILE_$(subst -,_,$@)))
	# Unpack upstream tarball
	[ -d $@ ] || mkdir $@
	tar -a $(OPTION_$(subst -,_,$@)) -C $@ -xf ../$(source)_$(upstream).orig-$@.tar$(suffix $(FILE_$(subst -,_,$@)))

# Build a new .orig.tar.gz.
orig: clean
	tar --xz -C .. --exclude debian --exclude .git --exclude .pc \
		$(patsubst %,--exclude %,$(SUPP)) -cvf \
	 	../$(source)_$(upstream).orig.tar.xz $(source)

override_dh_auto_build:
	cd fcol-faq && make
	cd fhs-fr && make
	cd howto && for i in *.tar.gz; do tar xaf $$i; done; \
		sed -i 's%docbook.org%oasis-open.org/docbook%' \
		CSPM-HOWTO.xml Linux-Complete-Backup-and-Recovery-HOWTO.xml Mobile-IPv6-HOWTO.xml; \
		fig2eps images/Home-Network/*.fig; \
		for i in images/Home-Network/*.eps; do convert $$i $${i%eps}png; done; \
		./preparation-des-scripts.sh; \
		for i in *xml *.sgml; do \
		HOME=/tmp LC_ALL=fr_FR.UTF-8 ../ProductionGuidesPratiques/production-traduc.org.sh $$i; done

override_dh_install:
	dh_install
	# Clean the index and remove reference to the RedHat-CD-HOWTO
	debian/index-cleaner $(HTML_BUILD)/index.html	
	rm -rf $(HTML_BUILD)/RedHat-CD-HOWTO*
	rm -rf $(HTML_BUILD)/outils/RedHat*
	rm -f $(PDF_BUILD)/RedHat-CD-HOWTO.pdf
	rm -f $(PS_BUILD)/RedHat-CD-HOWTO.ps
	rm -f $(TEXT_BUILD)/RedHat-CD-HOWTO.txt
	find $(HTML_BUILD)/images -type f -name *.eps -o -name *.fig | xargs rm -f
	gzip -n $(TEXT_BUILD)/* $(R2)/usr/share/doc/LANG/fr/linux/fhs-fr/fhs.txt
override_dh_installdocs:
	dh_installdocs
	# Workaround over #525821
	sed -i -e "s/\(^Document: .*\)-\(html\|pdf\|ps\|text\)$$/\1/g" \
	$(CURDIR)/debian/$(source)-*/usr/share/doc-base/*
override_dh_compress:
	dh_compress -X.fdisk -X.metadata
override_dh_clean:
	cd fcol-faq && make clean
	cd fhs-fr && make clean
	cd howto && rm -f *.css *.fo *.html *.pdf *.ps *.sh *.txt \
		Authentication-Gateway-HOWTO.sgml Debian-and-Windows-Shared-Printing.xml \
		Home-Network.xml Howto-HOWTO.sgml SquashFS-HOWTO.xml chapter*.xml; \
		find . -mindepth 1 -maxdepth 1 -type d ! -name images | xargs rm -rf; \
		find images -mindepth 1 -maxdepth 1 ! -name HP-HOWTO ! -name TimePrecision-HOWTO \
		$(patsubst %,! -name %,$(WIKI)) | xargs rm -rf
	dh_clean

%:
	dh $@

.PHONY: get-orig-source howto $(SUBVERSION_images) $(SUPP) $(WIKI) orig
