#! /bin/sh

usage()
{
    echo "This script counts how many strings of the given localization differs from"
    echo "the English(US) localization"
    echo
    echo "Usage:" ${0##*/} locale ...
    echo
    echo "Presumptions:"
    echo "	- the module transex3 is built"
    echo "	- the profile *Env.Set* is sourced"
    echo
    echo "Note that the script is quite slow. It takes some minutes to extract strings"
    echo "for one localization..."
}

if test -z "$1"  -o  "$1" = "--help" ; then
    usage && exit 1;
fi

if ! which localize >/dev/null 2>&1 ; then
    echo "Error: Unable to find the script \"localize\". Please, build and deliver"
    echo "       the module transex3 and keep the \*Env.Set\* sourced."
    exit 1;
fi

extract_gsi()
{
   echo "Extracting $1 strings..."
   localize -e -f "$2" -l "$1=$primary_lang" >/dev/null 2>&1
}

primary_lang="en-US"

final_stat=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`


primary_gsi=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
extract_gsi $primary_lang $primary_gsi
primary_strings_ui=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
grep -v "^helpcontent2" $primary_gsi | sort | cut -f 1,2,5,11 >$primary_strings_ui
primary_strings_help=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
grep "^helpcontent2" $primary_gsi | sort | cut -f 1,2,5,11 >$primary_strings_help

primary_num_ui=`cat $primary_strings_ui | wc -l`
primary_num_help=`cat $primary_strings_help | wc -l`


for secondary_lang in $* ; do

    secondary_gsi=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
    extract_gsi $secondary_lang $secondary_gsi
    secondary_strings_ui=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
    grep -v "^helpcontent2" $secondary_gsi | sort | cut -f 1,2,5,11 >$secondary_strings_ui
    secondary_strings_help=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
    grep "^helpcontent2" $secondary_gsi | sort | cut -f 1,2,5,11 >$secondary_strings_help

    echo "Counting $secondary_lang localized strings..."

    secondary_diff_num_ui=`diff $primary_strings_ui $secondary_strings_ui | grep "^>" | wc -l`
    secondary_diff_num_help=`diff $primary_strings_help $secondary_strings_help | grep "^>" | wc -l`

    #echo primary_num_ui=$primary_num_ui
    #echo secondary_diff_num_ui=$secondary_num_ui

    #echo primary_num_help=$primary_num_help
    #echo secondary_diff_num_help=$secondary_num_help

    localized_ui=$(($secondary_diff_num_ui * 100 / $primary_num_ui))
    localized_help=$(($secondary_diff_num_help * 100 / $primary_num_help))

    echo
    echo "Locale:$secondary_lang	ui:${localized_ui}%		help:${localized_help}%"
    echo

    # 
    echo "Locale:$secondary_lang	ui:${localized_ui}%		help:${localized_help}%" >>$final_stat
    
    rm $secondary_strings_ui
    rm $secondary_strings_help
    rm $secondary_gsi
done

rm $primary_strings_ui
rm $primary_strings_help
rm $primary_gsi

echo
echo "==============================================================="
echo "Status of localizations"
echo "==============================================================="
sort -n -t':' --key=4 $final_stat

rm $final_stat
