#!/bin/sh
ROOT=`dirname "$0"`
export ROOT=`( cd "$ROOT" && cd .. && pwd )`
MANSOURCE=$ROOT/slackware_man_pages
if [ ! $# = 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
 echo "
This script generates localized man pages for pkgtools and slackpkg.
 
Usage:
  ./make_man_pages.sh <package_name>
  
<package_name> can be either \"<ll>.pkgtools\" or \"<ll>.slackpkg\",
<ll> being your two letters language code (e.g. es.pkgtools)

Requirements:
  . 'po4a' should be installed. It is shipped in Slint.
  . Your PO file(s), (re)named <ll>.pkgtools.po or <ll>.slackpkg.po, 
    should be stored in ${ROOT}/wip (create that directory first if need be).
    For instance I would store there file fr.pkgtools.po or fr.slackpkg.po
  . You need to use an UTF-8 locale in all commands. Either prepend each 
    command with for instance:
      LANG=pt_BR.utf8 # you can use any locale ending in .utf8 or known to
                      # be UTF-8 encoded
    or to save you some typing write in /etc/profile.d/lang.sh or ~/.bashrc:
      export en_US.utf8 # Again, use any valid UTF-8 encoded locale

This script generates man pages in , simply named
as the corresponding script or file, with the <ll>. prefix

The file ${ROOT}/wip/<package> gathers formatting errors, that could have
prevented to generate one or several man page(s).

To compare localized vs genuine man pages, do this for instance :

  cd ${ROOT}/wip
  GROFF_ENCODING=utf8 man ./fa.installpkg # localized man page
  GROFF_ENCODING=utf8 man installpkg # genuine man page
 
GROFF_ENCODING is set by default to utf8 in Slint in ~/.profile.

Note: to get a proper display of ideographs in CJK language:
  . In the console run "fbterm" (shipped in Slint) before running "man"
  . In Xterm in the "VT fonts" menu (accessible with a right click while
    holding a [Ctrl] key if not displayed by default as it is in Slint):
    . Do not check True Type Fonts
    . Check UTF-8 Fonts
    . Preferably choose a Large font size
  . Yu can also display the page in mlterm (shipped in Slint).

This script is intended solely to help you test your translations of man
pages in context: please provide PO files only, not generated man pages.

When you are satisfied to the result please update the PO file in TRANSIFEX.
"
  exit
fi
if [ "`whereis -b po4a`" = "po4a:" ]; then
  echo "this script needs po4a, please install it then try again"
  exit
fi
if [ "`echo $LANG|grep utf8$`" = "" ] && [ ! "$LANG"="fa_IR" ]; then
  echo "Please use an UTF-8 encoded locale to run this script.
either set LANG permanently (for instance write in /etc/profile.d/LANG.sh
or in ~/.bashrc:
  export LANG=pt_BR.utf8 # (any UTF-8 encoded valid locale can be used)
or prepend the command with \"LANG=<ll_TT>.utf8\", e.g. 
  LANG=pt_BR.utf8 ./make_man_pages.sh slackpkg.conf"
  exit
fi
echo "pkgname=${1#*.}"
echo "language=${1%.*}"
pkgname=${1#*.}
language=${1%.*}
if [ ! "$pkgname" = "pkgtools" ] && [ ! "$pkgname" = "slackpkg" ]; then
  echo "The argument of the command should be either "pkgtools" or "slackpkg".
Please try again."
  exit
fi
if [ ! -f "${ROOT}/wip/${1}.po" ]; then
  echo "I can't find ${1}.po in ${ROOT}/wip. Please try again."
  exit
fi
rm -f  ${1}.errors
( cd ${ROOT}/wip
echo "" > ${1}.errors
for FILE in `ls $MANSOURCE/$pkgname`; do
   msggrep -N $FILE -o ${FILE}.temp ${1}.po
   po4a-translate -f man -o groff_code=verbatim \
   -m $MANSOURCE/$pkgname/$FILE -p ${FILE}.temp -l ${language}.${FILE} \
   2>>${ROOT}/wip/${1}.errors
   rm -f ${FILE}.temp
done
)
echo "If all went well, man pages are stored in ${ROOT}/wip.
Check ${ROOT}/wip/${1} for possible errors."
