#!/bin/bash

#  fehlix test project do not use
#: ${ORGANIZATION:=fehlix}
#: ${PROJECT:=testproject-do-not-use}

# set transifex organization and project name - if not set in environment already
: ${ORGANIZATION:=anticapitalista}
: ${PROJECT:=antix-development}


echodo() { echo "${@}";  ${@}; }

# need 'latest' transifex client
: ${TXBIN:=$(which tx)}
[ ! -x "$TXBIN" ] && echo "Error: transifex client not found!" && exit 1

# prepare transifex 
# get package name from changelog
CHNGLOG=$(find {,../,../../}debian/changelog  -type f -name changelog -print -quit 2>/dev/null)
PKGNAME=$(dpkg-parsechangelog -l "$CHNGLOG" -SSource)

# set transifex resource name here if it is not the package name
RESOURCE=$PKGNAME
# set transifex resource_id
RESOUCE_ID="${PROJECT}.${RESOURCE}"
PODIR=po
POTFILE=$PKGNAME.pot
# set minium translations completion in percent to pull translation
: ${MINIMUM_PERC:=30}

[ -d .tx         ] || mkdir -p .tx
[ -f  .tx/config ] && rm  .tx/config

cat <<EOF > .tx/config
[main]
host = https://app.transifex.com

[o:${ORGANIZATION}:p:${PROJECT}:r:${RESOURCE}]

file_filter = ${PODIR}/<lang>.po
minimum_perc = ${MINIMUM_PERC:=5}
resource_name = ${RESOURCE}
source_file = ${POTFILE}
source_lang = en
type = PO
EOF

# backup existing - if any yet
if [ -d ${PODIR} ]; then
    for PO in ${PODIR}/*.po; do
        [ -e "$PO" ] || continue
        mv $PO $PO~
    done
fi 

echodo  ${TXBIN} pull --translations --all "$RESOUCE_ID"

# make LINGUAS
LINGUAS=${PODIR}/LINGUAS
[ -f $LINGUAS ] && echodo rm $LINGUAS
touch $LINGUAS

cat<<LINGUAS | tee $LINGUAS
# LINGUAS with minimum completion percent ${MINIMUM_PERC}% 
# generated at $(TZ=UCT date -R)
#
LINGUAS

for po in ${PODIR}/*.po; do
    [ -e "$po" ] || continue
    lang=${po##*/}
    lang=${lang%.po}
    printf '%s ' "${lang}"
    echo "${lang}" >> $LINGUAS
done

