#!/bin/sh
# vim: expandtab sw=4 ts=4 sts=4:

set -e

LOCS=`ls locale/*/gammu.po | sed 's@.*/\(.*\)/[^/]*@\1@'`
ver=`sed -n 's/set (GAMMU_VERSION "\([^"]*\)".*)/\1/p' CMakeLists.txt`

xgettext \
    -d gammu \
    --msgid-bugs-address=gammu-users@lists.sourceforge.net \
    -o locale/gammu.pot \
    --language=C \
    --keyword=_ \
    --keyword=N_ \
    --add-comments=l10n \
    --add-location \
    --copyright-holder "Michal Čihař <michal@cihar.com>" \
    --package-name gammu \
    --package-version $ver \
    --from-code utf-8 \
    `find gammu helper gammu-detect -name '*.c' -o -name '*.h' -not -name 'locales.h' | sort`

xgettext \
    -d libgammu \
    --msgid-bugs-address=gammu-users@lists.sourceforge.net \
    -o locale/libgammu.pot \
    --language=C \
    --keyword=_ \
    --keyword=N_ \
    --add-comments=l10n \
    --add-location \
    --copyright-holder "Michal Čihař <michal@cihar.com>" \
    --package-name libgammu \
    --package-version $ver \
    --from-code utf-8 \
    `find libgammu -name '*.c' -o -name '*.h' | sort`

if [ "x$1" != "x--no-po4a" ] ; then
    ./admin/po4a-docs
fi

for loc in $LOCS ; do
    cd locale/$loc
    msgmerge --previous -U -C ../../../wammu/locale/$loc/docs.po -C ../../../wammu/locale/$loc/wammu.po -C gammu.po -C libgammu.po docs.po ../docs.pot
    if [ -f  ../../..//wammu_web/locale/$loc/LC_MESSAGES/django.po ] ; then
        msgmerge --previous -C ../../../wammu_web/locale/$loc/LC_MESSAGES/django.po -U docs.po ../docs.pot
    fi
    cd ../..
done

sed -i '
    s/SOME DESCRIPTIVE TITLE/Gammu translation/;
    s/PACKAGE/Gammu/;
    s/(C) YEAR/(C) 2003 - '`date +%Y`'/;
    s/VERSION/'$ver'/;
    ' locale/gammu.pot
sed -i '
    s/SOME DESCRIPTIVE TITLE/libGammu translation/;
    s/PACKAGE/libGammu/;
    s/(C) YEAR/(C) 2003 - '`date +%Y`'/;
    s/VERSION/'$ver'/;
    ' locale/libgammu.pot

for loc in $LOCS ; do
    sed -i '
        s/SOME DESCRIPTIVE TITLE/Gammu translation/;
        s/PACKAGE/Gammu/;
        s/VERSION/'$ver'/;
        s/Project-Id-Version: Gammu [0-9.]*/Project-Id-Version: Gammu '$ver'/;
        ' locale/$loc/gammu.po
    msgmerge --previous -C ../wammu/locale/$loc/docs.po -C ../wammu/locale/$loc/wammu.po -C locale/$loc/libgammu.po -C locale/$loc/docs.po -U locale/$loc/gammu.po locale/gammu.pot
    if [ -f  ../wammu_web/locale/$loc/LC_MESSAGES/django.po ] ; then
        msgmerge --previous -C ../wammu_web/locale/$loc/LC_MESSAGES/django.po -U locale/$loc/gammu.po locale/gammu.pot
    fi
    sed -i '
        s/SOME DESCRIPTIVE TITLE/libGammu translation/;
        s/PACKAGE/libGammu/;
        s/VERSION/'$ver'/;
        s/Project-Id-Version: libGammu [0-9.]*/Project-Id-Version: libGammu '$ver'/;
        ' locale/$loc/libgammu.po
    msgmerge --previous -C ../wammu/locale/$loc/docs.po -C ../wammu/locale/$loc/wammu.po -C locale/$loc/gammu.po -C locale/$loc/docs.po -U locale/$loc/libgammu.po locale/libgammu.pot
    if [ -f  ../wammu_web/locale/$loc/LC_MESSAGES/django.po ] ; then
        msgmerge --previous -C ../wammu_web/locale/$loc/LC_MESSAGES/django.po -U locale/$loc/libgammu.po locale/libgammu.pot
    fi
    sed -i '
        s/SOME DESCRIPTIVE TITLE/Gammu documentation translation/;
        s/PACKAGE/Gammu-docs/;
        s/(C) YEAR/(C) 2003 - '`date +%Y`'/;
        s/VERSION/'$ver'/;
        s/Project-Id-Version: Gammu-docs [0-9.]*/Project-Id-Version: Gammu-docs '$ver'/;
        ' locale/$loc/docs.po
done
