#!/usr/bin/env bash

# Firstly this script must be ALWAYS run after pomerge, so run it here
`which kde-pomerge`

SCATTERING_DIRS=(
    "trunk/l10n-kde4"
    "trunk/l10n-kf5"
    "branches/stable/l10n-kde4"
    "branches/stable/l10n-kf5"
)


# Update xml files
update_xml_files() {
    pushd "${KDEREPO_PATH}"/$1 > /dev/null || exit 1
    ./scripts/update_xml ${KDE_LANG} || exit 1
    popd > /dev/null || exit 1
}

# Update one of the specified branches translations
update_branch_translation() {
    pushd "${KDEREPO_PATH}"/$1/${KDE_LANG} > /dev/null || exit 1

    echo "Removing fuzzy string from $1"
    find ./ -name \*.po |xargs -i msgattrib {}  --no-fuzzy -o {}

    echo "Removing obsolete string from $1"
    find ./ -name \*.po |xargs -i msgattrib {}  --no-obsolete -o {}

    echo "Updating files from pot files to compensate removed fuzzy lines"
    find ./ -name \*.po | xargs -i msgmerge -NU {} ../templates/{}t

    echo "Commiting in all the changes on $1 branch"
    svn ci -m "Update from Summit" || exit 1

    popd > /dev/null || exit 1
}

. /usr/share/kde-l10n-scripts/common.sh

pushd "${KDEREPO_PATH}"/trunk/l10n-support > /dev/null || exit 1

echo "Merging all data from summit repository"
posummit scripts/messages.summit ${KDE_LANG} scatter || exit 1
posummit scripts/docmessages.summit ${KDE_LANG} scatter || exit 1

popd > /dev/null || exit 1

for scatdir in ${SCATTERING_DIRS[@]}; do
    update_xml_files ${scatdir}
    update_branch_translation ${scatdir}
done
