#! /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" >/dev/null 2>&1
}

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

primary_lang="en-US"

primary_gsi=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
extract_gsi $primary_lang $primary_gsi
primary_strings=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
cat $primary_gsi | cut -f 1,11 >$primary_strings

primary_single_num=`grep -v "^helpcontent2" $primary_strings | sort -u | wc -l`
primary_single_num_help=`grep "^helpcontent2" $primary_strings | sort -u | wc -l`


for secondary_lang in $* ; do

    secondary_gsi=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
    extract_gsi $secondary_lang $secondary_gsi
    secondary_strings=`mktemp /tmp/ooo-stat-localizationse.XXXXXXXX`
    cat $secondary_gsi | cut -f 1,11 >$secondary_strings

    echo "Counting $secondary_lang strings..."

    single_num=`grep -h -v "^helpcontent2" $primary_strings $secondary_strings | sort -u | wc -l`
    secondary_single_num=`grep -v "^helpcontent2" $secondary_strings | sort -u | wc -l`
    single_num_help=`grep -h "^helpcontent2" $primary_strings $secondary_strings | sort -u | wc -l`
    secondary_single_num_help=`grep "^helpcontent2" $secondary_strings | sort -u | wc -l`

    #echo single_num=$single_num
    #echo primary_single_num=$primary_single_num
    #echo secondary_single_num=$secondary_single_num

    #echo single_num_help=$single_num_help
    #echo primary_single_num_help=$primary_single_num_help
    #echo secondary_single_num_help=$secondary_single_num_help

    localized=$((($single_num-$primary_single_num) * 100 / $primary_single_num))
    localized_help=$((($single_num_help-$primary_single_num_help) * 100 / $primary_single_num_help))

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

    # 
    echo "Locale:$secondary_lang	ui:${localized}%		help:${localized_help}%" >>$final_stat
    
    rm $secondary_strings
    rm $secondary_gsi
done

rm $primary_strings
rm $primary_gsi

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

rm $final_stat
