#!/bin/bash

#   Copyright (C) 2005-2006 Tom Albers <tomalbers@kde.nl>
#   Copyright (C) 2006 Achim Bohnet <ach@mpe.mpg.de>
#
#   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 50 digikam-0.8.2/po digikam-0.8.1/po" or 
#       "./kdel10stat -l 50 digikam-0.8.2/po digikam-0.8.1/po"

debug () { return; }
#debug () { echo "$@"; }

export LANGAUGE=en
export LC_ALL=en_US.UTF-8

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;

# FIXME: assumes that at least one language has copied all language templates
S_NUMPO=`for d in $PAD/*/ ; do ls $d/*.po | wc -l; done | sort -n | uniq | tail -1`
S_NUMPO2=`for d in $PAD2/*/ ; do ls $d/*.po | wc -l; done | sort -n | uniq | tail -1`

debug $S_NUMPO $S_NUMPO2

echo -n "processing: "
for lang in `find $PAD -maxdepth 1 -type d -follow -printf '%f\n' | sed -n '2,$p'|sort`; do
    S_TRANS=0
    S_NOTRANS=0

    NUMPO=0 
    for po in `ls $PAD/$lang/*.po`; do
	let 'NUMPO++'

        # Make some stats!
        STATS=`msgfmt --statistics $po 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;

        S_TRANS=`expr $S_TRANS + $TRANS`
        S_NOTRANS=`expr $S_NOTRANS + $FUZZY + $UNTRANS`
    done

    TOT=`expr $S_TRANS + $S_NOTRANS`
    PERC=`expr $[ $S_TRANS * 100 * $NUMPO / $TOT / $S_NUMPO ]`

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

    echo -n "$lang($PERC%) "
    debug "all_lang_min=$ALL_LANG_MIN"

    # Do the same for the PAD2
    if [ -d $PAD2/$lang/ ]; then
        S_TRANS2=0
        S_NOTRANS2=0

        NUMPO=0 
        for po in $PAD2/$lang/*.po; do
	    let 'NUMPO++'

            STATS=`msgfmt --statistics $po 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;

            S_TRANS2=`expr $S_TRANS2 + $TRANS`
            S_NOTRANS2=`expr $S_NOTRANS2 + $FUZZY + $UNTRANS`
        done

        TOT2=`expr $S_TRANS2 + $S_NOTRANS2`
        PERC2=`expr $[ $S_TRANS2 * 100 * $NUMPO / $TOT2 / $S_NUMPO2 ]`

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

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

    else
        NEWLANG+=$lang" ";
    fi

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

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

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

echo done.
echo

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\n"$COPY;
    echo -e "We welcome "$NEWLANG"as new languages"
    echo -e "The following languages have improved their translations with "$MIN_PERC"% or more: "$IMPROVED
    echo -e "The following languages have 100% translations for the second release in row: "$AGAIN100
fi

