#!/bin/sh

#   Copyright (C) 2005-2006 Tom Albers <tomalbers@kde.nl>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


# Usage "./kdel10stat -L 48 digikam-0.8.2/po digikam-0.8.1/po" or 
#       "./kdel10stat -l 48 digikam-0.8.2/po digikam-0.8.1/po"

MSGFMT=`which msgfmt`
if [ "$MSGFMT" == "" ] 	   ; then echo "please install msgfmt and make sure it is in your path, can not continue"; exit; fi;

MIN_PERC=0;
if [ "$1" == "-L" ]; then MIN_PERC=$2; PAD=$3; PAD2=$4
else
    if [ "$1" == "-l" ]; then MIN_PERC=$2; PAD=$3; PAD2=$4 
    else
        exit;
    fi
fi;

for i in `find $PAD -maxdepth 1 -type d -follow -printf '%f\n' | sed -n '2,$p'`
do
for j in `ls $PAD/$i/*.po`
do

    # Make some stats!
    STATS=`msgfmt --statistics $j 2>&1`

    TRANS=`echo $STATS |sed 's/^\(.*\) translated message.*/\1/'`
    if [ "$TRANS" == "$STATS" ]; then TRANS=0; fi;

    FUZZY=`echo $STATS |sed '/.*, \(.*\) fuzzy translation.*/ s//\1/'`
    if [ "$FUZZY" == "$STATS" ]; then FUZZY=0; fi;

    UNTRANS=`echo $STATS |sed '/.*, \(.*\) untranslated messages.*/ s//\1/'`
    if [ "$UNTRANS" == "$STATS" ]; then UNTRANS=0; fi;

    NOTRANS=`expr $FUZZY + $UNTRANS`;
    TOT=`expr $TRANS + $NOTRANS`
    PERC=`expr $[ $TRANS * 100 / $TOT]`

    if [ `expr $[ $PERC > $MIN_PERC ]` = 1 ]
    then 
        ALL_LANG_MIN+=$i"("$PERC"%) ";
    fi


    # Do the same for the PAD2
    if [ -e $PAD2/$i/*.po ];
    then
        STATS=`msgfmt --statistics $PAD2/$i/*.po 2>&1`

        TRANS2=`echo $STATS |sed 's/^\(.*\) translated message.*/\1/'`
        if [ "$TRANS2" == "$STATS" ]; then TRANS2=0; fi;

        FUZZY2=`echo $STATS |sed '/.*, \(.*\) fuzzy translation.*/ s//\1/'`
        if [ "$FUZZY2" == "$STATS" ]; then FUZZY2=0; fi;

        UNTRANS2=`echo $STATS |sed '/.*, \(.*\) untranslated messages.*/ s//\1/'`
        if [ "$UNTRANS2" == "$STATS" ]; then UNTRANS2=0; fi;

        NOTRANS2=`expr $FUZZY2 + $UNTRANS2`;
        TOT2=`expr $TRANS2 + $NOTRANS2`
        PERC2=`expr $[ $TRANS2 * 100 / $TOT2]`

        DIFF=`expr $PERC - $PERC2`
        if [ $DIFF -gt $MIN_PERC ]
        then
            IMPROVED+=$i" ";
        fi

        if [ $DIFF == 0 ] && [ $PERC == 100 ]
        then
            AGAIN100+=$i" ";
        fi

    else
        NEWLANG+=$i" ";
    fi

    # Save all languages
    ALL_LANG+=$i" ";

    # Copyrights
    if [ "$1" == "-L" ];
    then 
        COPYRIGHT=`grep '^#.*@.*' $j | sed 's/# \(.*\)/\1/' | sed 's/ /easywayout/g'`
        COPYRIGHT2=`grep 'Last-Translator' $j | sed 's/.*Last-Translator: \(.*\)\\\\n.*/Last Translator: \1/' | sed 's/ /easywayout/g'`
        COPY+=$i":\n";
        for k in $COPYRIGHT2 $COPYRIGHT
        do
            COPY+="\t"
            COPY+=`echo $k | sed 's/easywayout/ /g'` 
            COPY+="\n"
        done
    else
        COPYRIGHT2=`grep 'Last-Translator' $j | sed 's/.*Last-Translator: \(.*\)\\\\n.*/\1/' | sed 's/ /easywayout/g'`
        for k in $COPYRIGHT2
        do
            COPY+="\t"
            COPY+=`echo $k | sed 's/easywayout/ /g'` 
        done
        COPY+=" ("$i")";
        COPY+="\n"
    fi 

    AMOUNT=`expr $AMOUNT + 1`;
done;
done;

if [ "$1" == "-L" ];
then
    echo "All languages available: " $ALL_LANG;
    echo "All Languages with "$MIN_PERC"% translated: "$ALL_LANG_MIN;
    echo -e "Copyrights:\n"$COPY;
else
    echo -e "There are "$AMOUNT" languages available, this is a list of the current translators:\n"$COPY;
    echo -e "We welcome "$NEWLANG"as new languages"
    echo -e "The following languages have improved there translations with "$MIN_PERC"% or more: "$IMPROVED
    echo -e "The following languages have 100% translations for the second release in row: "$AGAIN100
fi

