#!/bin/bash
# KMidimon - ALSA Sequencer based MIDI Monitor
# Copyright (C) 2005-2013 Pedro Lopez-Cabanillas <plcl@users.sourceforge.net>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA

PROJECTNAME="kmidimon"
SOURCEDIR="src"
WORKDIR="po"
KDEDIR=$(kde4-config --prefix)
EXTRACTRC=$KDEDIR/bin/extractrc
# MSGID-Bugs
BUGADDR="http://sourceforge.net/tracker/?atid=731542&group_id=134956"

mkdir -p $WORKDIR
pushd $WORKDIR
rm -f *.old
rm -f rcfiles.list
rm -f infiles.list
rm -f rc.cpp
rm -f messages.pot
popd

echo "Preparing rc/ui/kcfg files"
find $SOURCEDIR -name '*.rc' -o -name '*.ui' -o -name '*.kcfg' | sort > ${WORKDIR}/rcfiles.list
xargs --arg-file=${WORKDIR}/rcfiles.list ${EXTRACTRC} > ${WORKDIR}/rc.cpp
echo 'i18nc("NAME OF TRANSLATORS","Your names");' >> ${WORKDIR}/rc.cpp
echo 'i18nc("EMAIL OF TRANSLATORS","Your emails");' >> ${WORKDIR}/rc.cpp

echo "Preparing sources list"
find $SOURCEDIR -name '*.cpp' -o -name '*.h' -o -name '*.c' | sort > ${WORKDIR}/infiles.list
echo "${WORKDIR}/rc.cpp" >> ${WORKDIR}/infiles.list

echo "Extracting messages"
pushd $WORKDIR
xgettext --from-code=UTF-8 -C -kde -ci18n -ki18n:1 -ki18nc:1c,2 -ki18np:1,2 -ki18ncp:1c,2,3 -ktr2i18n:1 \
         -kI18N_NOOP:1 -kI18N_NOOP2:1c,2 -kaliasLocale -kki18n:1 -kki18nc:1c,2 -kki18np:1,2 -kki18ncp:1c,2,3 \
         --msgid-bugs-address="${BUGADDR}" \
         --files-from=infiles.list -D.. -D. -o messages.pot || { echo "error while calling xgettext. aborting."; exit 1; }
if [ -e ${PROJECTNAME}.pot ]; then
    mv ${PROJECTNAME}.pot ${PROJECTNAME}.pot.old
fi
mv messages.pot ${PROJECTNAME}.pot

echo "Merging existing translations"
catalogs=$(find . -name '*.po')
for CATALOG in $catalogs; do
    msgmerge $CATALOG ${PROJECTNAME}.pot -o $CATALOG.new || { echo "Error merging catalog $CATALOG with msgmerge. Aborting."; exit 1; }
    mv $CATALOG $CATALOG.old
    mv $CATALOG.new $CATALOG
done

echo "Cleaning"
rm -f rcfiles.list
rm -f infiles.list
rm -f rc.cpp
popd
echo "Done"
