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

# 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-fr/po4a-fixes.patch; do \
	  if test -f $$p; \
	  then \
	    cd top_build/build/C && patch -p1 < $$p; \
	    break; \
	  fi; \
	done
	touch $@

# Compendium and POT files for translation of upstream Linux man-pages
generate-upstream-pot: $(stamp)generate-upstream-pot
$(stamp)generate-upstream-pot: $(stamp)setup-man
	# Compendium and POT files for original strings
	msgcat --use-first perkamon-fr/po4a/*/po/*.pot > top_build/man-pages-upstream.pot
	msgcat --use-first perkamon-fr/po4a/*/po/fr.po > top_build/compendium-fr.po
	touch $@

# Merge perkamon-fr, 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
	$(MAKE) -C perkamon-fr unpack
	# Copy perkamon-fr/perkamon/po4a directory into top_build
	cp -a perkamon-fr/perkamon/po4a top_build
	# Copy perkamon-fr/po4a directory into top_build
	-tar cf - --exclude=.svn -C perkamon-fr 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
	# Add stamp files to pretend that top_build has been generated by the Makefile
	touch top_build/stamp-unpack-release
	touch top_build/stamp-unpack
	touch top_build/stamp-setup
	# Copy Makefile to simplify make commands
	cp perkamon-fr/perkamon/Makefile top_build/
	#  Apply patches to fix perkamon-fr/perkamon/Makefile
	test ! -f $(CURDIR)/makefile-fixes.patch || patch top_build/Makefile < $(CURDIR)/makefile-fixes.patch
	touch $@

# Modify top_build/po4a/*/*.cfg to discard files removed in Debian
discard-files: $(stamp)discard-files
$(stamp)discard-files: $(stamp)setup-po4a
	$(MAKE) -C top_build EXTRA_PO4A_SUBDIRS=man-debian disable-removed
	touch $@

# Print files added in Debian which have not been inserted into debian.cfg
print-new-files: $(stamp)print-new-files
$(stamp)print-new-files: $(stamp)setup-po4a
	$(MAKE) -C top_build print-new-files
	touch $@

# Generate new POT files for man-pages as shipped in Debian
generate-pot: $(stamp)generate-pot
$(stamp)generate-pot: $(stamp)setup-po4a $(stamp)generate-upstream-pot $(stamp)discard-files $(stamp)print-new-files
	# Generate new POT files for man-pages as shipped in Debian
	$(MAKE) -C top_build EXTRA_PO4A_SUBDIRS=man-debian LANGS=fr PO4AFLAGS="--force --no-translations"
	# Here goes the magic.  Put in top_build/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 && msgcat --use-first po4a/*/po/*.pot > man-pages-dist-debian.pot
	cd top_build && 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 top_build/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 top_build/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/*/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
	$(MAKE) -C top_build \
       EXTRA_PO4A_SUBDIRS=man-debian LANGS=fr \
       PO4AFLAGS="-k 100 --msgmerge-opt '-C compendium-fr.po -C ../po4a-local/po/fr.po'"
	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 top_build/compendium-fr.po top_build/*.pot po4a-local/po/newfr.po
	-$(MAKE) -C perkamon-fr $@
	-rm -rf $(stamp)

postats: $(stamp)updatepo
	$(MAKE) -C top_build PO4A_SUBDIRS=man-debian 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
