#!/bin/sh -e
#
# This updates po/debian-reference.pot and po/*.po
#
rm -rf po1
cp -a po po1
# List of translation languges as arguments
LANGPO="$*"
# current debian-reference package verison in Debian
DVERSION=$(dpkg-parsechangelog -S Version)

# since po/debian-reference.pot is committed to git repo, no need to do this any more.
#touch -t 200001010000.00 po/debian-reference.pot
# set up po4a.cfg for po
sed -e "s/@LANGPO@/${LANGPO}/" -e "s/@DVERSION@/${DVERSION}/" po4a1.cfg.in >po4a1.cfg
echo "I: update po1/debian-reference.pot based on debian-reference.en.xmlt"
echo "I: report of fuzzy translation is spurious and should be ignored"
#echo "I: Ignore next line 'po4a0.cfg:4: No PO file provided as subsequent parameters to 'po4a_path'. Your documentation will not be translated.'"
po4a po4a1.cfg
echo "I: auto-generate po1/ignore-rough.pot based on updated po1/debian-reference.pot"
msggrep --no-wrap -o po1/ignore-rough.pot --msgid -E -e "^<literal>[^<>][^<>]*</literal>$" po1/debian-reference.pot
# the above matches all "^<literal>...</literal>$" while excluding:
#                       "^<literal>...</literal>... <literal>...</literal>$"
#                       "^<literal>...<emphasis>... </emphasis>...</literal>$"
msggrep --no-wrap -o po1/ignore-skip-template.pot \
  --msgid -E -e "(file|package|example|command|delimiter|\.ext|\.orig| name|newname|filename|username|branch_name|user_ID)" po1/ignore-rough.pot
if [ ! -e po/ignore-skip.pot ]; then
  cp -f po1/ignore-skip-template.pot po/ignore-skip.pot
fi
echo "I: auto-generate po1/ignore.pot"
msgattrib --set-obsolete --only-file=po/ignore-skip.pot po1/ignore-rough.pot | \
msgattrib --no-obsolete -o po1/ignore.pot
echo "I: clean po1/debian-reference.pot using po1/ignore.pot"
mv -f po1/debian-reference.pot po1/debian-reference-rough.pot
msgattrib --set-obsolete --only-file=po1/ignore.pot po1/debian-reference-rough.pot | \
msgattrib --no-obsolete -o po1/debian-reference.pot
bin/update_po po/debian-reference.pot po1/debian-reference.pot
for lang in $LANGPO; do
  echo "I: clean po1/$lang.po using po1/ignore.pot"
  mv -f "po1/$lang.po" "po1/${lang}-rough.po"
  msgattrib --set-obsolete --only-file=po1/ignore.pot "po1/${lang}-rough.po" | \
  msgattrib --no-obsolete -o "po1/${lang}.po"
  bin/update_po "po/${lang}.po" "po1/${lang}.po"
done
