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

PO4AFLAGS = -k 100 --msgmerge-opt '-C ../po4a-local/po/compendium-$$lang.po'

# Directory containing timestamp files
stamp := stamp-dir/

# Create this directory to simplify timestamp management
DUMMY := $(shell mkdir -p $(stamp))

# function to create links
# It uses ".so" inclusions, even if the original link is a symbolic link
# examples:
#   $(call create_link,foo.1,bar.1)
#     creates the link bar.1 pointing to foo.1 in fr/man1
#   $(call create_link,../man1/foo.1,bar.2)
#     creates the link bar.2 in fr/man2 pointing to
#     ../man1/foo.1, i.e. fr/man1/foo.1
#     creates the fr/man2 directory if needed
#  The elif clause below is because pages from manpages-dev
#  can link to pages from manpages
define create_link
  echo -n "Creating link $(2) pointing to $(1)... "; \
  section1=`echo $(1) | sed -e "s/^.*\.\([1-9]\)\([^1-9.][^.]*\)*$$/\1/"`; \
  section2=`echo $(2) | sed -e "s/^.*\.\([1-9]\)\([^1-9.][^.]*\)*$$/\1/"`; \
  if ! expr $$section1 : "[1-9]" >/dev/null || ! expr $$section2 : "[1-9]" >/dev/null; then \
    echo "failed."; \
    exit 0; \
  fi; \
  if [ -e fr/man$$section1/$(1) ] && [ ! -e fr/man$$section2/$(2) ]; then \
    if [ ! -d fr/man$$section2 ]; then \
      mkdir fr/man$$section2; \
    fi; \
    echo ".so man$$section1/$(1)" > fr/man$$section2/$(2); \
    echo "done."; \
  elif [ -e ../manpages/fr/man$$section1/$(1) ] && [ ! -e fr/man$$section2/$(2) ]; then \
    if [ ! -d fr/man$$section2 ]; then \
      mkdir fr/man$$section2; \
    fi; \
    echo ".so man$$section1/$(1)" > fr/man$$section2/$(2); \
    echo "done."; \
  else \
    echo "ignored."; \
  fi
endef

#  Copy manual pages into top_build/build/C
setup-man: $(stamp)setup-man
$(stamp)setup-man:
	-rm -rf top_build
	mkdir -p top_build/build/C
	for i in $$(seq 8); do mkdir top_build/build/C/man$$i; done
	#  Some manual pages are only aliases, they contain a single line
	#      .so target_man_page
	#  Create a file named 'link', its format is:
	#      target_man_page src_man_page
	#  This file is used after pages are translated to create aliases
	#  of translated manual pages.
	set -e; \
	  for p in manpages manpages-dev; do \
		echo "Processing package $$p ..."; \
		tar cf - --exclude=.svn -C $$p C | tar xf - -C top_build/build; \
		tar cf - --exclude=.svn -C $$p C | tar tf - | sed -e '/\/$$/d' -e 's,^C/,,' > top_build/build/C/$$p.list; \
		sed -e 's/\.gz\b//g' top_build/build/C/link > top_build/build/C/$$p.link; \
	  done
	#  Remove empty directories, if any
	-rmdir top_build/build/C/man* 2>/dev/null
	#  armscii-8 encoding is missing in Perl, convert to UTF-8 to make po4a work
	@echo "Convert encodings missing in Perl..."
	iconv -f armscii-8 -t utf-8 top_build/build/C/man7/armscii-8.7 | sed -e '1s/coding: ARMSCII-8/coding: UTF-8/' > temp \
	    && mv temp top_build/build/C/man7/armscii-8.7
	#  Apply patches to fix groff syntax errors which  prevent po4a processing
	for p in $(CURDIR)/debian-fixes.patch $(CURDIR)/perkamon/po4a-fixes.patch; do \
	  if test -f $$p; \
	  then \
	    cd top_build/build/C && patch -p1 < $$p; \
	    break; \
	  fi; \
	done
	touch $@

# Merge perkamon and po4a-local stuff into top_build/po4a
setup-po4a: $(stamp)setup-po4a
$(stamp)setup-po4a: $(stamp)setup-man
	-rm -rf top_build/po4a
	mkdir top_build/po4a
	# Copy perkamon/po4a directory into top_build
	-tar cf - --exclude=.svn -C perkamon po4a | tar xf - -C top_build
	# Copy po4a-local/po and po4a-local/man-debian.cfg into top_build/po4a/man-debian
	mkdir top_build/po4a/man-debian
	tar cf - --exclude=.svn -C po4a-local po man-debian.cfg | tar xf - -C top_build/po4a/man-debian
	# Addenda must be placed into top_build/po4a/add_fr
	tar cf - --exclude=.svn -C po4a-local add_fr | tar xf - -C top_build/po4a
	touch $@

# Modify top_build/po4a/man*/man*.cfg to discard files removed in Debian
discard-files: $(stamp)discard-files
$(stamp)discard-files: $(stamp)setup-po4a
	cd top_build && $(MAKE) -I../perkamon -f../perkamon/Makefile disable-removed
	touch $@

# Generate new POT files for man-pages as shipped in Debian
generate-pot: $(stamp)generate-pot
$(stamp)generate-pot: $(stamp)discard-files
	# Compendium and POT files for original strings
	msgcat --use-first perkamon/po4a/man*/po/man*.pot > po4a-local/po/man-pages-upstream.pot
	msgcat --use-first perkamon/po4a/man*/po/fr.po > po4a-local/po/compendium-fr.po
	# Generate new POT files for man-pages as shipped in Debian
	set -e; cd top_build; for f in po4a/man*/man*.cfg; do po4a --force --no-translations $$f; done
	# Here goes the magic.  Put in po4a-local/po/man-pages-only-debian.pot
	# the strings which appear in man-pages shipped in Debian but not in
	# man-pages-upstream.pot
	cd top_build/po4a && msgcat --use-first man-debian/po/man-debian.pot man[0-9]*/po/man*.pot > ../../po4a-local/po/man-pages-dist-debian.pot
	cd po4a-local/po && cat man-pages-upstream.pot | msgcat --less-than=2 man-pages-upstream.pot - man-pages-dist-debian.pot > man-pages-only-debian.pot
	touch $@

# Update po4a-local/po/fr.po with po4a-local/po/man-pages-only-debian.pot
updatepo: $(stamp)updatepo
$(stamp)updatepo: $(stamp)generate-pot
	# No fuzzy matching, otherwise string remains fuzzy even if there is a right entry in compendium
	msgmerge --previous po4a-local/po/fr.po po4a-local/po/man-pages-only-debian.pot > po4a-local/po/newfr.po
	msgattrib --no-obsolete po4a-local/po/newfr.po > po4a-local/po/fr.po
	for f in top_build/po4a/man*/po/fr.po; do \
	  msgcat --use-first po4a-local/po/fr.po $$f | msgattrib --no-obsolete --no-fuzzy - > temp && mv temp $$f; \
	done
	touch $@

# Generate translations
translate: $(stamp)translate
$(stamp)translate: $(stamp)updatepo
	set -e; cd top_build; for f in po4a/man*/man*.cfg; do po4a --force $(PO4AFLAGS) $$f; done
	set -e; cd top_build; for f in build/[!C]*/man7/*.7; do sed -i -e '1s/coding: *[^ ]*/coding: UTF-8/' $$f; done
	touch $@

clean:
	-rm -rf top_build
	-rm -f po4a-local/po/compendium-fr.po po4a-local/po/newfr.po po4a-local/po/man*.pot
	-rm -rf $(stamp)

postats: $(stamp)updatepo
	cd top_build && $(MAKE) -I../perkamon -f../perkamon/Makefile stats-fr

post-build: translate
	@set -e; cd top_build/build; \
	  for p in manpages manpages-dev; do \
	    rm -rf $$p; \
	    mkdir -p $$p/fr; \
	    for i in $$(seq 8); do mkdir $$p/fr/man$$i; done; \
	    for f in $$(cat C/$$p.list); do \
	      test -f fr/$$f || continue; \
	      cp fr/$$f $$p/fr/$$f; \
	    done; \
	    cd $$p; \
	    sed -e 's,[^ ]*/\([^/ ]*\),\1,g' ../C/$$p.link | while read line; do \
	      TARGET=`echo $$line | cut -d " " -f 1`; \
	      LINK=`echo $$line | cut -d " " -f 2`; \
	      $(call create_link,$$TARGET,$$LINK); \
	    done; \
	    cd ..; \
	  done
	# Remove empty directories
	rmdir top_build/build/manpages*/fr/man* 2>/dev/null || true

.PHONY: setup-man setup-po4a discard-files generate-pot updatepo translate post-build clean
