#!/bin/bash

FUZZY_COLOR=#ffd800
UNTRTD_COLOR=#ff0000
TRTD_COLOR=#00ff06
WIDTH=400
HEIGHT=20

function usage() {
    echo "Usage: $0 <dir with .po files> .pot <path to images>"
    exit 1
}

PO_DIR=$1
POT_FILE=$2
IMAGES_DIR=$3

MSGSTATS=`dirname $0`/msgstats

test -d $1 || usage
test -e $2 || usage

test -d $IMAGES_DIR || mkdir -p $IMAGES_DIR
rm -f $IMAGES_DIR/translation_stats
for po in `ls $PO_DIR/*.po`; do
    image=`basename $po .po`.png
    echo -n "Generating stats for locale ${image%.png}..."
    fuzzy_x=`$MSGSTATS $POT_FILE $po | \
	awk -v width=$WIDTH '/fuzzy:/ {fuzzy = $2} 
             /total:/ {total = $2}
             END {print int((fuzzy/total)*width)}'`
    untr_x=`$MSGSTATS $POT_FILE $po | \
	awk -v width=$WIDTH '/untranslated:/ {untr = $2} 
             /total:/ {total = $2}
             END {print int((untr/total)*width)}'`
    untrtd_x0=$(( $WIDTH-$untr_x))
    untrtd_x1=$(( $WIDTH ))
    fuzzy_x0=$(( $untrtd_x0-$fuzzy_x ))
    fuzzy_x1=$(( $untrtd_x0 ))
    y1=$(( $HEIGHT-1 ))
    convert -size ${WIDTH}x${HEIGHT} xc:${TRTD_COLOR} \
	    -draw "fill $FUZZY_COLOR rectangle $fuzzy_x0,0 $fuzzy_x1,19
                   fill $UNTRTD_COLOR rectangle $untrtd_x0,0 $untrtd_x1,19" \
	    $IMAGES_DIR/$image
    tran_fuzzy=`$MSGSTATS $POT_FILE $po | \
      awk '/untranslated:/ {untr = $2} 
           /fuzzy:/ {fuzzy = $2; if(fuzzy < 1) fuzzy = 0; }
           /total:/ {total = $2}
           END {print total-untr-fuzzy, fuzzy}'`
    echo "$image $tran_fuzzy" >> $IMAGES_DIR/translation_stats
    echo " done"
done
