#!/bin/bash
#
# bin/gettext-extract
# Generate English language .po files from the source code and email templates,
# for all localisable mySociety projects. Writes the output to appropriate .po
# files in locale/.
#
# Copyright (c) 2005 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: gettext-extract,v 1.13 2007/05/01 10:10:47 francis Exp $

if [ ! -e ../mysociety ]
then
    echo "Please run with current directory main mysociety directory"
    echo "i.e. bin/gettext-extract"
    exit 1
fi

# File to write to, clear it to start with
PO=locale/PledgeBank.po 
rm -f $PO

# Useful functions

# Take chunk of text and escape each line in it for putting in catalogue
function plain_gettext_escape() {
    IFS=""
    while read LINE
    do
        LINE=${LINE//\"/\\\"}
        echo \"$LINE\\n\"
    done
}
# Write each line input as separate gettext message
# $1 - string to put in comment
function write_each_line_as_msg() {
    while read LINE
    do
        LINE=${LINE//\"/\\\"}
        echo >> $PO
        echo "#: $1" >> $PO
        echo msgid \"$LINE\" >> $PO
        echo msgstr \"\" >> $PO
    done
}

# Extract from PHP
xgettext --add-comments=TRANS --language=PHP --from-code=utf-8 -o $PO phplib/*.php pb/phplib/*.php pb/web/*.php pb/bin/frequentupdate pb/bin/send-comment-alerts pb/bin/send-local-alerts

# Extract from Python and JS (which seems to work if I tell it it's Python)
xgettext --add-comments=TRANS --language=Python --from-code=utf-8 --join-existing -o $PO pb/web/poster.cgi pb/web/pb.js

# Extract from Perl
xgettext --add-comments=TRANS --language=Perl --keyword=_ --from-code=utf-8 --join-existing -o $PO pb/web/graph.cgi  

# Fix headers
TEMP=`tempfile`
cat $PO | sed "
        s/SOME DESCRIPTIVE TITLE/PledgeBank original .po file, autogenerated by gettext-extract/;
        s/YEAR THE PACKAGE'S COPYRIGHT HOLDER/2005 UK Citizens Online Democracy/;
        s/PACKAGE package/main PledgeBank code/;
        s/FIRST AUTHOR <EMAIL@ADDRESS>, YEAR./Matthew Somerville <matthew@mysociety.org>, 2005-08-13./;

        s/PACKAGE VERSION/1.0/;
        s/Report-Msgid-Bugs-To: /Report-Msgid-Bugs-To: matthew@mysociety.org/;
        s/LL@li.org/team@pledgebank.com/;
" >> $TEMP
mv $TEMP $PO

# Extract email templates
echo >> $PO
echo '#. In all of these email templates, should the first word "Subject:" be translated or not? Is it machine-read or human-read? (Tim Morley, 2005-11-23)' >> $PO
echo "#. It's machine-read, yes, so please leave as \"Subject:\". (Matthew Somerville, http://www.mysociety.org/pipermail/mysociety-i18n/2005-November/000104.html)" >> $PO
for X in pb/templates/emails/*
do
    # TODO: Should check for "*~" type filenames too, and do the *-livesimply case
    # with wildcards rather than checking per template
    if [ "$X" != "pb/templates/emails/CVS" -a "$X" != "pb/templates/emails/README" -a "$X" != "pb/templates/emails/email-friends-livesimply" -a "$X" != "pb/templates/emails/failed-creator-livesimply" -a "$X" != "pb/templates/emails/failed-signer-livesimply" ]
    then
        echo >> $PO
        echo "#: $X" >> $PO
        echo msgid \"\" >> $PO
        cat $X | plain_gettext_escape >> $PO
        echo msgstr \"\" >> $PO
    fi
done

# PledgeBank categories
cat pb/db/ican-categories.sql | grep NULL | perl -p -e "/'(.*)'/; \$_=\$1.\"\n\";" | write_each_line_as_msg pb/db/ican-categories.sql
