#!/bin/bash
#   Calculates statistics for po files/branches
#   Copyright (C) 2005 Greek KDE l10n Team.
#   Written by Spiros Georgaras <sngeorgaras@otenet.gr>, 2005.
#
#   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, 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.
#


function calculateDay(){
local dday=$(date '+%d')
#local mmonth=$(date '+%m'|sed 's|^0||')
local mmonth=$(date '+%m')
thisDay=$(date '+/%Y, ώρα: %H:%M')
thisDay="Ημερομηνία: $dday"/"$mmonth""$thisDay"
}

function fixPerCent(){
  local l=$(echo "$1" | sed 's|\.|,|' | sed 's|^,|0,|')
  if [ "$l" = "100,00" ];then l="100";fi
  echo "$l"
}

function fixBCPerCent(){
  local l=$(echo "$1" | sed 's|\.|,|' | sed 's|^,|0,|')
  if [ -z "$(echo "$l" | grep ',')" ];then
    if [ "$l" != "0" ];then
      l="$l"',00'
    fi
  fi
  if [ "$l" = "100,00" ];then l="100";fi
  echo "$l"
}


#############   Script starts here   #############
#
# Copy this to all scripts
if [ -e "$HOME"/.l10n.conf ];then
  . "$HOME"/.l10n.conf
  if [ -z "$installFolder" ];then
    echo `basename "$0"`"> Error: parameter installFolder not set..."
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  if [ ! -d "$installFolder" ];then
    echo `basename "$0"`"> Error: $installFolder (parameter installFolder)"
    echo  `basename "$0"`"         Does not exist or is not a folder" 
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  if [ ! -r "$installFolder" ];then
    echo `basename "$0"`"> Error: $installFolder (parameter installFolder)"
    echo `basename "$0"`"          No read permission to this folder"
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  . "$installFolder"/l10n-common
  parceConfig
  errorDetected=$?
else
  echo `basename "$0"`"> Error: ~/.l10n.conf - Not found"
  echo "A sample l10n.conf file can be found at
ftp://ftp.i18n.kde.org/teams/el/scripts"
  echo
  exit 1
fi
# End copy
#
rm "$cvsDIR"/*.stats 2>/dev/null

outName[0]="$cvsDIR"/stat-stable-all.html
outName[1]="$cvsDIR"/stat-stable.html
outName[2]="$cvsDIR"/stat-trunk-all.html
outName[3]="$cvsDIR"/stat-trunk.html
outName[4]="$cvsDIR"/stat-in-kde.html

brName[0]="stable"
brName[1]="stable (Τυπικά πακέτα)"
brName[2]="trunk"
brName[3]="trunk (Τυπικά πακέτα)"
brName[4]="Απαιτήσεις για είσοδο στο KDE"

calculateDay


fixThisFile "$cvsDIR"/index.html

cat "$cvsDIR"/index.html | sed 's|<[Ll][Ii] [Cc][Ll][Aa][Ss][Ss]="here">|<LI>|' | sed '/[hH][Rr][Ee][Ff]="el.css"/a \
<link rel="stylesheet"  TYPE="text/css" href="stats.css" />'> "$cvsDIR"/template.html



# | sed 's|<!--     <LINK REL="stylesheet"  TYPE="text/css" HREF="stats.css"> -->|<LINK REL="stylesheet"  TYPE="text/css" HREF="stats.css">|' > "$cvsDIR"/template.html

st=$(grep -n '<!-- Σημείο αρχής εγγραφής στατιστικών -->' "$cvsDIR"/template.html| sed 's|:.*||')
((st++))



sed ""$st",/<!-- Σημείο τέλους εγγραφής στατιστικών -->/d" "$cvsDIR"/template.html >"$cvsDIR"/template.html.bck
mv "$cvsDIR"/template.html.bck "$cvsDIR"/template.html


for n in `seq 0 4`;do

onlyOutFileName="$(echo "${outName[$n]}"| sed 's|.*/\([^/]*html$\)|\1|')"
sed "s|<[Hh]1>Καλώς ή[ρλ]θατε|<h1>Στατιστικά του κλάδου ${brName[$n]}<h2>$thisDay</h2>|" "$cvsDIR"/template.html \
| sed "s|Καλώς ή[ρλ]θατε|Στατιστικά του κλάδου ${brName[$n]}|" \
| sed "s|<[Ll][iI]><[Aa] [Hh][Rr][Ee][Ff]=\""$onlyOutFileName"|<li class=\"here\"><a href=\""$onlyOutFileName"|" \
| sed "/Τοποθεσία: <[Aa] [Hh][Rr][Ee][Ff]=\"index.html\" /s|$| / Στατιστικά του κλάδου ${brName[$n]}|" \
| sed "s|Στατιστικά του κλάδου Απαιτήσεις|Απαιτήσεις|" \
> "${outName[$n]}"


if [ $n -lt 4 ];then
sed '/<!-- Σημείο αρχής εγγραφής στατιστικών -->/a \
<div style=\"margin-top: 2em;\"> \
<TABLE ALIGN=\"center\" border=\"0\" width=\"100%\"> \
<TR> \
<td width=\"05%\"></td> \
<TD> \
  <h3>Σημείωση:</h3> \
  <ol> \
    <li> \
    Μόνο αρχεία PO για τα οποία υπάρχει το αντίστοιχο αρχείο POT συμπεριλαμβάνονται στα στατιστικά. \
    </li> \
    <li> \
    Το σύνολο των μηνυμάτων υπολογίζεται με βάση τον αριθμό των μηνυμάτων που υπάρχει στα αρχεί PO. Αν, για παράδειγμα, τα μεταφρασμένα αρχεία PO έχουν διαφορετικό αριθμό μηνυμάτων από τα αντίστοιχα αρχεία POT,  \
    μπορεί να υπάρχουν διαφορές. \
    </li> \
    <li> \
    Γραμμές με κόκκινο χρώμα φόντου δείχνουν ότι το αντίστοιχο πακέτο περιέχει τουλάχιστον ένα αρχείο με σφάλματα. \
    </li> \
    <li> \
    Είναι πιθανό το άθροισμα των μεταφρασμένων, αμετάφραστων και ασαφών μηνυμάτων να είναι διαφορετικό από το 100%. Αυτό οφείλεται σε σφάλματα κατά τη στρογγυλοποίηση. \
    </li> \
  </ol> \
  <h3>Όπου:</h3> \
  <ul> \
    <li> \
    <img src=\"./images/bar0.gif\" height=\"15\" width=\"30\" alt=\"Green bar\" /> - Μεταφρασμένα μηνύματα. \
    </li> \
    <li> \
    <img src=\"./images/bar4.gif\" height=\"15\" width=\"30\" alt=\"Blue bar\" /> - Ασαφή μηνύματα. \
    </li> \
    <li> \
    <img src=\"./images/bar1.gif\" height=\"15\" width=\"30\" alt=\"Red bar\" /> - Αμετάφραστα μηνύματα. \
    </li> \
  </ul> \
</TD> \
</TR> \
</TABLE> \
</div> \
<!-- Σημείο τέλους εγγραφής στατιστικών -->' "${outName[$n]}" > "${outName[$n]}".bck
mv "${outName[$n]}".bck "${outName[$n]}"

else

sed '/<!-- Σημείο αρχής εγγραφής στατιστικών -->/i \
<p>To KDE έχει ορίσει ότι για να συμπεριληφθούν τα αρχεία μετάφρασης μίας γλώσσας στην επόμενη έκδοσή του, θα πρέπει να ικανοποιούνται οι παρακάτω απαιτήσεις (από πλευράς ποσοστού μεταφρασμένων μηνυμάτων).<BR><BR></p>' "${outName[$n]}" | sed '/<!-- Σημείο αρχής εγγραφής στατιστικών -->/a \
<p>Αν κάποιο από τα παραπάνω συνολικά ποσοστά έχει τιμή διάφορη του 100% (και εμφανίζεται με κόκκινο χρώμα) σημαίνει ότι υπάρχει πρόβλημα στον αντίστοιχο κλάδο, το οποίο θα πρέπει να διορθωθεί πριν να κυκλοφορήσει η επόμενη έκδοση του KDE (αν πρόκειται για τον κλάδο stable), ή πριν την προετοιμασία για την επόμενη κύρια έκδοση του KDE (αν πρόκειται για τον κλάδο trunk).</p> \
<!-- Σημείο τέλους εγγραφής στατιστικών -->'> "${outName[$n]}".bck
mv "${outName[$n]}".bck "${outName[$n]}"



fi


done




for n in $(seq 0 3);do
  if [ $n -eq 0 ];then
    cd "$PO_STABLE"
    activeModules=`find . -type d -name "*" | grep -v '/\.svn' | sed 's|^\./||' | sort | uniq`
  elif [ $n -eq 1 ];then
    cd "$PO_STABLE"
    activeModules="$kdeModules4"
  elif [ $n -eq 2 ];then
    cd "$PO_TRUNK"
    activeModules=`find . -type d -name "*" | grep -v '/\.svn' | sed 's|^\./||' | sort | uniq`
  elif [ $n -eq 3 ];then
    cd "$PO_TRUNK"
    activeModules="$kdeModules4"
  else


    echo
  fi

echo "<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" BGCOLOR=\"#8b898b\" WIDTH=\"100%\">
<TBODY>
<TR>
<TD>
<TABLE CELLSPACING=\"1\" CELLPADDING=\"2\" BORDER=\"0\" WIDTH=\"100%\">
<TBODY>
<TR BGCOLOR=\"#e0e0e0\">
<TD><div class=\"statsArrayHeaderLeft\"><B>Πακέτο</B></div></TD>
<TD><div class=\"statsArrayHeaderRight\"><B>Μεταφ.</B></div></TD>
<td><div class=\"statsArrayHeaderRight\"><B>%</B></div></TD>
<TD><div class=\"statsArrayHeaderRight\"><B>Ασαφή</B></div></TD>
<td><div class=\"statsArrayHeaderRight\"><B>%</B></div></TD>
<TD><div class=\"statsArrayHeaderRight\"><B>Αμετάφ.</B></div></TD>
<td><div class=\"statsArrayHeaderRight\"><B>%</B></div></TD>
<TD><div class=\"statsArrayHeaderRight\"><B>Σύνολο</B></div></TD>
<TD WIDTH=\"100%\"><div class=\"statsArrayHeaderLeft\"><B>Γραφικό</B></div></TD>
</TR>" > "$cvsDIR"/table.stats

totAll=0
totTran=0
totUnTran=0
totFuzzy=0


  echo "Writing stats for: ${brName[$n]} in ${outName[$n]}"
  for k in $activeModules;do
    if [ "$k" != "." ] && [ -d "$k" ];then
      echo -n "  module $k...   "
      l10n-stats "$k" > "$cvsDIR"/"$k".stats
      hasErrors=`grep 'file has errors!!!' "$cvsDIR"/"$k".stats| wc -l`
      m=$(sed -n '/----*/,$p' "$cvsDIR"/"$k".stats | sed -n '2p')
      allMessages=`echo "$m" | awk '{print $1}'`
      tranMessages=`echo "$m" | awk '{print $2}'`
      fuzyMessages=`echo "$m" | awk '{print $3}'`
      untranMessages=`echo "$m" | awk '{print $4}'`

      [ -n "$allMessages" ] && totAll=$(($totAll+$allMessages)) || allMessages=0
      [ -n "$tranMessages" ] && totTran=$(($totTran+$tranMessages)) || tranMessages=0
      [ -n "$fuzyMessages" ] && totFuzzy=$(($totFuzzy+$fuzyMessages)) || fuzyMessages=0
      [ -n "$untranMessages" ] && totUnTran=$(($totUnTran+$untranMessages)) || untranMessages=0

      p=$(sed -n '/----*/,$p' "$cvsDIR"/"$k".stats | sed -n '3p')
      tranMessagesPer=`echo "$p" | awk '{print $2}'`
      if [ "$tranMessagesPer" = "0" ] && [ $tranMessages -ne 0 ];then tranMessagesPer=".01";fi
      fuzyMessagesPer=`echo "$p" | awk '{print $3}'`
      if [ "$fuzyMessagesPer" = "0" ] && [ $fuzyMessages -ne 0 ];then fuzyMessagesPer=".01";fi
      untranMessagesPer=`echo "$p" | awk '{print $4}'`
      if [ "$untranMessagesPer" = "0" ] && [ $untranMessages -ne 0 ];then untranMessagesPer=".01";fi
# if [ "$k" = "kdepim" ];then
#   set -x
# fi
      barUnTran=$(echo $untranMessagesPer| sed 's|\..*$||')
      if [ -z "$barUnTran" ] || [ "$barUnTran" = "0" ];then
        if [ $untranMessages -eq 0 ];then
          barUnTran=0
        else
          barUnTran=1
        fi
      else
        barUnTran=$(($barUnTran*2))
      fi






      barFuzzy=$(echo $fuzyMessagesPer| sed 's|\..*$||')
      if [ -z "$barFuzzy" ] || [ "$barFuzzy" = "0" ];then
        if [ $fuzyMessages -eq 0 ];then
          barFuzzy=0
        else
          barFuzzy=1
        fi
      else
        barFuzzy=$(($barFuzzy*2))
      fi

      if [ $barFuzzy -eq 1 ];then barFuzzy=2;fi
      if [ $barUnTran -eq 1 ];then barUnTran=2;fi



      barTran=$((200-$barFuzzy-$barUnTran))

      if [ $hasErrors -eq 0 ];then
echo -n "<TR BGCOLOR=\"#ffffff\">
<TD bgcolor=#D1DAF9><div class=\"statsArrayLeft\"><NOBR>$k</NOBR></div></TD>
<TD><div class=\"statsArrayRight\">$tranMessages</DIV></TD>
<TD BGCOLOR=\"#D1DAF9\"><div class=\"statsArrayRight\">$(fixPerCent $tranMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$fuzyMessages</DIV></TD>
<TD BGCOLOR=\"#D1DAF9\"><div class=\"statsArrayRight\">$(fixPerCent $fuzyMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$untranMessages</DIV></TD>
<TD BGCOLOR=\"#D1DAF9\"><div class=\"statsArrayRight\">$(fixPerCent $untranMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$allMessages</DIV></TD>
<TD bgcolor=#D1DAF9><div class=\"statsArrayLeft\"><NOBR>" >>"$cvsDIR"/table.stats


if [ $barTran -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar0.gif\" HEIGHT=\"15\" WIDTH=\"$barTran\">" >> "$cvsDIR"/table.stats
fi
if [ $barFuzzy -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar4.gif\" HEIGHT=\"15\" WIDTH=\"$barFuzzy\">" >> "$cvsDIR"/table.stats
fi
if [ $barUnTran -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar1.gif\" HEIGHT=\"15\" WIDTH=\"$barUnTran\">" >> "$cvsDIR"/table.stats
fi
echo "</NOBR></DIV></TD>
</TR>" >> "$cvsDIR"/table.stats

      else
echo "<TR BGCOLOR=\"#FFCCCC\">
<TD bgcolor=#FFCCCC><div class=\"statsArrayLeft\"><NOBR>$k</NOBR></div></TD>
<TD><div class=\"statsArrayRight\">$tranMessages</DIV></TD>
<TD BGCOLOR=\"#FFCCCC\"><div class=\"statsArrayRight\">$(fixPerCent $tranMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$fuzyMessages</DIV></TD>
<TD BGCOLOR=\"#FFCCCC\"><div class=\"statsArrayRight\">$(fixPerCent $fuzyMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$untranMessages</DIV></TD>
<TD BGCOLOR=\"#FFCCCC\"><div class=\"statsArrayRight\">$(fixPerCent $untranMessagesPer)</DIV></TD>
<TD><div class=\"statsArrayRight\">$allMessages</DIV></TD>
<TD bgcolor=#FFCCCC><div class=\"statsArrayLeft\">
<NOBR>Το πακέτο περιέχει σφάλματα!!!</NOBR></DIV></TD>
</TR>" >> "$cvsDIR"/table.stats

      fi
    
# if [ "$k" = "kdepim" ];then
#   set +x
# fi
      echo 'done'
    fi
  done

# set -x
totUnTranPer=$(echo "scale=4;100 * ($totUnTran / $totAll)"|bc | sed 's|\([^\.]*\...\).*|\1|')
if [ -z "$totUnTranPer" ] || [ "$totUnTranPer" = "0" ];then
  if [ $totUnTran -eq 0 ];then
    totUnTranPer=0
  else
    totUnTranPer='.01'
  fi
fi
totFuzzyPer=$(echo "scale=4;100 * ($totFuzzy / $totAll)"|bc | sed 's|\([^\.]*\...\).*|\1|')
if [ -z "$totFuzzyPer" ] || [ "$totFuzzyPer" = "0" ];then
  if [ $totFuzzy -eq 0 ];then
    totFuzzyPer=0
  else
    totFuzzyPer='.01'
  fi
fi

# totTranPer=$(echo "scale=4;100 * ($totTran / $totAll)"|bc | sed 's|\([^\.]*\...\).*|\1|')
#totTranPer=$((200-$totFuzzyPer-$totUnTranPer))



totTranPer=$(echo "scale=4; 100 - $totFuzzyPer - $totUnTranPer" | bc | sed 's|\([^\.]*\...\).*|\1|')



      barUnTran=$(echo $totFuzzyPer| sed 's|\..*$||')
      if [ -z "$barUnTran" ] || [ "$barUnTran" = "0" ];then
        if [ $totUnTran -eq 0 ];then
          barUnTran=0
        else
          barUnTran=1
        fi
      else
        barUnTran=$(($barUnTran*2))
      fi






      barFuzzy=$(echo $fuzyMessagesPer| sed 's|\..*$||')
      if [ -z "$barFuzzy" ] || [ "$barFuzzy" = "0" ];then
        if [ $totFuzzy -eq 0 ];then
          barFuzzy=0
        else
          barFuzzy=1
        fi
      else
        barFuzzy=$(($barFuzzy*2))
      fi

      if [ $barFuzzy -eq 1 ];then barFuzzy=2;fi
      if [ $barUnTran -eq 1 ];then barUnTran=2;fi



      barTran=$((200-$barFuzzy-$barUnTran))



echo "<TR BGCOLOR=\"#ffffff\">
<TD><div class=\"statsArrayFooterLeft\"><NOBR><B>Σύνολο</B></NOBR></DIV></TD>
<TD><div class=\"statsArrayFooterRight\"><B>$totTran</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRightPercent\"><B>$(fixBCPerCent $totTranPer)</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRight\"><B>$totFuzzy</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRightPercent\"><B>$(fixBCPerCent $totFuzzyPer)</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRight\"><B>$totUnTran</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRightPercent\"><B>$(fixBCPerCent $totUnTranPer)</B></DIV></DIV></TD>
<TD><div class=\"statsArrayFooterRight\"><B>$totAll</B></DIV></DIV></TD>
<TD bgcolor=\"#2B427F\"><div class=\"statsArrayFooterLeft\"><NOBR>" >> "$cvsDIR"/table.stats


# echo "<IMG SRC=\"images/bar0.gif\" HEIGHT=\"15\" WIDTH=\"200\"></NOBR></div>" >> "$cvsDIR"/table.stats

if [ $barTran -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar0.gif\" HEIGHT=\"15\" WIDTH=\"$barTran\">" >> "$cvsDIR"/table.stats
fi
if [ $barFuzzy -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar4.gif\" HEIGHT=\"15\" WIDTH=\"$barFuzzy\">" >> "$cvsDIR"/table.stats
fi
if [ $barUnTran -ne 0 ];then
  echo -n "<IMG SRC=\"images/bar1.gif\" HEIGHT=\"15\" WIDTH=\"$barUnTran\">" >> "$cvsDIR"/table.stats
fi
echo "</NOBR></DIV></TD>
</TR>" >> "$cvsDIR"/table.stats

echo "</TD>
</TR>
 </TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>" >> "$cvsDIR"/table.stats
# set +x
echo "Writing file: ${outName[$n]}"
# split files
sed '1,/<!-- Σημείο αρχής εγγραφής στατιστικών -->/d' "${outName[$n]}" > "${outName[$n]}".2
sed '/<!-- Σημείο αρχής εγγραφής στατιστικών -->/,$d' "${outName[$n]}" > "${outName[$n]}".1

# combine files
rm "${outName[$n]}"
cat "${outName[$n]}".1 "$cvsDIR"/table.stats "${outName[$n]}".2 > "${outName[$n]}"
rm "${outName[$n]}".1 "$cvsDIR"/table.stats "${outName[$n]}".2

done



echo "Writing Essentials page"

n=4
echo "<div align=\"center\">
<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" bgcolor=\"#8b898b\">
<tbody>
<tr>
<td>
<table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" width=\"100%\">
<tr bgcolor=\"#e0e0e0\">
<td><div class=\"statsArrayHeaderLeft\"><b>Όνομα</b></div></td>
<td><div class=\"statsArrayHeaderLeft\"><b>&nbsp;Απαίτηση&nbsp;</b></div></td>
<td><div class=\"statsArrayHeaderLeft\"><b>&nbsp;Κλάδος&nbsp;stable&nbsp;</b></div></td>
<td><div class=\"statsArrayHeaderLeft\"><b>&nbsp;Κλάδος&nbsp;trunk&nbsp;</b></div></td>
</tr>" > "$cvsDIR"/table.stats




count=$(echo "$inKdeItems"|wc -l)
branch[0]="$PO_STABLE"
branch[1]="$PO_TRUNK"
branchName[0]=stable
branchName[1]=trunk
tot[0]=0
tot[1]=0

for k in `seq 1 $count`;do
  l=`echo "$inKdeItems" | sed -n ""$k"p"`
  ch[$k]=$(echo "$l" | sed 's|,.*$||')
  lim[$k]=$(echo "$l" | sed 's|[^,]*,||')
  ((k++))
done

for k in `seq 1 $count`;do
  
  if [ -d "${ch[$k]}" ];then
    desc="Πακέτο&nbsp;<b>"${ch[$k]}"</b>"
  else
    desc="Αρχείο&nbsp;<b>"${ch[$k]}"</b>"
  fi



bCount=0
for bCount in 0 1;do
  cd "${branch[$bCount]}"
  echo -n "  item: ${ch[$k]}, branch: ${branchName[$bCount]}"
  if [ -d "${ch[$k]}" ];then
    per[$bCount]=$(l10n-stats "${ch[$k]}"|sed -n '/----*/,$p' | sed -n '3p' | awk '{print $2}')
    curPer[$bCount]=$(echo "a="${lim[$k]}";b="${per[$bCount]}";c=25; if (b<a) c=0;print c"|bc)
    tot[$bCount]=$((${tot[$bCount]}+${curPer[$bCount]}))
  else
    curPer[$bCount]=25

    theDir=$(find . -name "${ch[$k]}" | sed 's|^\./||' | sed 's|/.*||')
    if [ ! -e "$cvsDIR"/"$theDir"-"$bCount".stats ];then
      l10n-stats "$theDir" > "$cvsDIR"/"$theDir"-"$bCount".stats
    fi
# set -x
    tmpAllMsg=$(cat "$cvsDIR"/"$theDir"-"$bCount".stats | grep "${ch[$k]}" | awk '{print $1}')
    tmpTrMsg=$(cat "$cvsDIR"/"$theDir"-"$bCount".stats | grep "${ch[$k]}" | awk '{print $2}')
    per[$bCount]=$(echo "scale=4; 100 * ($tmpTrMsg / $tmpAllMsg)" | bc |sed 's|\([^\.]*\...\).*|\1|')
    curPer[$bCount]=$(echo "a="${lim[$k]}";b="${per[$bCount]}";c=25; if (b<a) c=0;print c"|bc)
    tot[$bCount]=$((${tot[$bCount]}+${curPer[$bCount]}))
  fi


  echo '  ->  done'

done


echo "<tr bgcolor=#e0e0e0>
<td bgcolor=#D1DAF9><div class=\"scrArrayBody\">
"$desc"</b></td>
<td bgcolor=\"#D1DAF9\"><div class=\"scrArray\">"${lim[$k]}"%</div></td>" >> "$cvsDIR"/table.stats
if [ ${curPer[0]} -eq 0 ];then
  echo "<td bgcolor=\"#D1DAF9\"><div class=\"scrArray\"><b><font color=\"Red\">"$(fixBCPerCent "${per[0]}")"%</font></b></div></div></td>" >> "$cvsDIR"/table.stats
else
  echo "<td bgcolor=\"#D1DAF9\"><div class=\"scrArray\">"$(fixBCPerCent "${per[0]}")"%</div></div></td>" >> "$cvsDIR"/table.stats
fi
if [ ${curPer[1]} -eq 0 ];then
  echo "<td bgcolor=\"#D1DAF9\"><div class=\"scrArray\"><b><font color=\"Red\">"$(fixBCPerCent "${per[1]}")"%</font></b></div></div></td>" >> "$cvsDIR"/table.stats
else
  echo "<td bgcolor=\"#D1DAF9\"><div class=\"scrArray\">"$(fixBCPerCent "${per[1]}")"%</div></div></td>" >> "$cvsDIR"/table.stats
fi
echo "</tr>" >> "$cvsDIR"/table.stats >> "$cvsDIR"/table.stats


done

echo "<tr bgcolor=\"#e0e0e0\">
<td><div class=\"scrArrayFooter\"><b>Σύνολο</b></div></td>
<td><div class=\"scrArrayFooter\">&nbsp;</div></td>
<td><div class=\"scrArrayFooterPercentGood\"><b>"$(fixBCPerCent "${tot[0]}")"%</b></div></td>
<td><div class=\"scrArrayFooterPercentGood\"><b>"$(fixBCPerCent "${tot[1]}")"%</b></div></td>
</tr>
</table>
</td></tr></table>
</div>" >> "$cvsDIR"/table.stats

echo "Writing file: ${outName[$n]}"
# split files
sed '1,/<!-- Σημείο αρχής εγγραφής στατιστικών -->/d' "${outName[$n]}" > "${outName[$n]}".2
sed '/<!-- Σημείο αρχής εγγραφής στατιστικών -->/,$d' "${outName[$n]}" > "${outName[$n]}".1

# combine files
rm "${outName[$n]}"
cat "${outName[$n]}".1 "$cvsDIR"/table.stats "${outName[$n]}".2 > "${outName[$n]}"
rm "${outName[$n]}".1 "$cvsDIR"/table.stats "${outName[$n]}".2


rm "$cvsDIR"/template.html
rm "$cvsDIR"/*.stats 2>/dev/null

# HTML TIDY
for n in "$cvsDIR"/*.html;do tidy -utf8 -language el -i -w 2000 -m "$n" 2>/dev/null;done
