#!/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 (VERSION "\([^"]*\)".*)/\1/p' CMakeLists.txt`

xgettext \
    -d gammu \
    --msgid-bugs-address=michal@cihar.com \
    -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 \
    `find gammu helper -name '*.c' -o -name '*.h' | sort`

xgettext \
    -d libgammu \
    --msgid-bugs-address=michal@cihar.com \
    -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 \
    `find libgammu -name '*.c' -o -name '*.h' | sort`

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

for loc in $LOCS ; do
    cd locale/$loc
    msgmerge -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 -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 -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 -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 -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 -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
