#!/bin/bash

# Midnight Commander - common functions for playing with translations.
#
# Copyright (C) 2013
# The Free Software Foundation, Inc.
#
# Written by:
#  Slava Zanko <slavazanko@gmail.com>, 2013
#
# This file is part of the Midnight Commander.
#
# The Midnight Commander is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# The Midnight Commander is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#*** include section (source functions, for example) *******************

#*** file scope functions **********************************************
# ----------------------------------------------------------------------

## Returns name of working directory.
##
## @param sync_file_name file name which will be sync'ed with Transifex
## @return name of working directory

getSyncDirName() {
    sync_file_name=$1; shift
    echo "${SYNC_TX_CURRENT_DIR}/var.d/${sync_file_name}"
}

# ----------------------------------------------------------------------

## Returns config file name as full path.
##
## @param sync_file_name   file name which will be sync'ed with Transifex
## @param config_file_name config file name
## @return config file name

getConfigFile() {
    sync_file_name=$1; shift
    config_file_name=$1; shift

    echo "${MC_SOURCE_ROOT_DIR}/maint/utils/sync-transifex/config.d/${sync_file_name}/${config_file_name}"
}

# ----------------------------------------------------------------------


## Returns name of working directory. Init the directory, if needed.
##
## @param sync_file_name file name which will be sync'ed with Transifex
## @return name of working directory

initSyncDirIfNeeded() {
    sync_file_name=$1; shift

    sync_dir_name=$(getSyncDirName "${sync_file_name}")

    [ ! -e  "${sync_dir_name}" ] && mkdir -p "${sync_dir_name}/.tx"
    cp \
        "$(getConfigFile ${sync_file_name} 'tx.config')" \
        "${sync_dir_name}/.tx/config"

    echo "${sync_dir_name}"
}

# ----------------------------------------------------------------------

## Convert file from plain text to POT-file format.
##
## @param source_file      path to plain text file
## @param destination_file path to po-file

convertFromTextToPo() {
    source_file=$1; shift
    destination_file=$1; shift

    po4a-gettextize -f text -m "${source_file}" -p "${destination_file}"
}

# ----------------------------------------------------------------------

## Send POT-file to Transifex.
##
## @param tx_work_dir working directory where placed the POT file and
##                    a config-file for tx utility

sendSourceToTransifex() {
    tx_work_dir=$1; shift

    tx -r "${tx_work_dir}" push -s
}

# ----------------------------------------------------------------------

## Reveive translations from Transifex.
##
## @param tx_work_dir working directory where placed the POT file and
##                    a config-file for tx utility

receiveTranslationsFromTransifex() {
    tx_work_dir=$1; shift

    pushd "${tx_work_dir}" >/dev/null
    tx pull --all --force
    popd >/dev/null
}

# ----------------------------------------------------------------------

## Create po4a.cfg file.
##
## @param sync_file_name file name which will be sync'ed with Transifex

createPo4A() {
    sync_file_name=$1; shift

    sync_dir_name=$(getSyncDirName "${sync_file_name}")
    [ ! -e  "${sync_dir_name}" ] && mkdir -p "${sync_dir_name}"

    pushd $sync_dir_name >/dev/null
    translations=$(ls *.po| sed -e 's/.po//g'| tr '\n' ' ')
    popd >/dev/null
    sed -e 's!@srcdir@!'"${MC_SOURCE_ROOT_DIR}"'!g' \
        -e 's!@translations@!'"${translations}"'!g' \
        "$(getConfigFile ${sync_file_name} 'po4a.cfg')" \
        > "${sync_dir_name}/po4a.cfg"
}

# ----------------------------------------------------------------------

## Create po4a.cfg file.
##
## @param sync_dir_name   working directory where placed translations
## @param sync_file_name file name which will be sync'ed with Transifex

convertFromPoToText() {
    sync_dir_name=$1; shift
    sync_file_name=$1; shift
    pushd "${SYNC_TX_CURRENT_DIR}"  >/dev/null
    po4a -k 0 -q \
        -M UTF-8 -L UTF-8 \
        --variable docfile="${sync_file_name}" \
        "${sync_dir_name}/po4a.cfg"
    popd  >/dev/null
}

# ----------------------------------------------------------------------
#*** main code *********************************************************

SYNC_TX_CURRENT_DIR=${SYNC_TX_CURRENT_DIR:-${MC_SOURCE_ROOT_DIR}/maint/utils/sync-transifex}
