#! /bin/bash

# This script will do the following:
#   - Update your code
#   - Extract new strings and push to the .po files
#   - Compile all .po files
#   - Commit all your changes
#   - Email the l10n list
#
# This script makes a lot of assumptions and has no error checking, so read it
# over before you run it.
#
# You'll need to install both commonplace and uglify-js globally for this script
# to work.  Try this:
#
#    npm install -g commonplace
#    npm install -g uglify-js
#
# Questions?  Talk to clouserw.


EMAIL_FROM="Marketplace Developers <dev-marketplace@lists.mozilla.org>"
EMAIL_TO="Awesome Localizers <dev-l10n-web@lists.mozilla.org>"
EMAIL_SUBJECT="[Mkt/fireplace] .po files updated"

# A link to the .po files
EMAIL_SOURCE="https://github.com/mozilla/fireplace/tree/master/locale"

# gettext flags
CLEAN_FLAGS="--no-obsolete --width=200"
MERGE_FLAGS="--update --no-fuzzy-matching --width=200 --backup=none"

# -------------------------------------------------------------------
unset DOALLTHETHINGS

function confirm {
    if [ ! -z $DOALLTHETHINGS ]; then
        return 0
    fi

    PROMPT=$1
    read -p "$PROMPT [Y/n]: " YESNO
    if [[ $YESNO == 'n' ]]
    then
        return 1
    else
        return 0
    fi
}

if [[ "$1" == "--do-all-the-things" ]]; then
    DOALLTHETHINGS=1
fi

if [ ! -d "locale" ]; then
    echo "Sorry, please run from the root of the project, eg.  ./locale/omg_new_l10n.sh"
    exit 1
fi

if [ ! -z "$(git status --porcelain)" ]; then
    echo "Looks like you have some local changes.  Please clean up your root before we start committing random things."
    git status
    exit 1
fi

echo "Alright, here we go..."

if confirm "Update to the latest code?"; then
    git co master --quiet
    git pull
fi

if confirm "Extract new strings?"; then
    commonplace extract_strings
fi

if confirm "Merge new strings to .po files?"; then
    pushd locale > /dev/null

    echo "Merging any new keys..."
    for i in `find . -name "messages.po" | grep -v "en_US"`; do
        msgmerge $MERGE_FLAGS "$i" "templates/LC_MESSAGES/messages.pot"
    done
    msgen templates/LC_MESSAGES/messages.pot | msgmerge $MERGE_FLAGS en_US/LC_MESSAGES/messages.po -

    echo "Cleaning out obsolete messages.  See bug 623634 for details."
    for i in `find . -name "messages.po"`; do
        msgattrib $CLEAN_FLAGS --output-file=$i $i
    done

    popd > /dev/null
fi

if confirm "Process your debug languages?"; then
    podebug --rewrite=unicode locale/templates/LC_MESSAGES/messages.pot locale/dbg/LC_MESSAGES/messages.po
    podebug --rewrite=flipped locale/templates/LC_MESSAGES/messages.pot locale/rtl/LC_MESSAGES/messages.po
fi

if confirm "Convert Cyrillic Serbian to Latin?"; then
    pushd locale > /dev/null
    msgfilter -i sr/LC_MESSAGES/messages.po -o sr_Latn/LC_MESSAGES/messages.po recode-sr-latin
    popd > /dev/null
fi

if [ -z "$(git status --porcelain)" ]; then
    echo "Looks like there are no new strings to commit."
    exit 0
fi

if confirm "Commit your changes?"; then
    git commit locale -m "L10n Extract/compile script. Today's lucky number is $RANDOM."
    git push mozilla master
fi

echo "Calculating changes...."
pushd locale > /dev/null
CHANGES=$(cat <<MAIL
From: $EMAIL_FROM
To: $EMAIL_TO
Subject: $EMAIL_SUBJECT

Hi,

I am an automated script letting you know some .po files have just been
updated.  Unless something unusual is happening, we do weekly pushes on
Tuesdays so any strings committed by then will go live.  To give you an idea of
the number of new strings I will calculate untranslated strings below.

`./stats-po.sh .`

Source files: $EMAIL_SOURCE

If you have any questions please reply to the list.

Thanks so much for all your help!


MAIL
)
popd > /dev/null

echo "-----------------------------------------------"
echo "$CHANGES"
echo "-----------------------------------------------"

# Uses sendmail so we can set a real From address
if confirm "Do you want to send that to $EMAIL_TO?"; then
    echo "$CHANGES" | /usr/lib/sendmail -t
fi

unset DOALLTHETHINGS
echo "done."
