#!/usr/bin/make -f
export LC_ALL=C

# For Policy 3.9.4
build-arch: build
build-indep: build

build:
	dh_testdir
	$(MAKE) post-build
	@for p in manpages manpages-dev; do \
	  find top_build/build/$$p -type f > debian/manpages-fr$${p#manpages}.manpages; \
	done

clean:
	dh_testdir
	dh_testroot
	dh_clean
	$(MAKE) clean
	-rm -rf top_build
	-rm -f debian/manpages-fr.manpages debian/manpages-fr-dev.manpages
	-rm -rf manpages/usr manpages-dev/usr manpages*_all.deb

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_lintian

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installman --language=fr -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

MIRRORSITE = http://ftp.fr.debian.org/debian

#  Source contains 4 tarballs:
#     manpages-fr_$v.orig.tar.xz
#          contains only top-level Makefile
#     manpages-fr_$v.orig-perkamon-fr.tar.xz
#          perkamon-fr sources, downloaded from https://alioth.debian.org/frs/?group_id=100455
#     manpages-fr_$v.orig-manpages.tar.gz
#          is data.tar.gz from Debian manpages_$d_all.deb
#     manpages-fr_$v.orig-manpages-dev.tar.gz
#          is data.tar.gz from Debian manpages-dev_$d_all.deb
#
#  A Debian version number like 3.25d2p4-3 is interpreted like this:
#   * This is the French translation of the Debian packages manpages{,-dev} 3.25-2
#   * It contains upstream French translations from perkamon-fr 3.25-4
#   * This is the 3rd Debian revision
#
#  As dpkg-source does not cope well with binary files and broken
#  symlinks, manual pages are uncompressed.  Symbolic links
#  are removed and a file named 'link' contains the list of
#  such links.  Its format is:
#      target_man_page src_man_page
#  This file is used after pages are translated to create aliases
#  of translated manual pages.
get-orig-source:
	# First remove existing files and links
	-find manpages* -type f -o -name .svn -prune | xargs --no-run-if-empty rm
	-find manpages* -type l -o -name .svn -prune | xargs --no-run-if-empty rm
	-find perkamon-fr -type f -o -name .svn -prune | xargs --no-run-if-empty rm
	# Now remove empty directories
	find manpages* -depth -type d -o -name .svn -prune | xargs --no-run-if-empty rmdir --ignore-fail-on-non-empty
	@set -e; \
	   v=$$(sed -e 1q debian/changelog | sed -e 's/.*(//' -e 's/).*//'); \
	   u=$$(echo $$v | sed -e 's/d.*//'); \
	   d=$$u-$$(echo $$v | sed -e 's/.*d//' -e 's/p.*//'); \
	   p=$$u-$$(echo $$v | sed -e 's/.*p//' -e 's/-.*//'); \
	   v=$$(echo $$v | sed -e 's/-[^-]*$$//'); \
	   echo "Version: $$v Upstream: $$u DebPkg: $$d Perkamon: $$p"; \
	   if test ! -e ../manpages-fr_$${v}.orig-perkamon-fr.tar.xz; then \
	        uscan --no-symlink --download-current-version; \
	        mv ../perkamon-fr-$${p}.tar.xz ../manpages-fr_$${v}.orig-perkamon-fr.tar.xz; \
	   fi; \
	   tar xf ../manpages-fr_$${v}.orig-perkamon-fr.tar.xz -C perkamon-fr --strip-components=1; \
	   if test ! -e ../manpages-fr_$${v}.orig.tar.xz; then \
	        rm -rf manpages-fr; \
	        mkdir manpages-fr; \
	        cp Makefile manpages-fr/; \
	        tar Jcf ../manpages-fr_$${v}.orig.tar.xz --numeric-owner manpages-fr; \
	        rm -rf manpages-fr; \
	   fi; \
	   for pkg in manpages manpages-dev; do \
		if test -e ../manpages-fr_$${v}.orig-$$pkg.tar.xz; then \
			tar xf ../manpages-fr_$${v}.orig-$$pkg.tar.xz; \
			continue; \
		fi; \
		echo "Downloading package $$pkg ..."; \
		if test -e ../$${pkg}_$${d}_all.deb; then \
			cp ../$${pkg}_$${d}_all.deb .; \
		else \
			wget $(MIRRORSITE)/pool/main/m/manpages/$${pkg}_$${d}_all.deb; \
		fi; \
		test -d $$pkg || mkdir $$pkg; \
		ar p $${pkg}_$${d}_all.deb data.tar.xz | tar Jxf - -C $$pkg; \
		echo "Processing package $$pkg ..."; \
		cd $$pkg; \
		mkdir C 2>/dev/null || true; \
		rm -f C/man?/*.? C/link; \
		for i in $$(seq 8); do mkdir C/man$$i 2>/dev/null || true; done; \
		for f in usr/share/man/man*/*.gz; do \
			if [ -L $$f ]; then \
				g=`readlink -m $$f | sed -e 's,.*/man/,,'`; \
				echo $$g /$$f >> C/link; \
			else \
				g=`echo $$f | sed -e 's,usr/share/man/,,' -e 's/\.gz$$//'`; \
				zcat $$f > C/$$g; \
			fi; \
		done; \
		rmdir C/man* 2>/dev/null || true; \
		LC_ALL=C sort C/link > temp && mv temp C/link; \
		cd ..; \
		echo "Generating ../manpages-fr_$${v}.orig-$$pkg.tar.xz ..."; \
		tar Jcf ../manpages-fr_$${v}.orig-$$pkg.tar.xz --exclude=.svn --owner root --group root $$pkg/C; \
	   done
	-rm -rf manpages/usr manpages-dev/usr manpages*_all.deb

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