#!/bin/bash

PACKAGE="user-installed-packages"

unset PO_DIRS
declare -A PO_DIRS=(
    ${PACKAGE}-help-mx  help-po
    ${PACKAGE}-mx       po
)
unset MIN_PERC
declare -A MIN_PERC=(
    ${PACKAGE}-help-mx  100
    ${PACKAGE}-mx        30
)

unset RESOURCES
declare -a RESOURCES=( "${!PO_DIRS[@]}" )


# need 'latest' transifex client
[ -z "$TXBIN" ] && [ -f ./tx ] && [ -x ./tx ] && TXBIN=./tx 
: ${TXBIN:=$(which tx)}
[ ! -x "$TXBIN" ] && echo "Need 'tx' transifex client! exit" && exit 1

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

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

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

EOF

for RESOURCE in ${RESOURCES[@]}; do

    POTFILE=${RESOURCE%-mx}.pot
    PODIR=${PO_DIRS[${RESOURCE}]}

    cat <<EOF >> .tx/config
[o:${ORGANIZATION}:p:${PROJECT}:r:${RESOURCE}]

file_filter   = ${PODIR}/<lang>.po
minimum_perc  = ${MIN_PERC[${RESOURCE}]}
resource_name = ${RESOURCE}
source_file   = ${POTFILE}
source_lang   = en
type          = PO

EOF

done

echo "#---- .tx/config -----"
cat .tx/config

for RESOURCE in ${RESOURCES[@]}; do
    POTFILE=${RESOURCE%-mx}.pot
    RESOUCE_ID="${PROJECT}.${RESOURCE}"
    echo "" 
    echo "To push pot-file ${POTFILE} to transifex" 
    echo "Run:" 
    echo ${TXBIN} push --source  "$RESOUCE_ID"
done
