#!/bin/sh -e
LANGPO="$*"
DPO="po"
DPOTMP="po-tmp"
DBIN="bin"
DCC="/usr/share/opencc"
# The threshold should be 80 if translation is completed.
MSGCAT="/usr/bin/msgcat"
OPENCC="/usr/bin/opencc"

# Generate PO (zh-cn)
gen_po_zh_CN () {
    if [ -f ${OPENCC} ] ; then
        rm -f ${DPOTMP}/zh-cn.po
        ${MSGCAT} --no-wrap ${DPO}/zh-tw.po | ${OPENCC} -c ${DCC}/tw2sp.json -o ${DPO}/zh-cn.po-opencc
        ${MSGCAT} -o ${DPOTMP}/zh-cn.po --use-first ${DPO}/zh-cn.po ${DPO}/zh-cn.po-opencc
    fi
}

# Generate PO (zh-tw)
gen_po_zh_TW () {
    if [ -f ${OPENCC} ] ; then
        rm -f ${DPOTMP}/zh-tw.po
        ${MSGCAT} --no-wrap ${DPO}/zh-cn.po | ${OPENCC} -c ${DCC}/s2twp.json -o ${DPO}/zh-tw.po-opencc
        ${MSGCAT} -o ${DPOTMP}/zh-tw.po --use-first ${DPO}/zh-tw.po ${DPO}/zh-tw.po-opencc
    fi
}


# build English source
make debian-reference.en.xml
make debian-reference.en.xmlt
# ensure POT is older
#touch -t 200001010000.00 po/debian-reference.pot
# update PO
sed -e "s/@LANGPO@/${LANGPO}/" po4a.cfg.in > po4a.cfg
po4a -v --no-translations po4a.cfg
# Fresh start
rm -rf po-tmp
cp -r po po-tmp
gen_po_zh_CN
gen_po_zh_TW
# ensure to update pot 
sed -e "s/@LANGPO@/${LANGPO}/" po4a-tmp.cfg.in > po4a-tmp.cfg
touch -t 200001010000.00 po-tmp/debian-reference.pot
po4a -v po4a-tmp.cfg
#rm -rf po-tmp


