#!/bin/bash
#
# bin/gettext-makemo
# Convert all .po files of translations into .mo files
# --help - show usage
# --verbose - show status of all translations
#
# Copyright (c) 2005 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: gettext-makemo,v 1.16 2010-01-25 15:21:11 louise Exp $

# don't expand a glob with no matches to itself
shopt -s nullglob

ORIGINAL_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ "$1" = "--help" ]
then 
    echo "Usage: gettext-makemo [--help] [--verbose|--quiet] [domain]"
    echo "If no domain is specified, all files will be converted"
    exit 0
fi 

if [ -e ../../locale ]
then
    cd ../../
fi

if [ -e ../locale ]
then
    cd ../
fi

if [ ! -d locale ]
then
    echo "Run in root mysociety directory with locale directory just below"!
    exit
fi

MSGFMT_PARAMS="-v"
if [ "$1" = "--quiet" ]
then
    MSGFMT_PARAMS=""
fi

for X in locale/*.UTF-8
do
    if [ -d $X ]
    then
        cd $X/LC_MESSAGES
        ALL_PO="*.po"
        if [ "$2" != "" ]
        then         
            ALL_PO="$2*.po"
        fi
        # Convert all .po files to .mo files
        for PO in $ALL_PO
        do
            # count number of times Subject: appears in original, times two
            ORIG="../../$PO"
            SUBJECT_COUNT_ORIG=0
            if [ -f $ORIG ]
            then
                SUBJECT_COUNT_ORIG=`grep '^"Subject: ' "../../$PO" "../../$PO" | egrep -v "^#" | wc -l`
            fi
            MO=${PO/.po/.mo}
            [ "$1" != "--quiet" ] && echo -n "$X $PO: " 
            msgfmt -c $MSGFMT_PARAMS -o $MO $PO
            SUBJECT_COUNT=`grep '^"Subject: ' $PO  | egrep -v "^#" | wc -l`
            if [ "$SUBJECT_COUNT" != "$SUBJECT_COUNT_ORIG" ] && [ "$SUBJECT_COUNT_ORIG" != 0 ] 
            then
                echo "WARNING: Email 'Subject: ' lines have probably been translated in $X' (got $SUBJECT_COUNT, $SUBJECT_COUNT_ORIG expected from locale/$PO)"
            fi
            cat $PO | $ORIGINAL_PATH/gettext-custom-validations
        done

        cd ../../../
    fi
done

