#! /bin/sh -e
# vim: set sts=4 expandtab:
# $1: en fr pt zh-cn ...
#

DPO="po"
DBIN="bin"
DCC="/usr/share/opencc"
MANUAL="debian-reference"
# The threshold should be 80 if translation is completed.
THRESHOLD="0"
TRANSLATE="po4a-translate -M utf-8 --format docbook --keep ${THRESHOLD} -v"
MSGCAT="/usr/bin/msgcat"
OPENCC="/usr/bin/opencc"

# Generate XML (en)
gen_xml_en () {
    echo "!!! Should not be here !!!" >&2
    false
}

# Generate XML (other)
gen_xml_other () {
    if [ -f ${DPO}/${1}.add ]; then
        ${TRANSLATE} -m ${MANUAL}.en.xml -a ${DPO}/${1}.add -p ${DPO}/${1}.po -l ${MANUAL}.${1}.xml
    else
        ${TRANSLATE} -m ${MANUAL}.en.xml -p ${DPO}/${1}.po -l ${MANUAL}.${1}.xml
    fi
}

# Generate XML (zh-cn)
gen_xml_zh_CN () {
    if [ -f ${OPENCC} ] ; then
        ${MSGCAT} --no-wrap ${DPO}/zh-tw.po |\
        ${OPENCC} -c ${DCC}/tw2sp.json -o ${DPO}/zh-cn.po-opencc
        ${MSGCAT} -o ${DPO}/zh-cn.po-best --use-first ${DPO}/zh-cn.po ${DPO}/zh-cn.po-opencc
        ${DBIN}/fuzzypo ${DPO}/zh-cn.po-best >>fuzzy.log
    else
        ln -f ${DPO}/zh-cn.po ${DPO}/zh-cn.po-best
    fi
    if [ -f ${DPO}/zh-cn.add ]; then
        ${TRANSLATE} -m ${MANUAL}.en.xml -a ${DPO}/zh-cn.add -p ${DPO}/zh-cn.po-best -l ${MANUAL}.zh-cn.xml
    else
        ${TRANSLATE} -m ${MANUAL}.en.xml -p ${DPO}/zh-cn.po-best -l ${MANUAL}.zh-cn.xml
    fi
    #rm -f ${DPO}/zh-cn.po-best
}

# Generate XML (zh-tw)
gen_xml_zh_TW () {
    if [ -f ${OPENCC} ] ; then
        ${MSGCAT} --no-wrap ${DPO}/zh-cn.po |\
        ${OPENCC} -c ${DCC}/s2twp.json -o ${DPO}/zh-tw.po-opencc
        ${MSGCAT} -o ${DPO}/zh-tw.po-best --use-first ${DPO}/zh-tw.po ${DPO}/zh-tw.po-opencc
        ${DBIN}/fuzzypo ${DPO}/zh-tw.po-best >>fuzzy.log
    else
        ln -f ${DPO}/zh-tw.po ${DPO}/zh-tw.po-best
    fi
    if [ -f ${DPO}/zh-tw.add ]; then
        ${TRANSLATE} -m ${MANUAL}.en.xml -a ${DPO}/zh-tw.add -p ${DPO}/zh-tw.po-best -l ${MANUAL}.zh-tw.xml
    else
        ${TRANSLATE} -m ${MANUAL}.en.xml -p ${DPO}/zh-tw.po-best -l ${MANUAL}.zh-tw.xml
    fi
    #rm -f ${DPO}/zh-tw.po-best
}

# Main routine
case "$1" in
    "zh-tw")    gen_xml_zh_TW;;
    "zh-cn")    gen_xml_zh_CN;;
    "en")       gen_xml_en;;
    *)          gen_xml_other "$1";;
esac
echo "Don't worry about above po4a-updatepo warning message caused by the intentional skipping of translation for some tags."

