#!/bin/bash

# This is a word count utility for gettext po files.
# Copyright (C) 2002  Szabolcs Ban <shooby@gnome.hu>
# Copyright (C) 2003  Andras Timar <timar@gnome.hu>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

declare -i stotal=0;
declare -i sfuzzy=0;
declare -i strans=0;
declare -i suntrans=0;

echo -e "<html>\n<head>\n<title>PO file word count report</title>\n</head>\n<body>\n<h1>PO file word count report</h1>\n"
echo -e "<table border=1 cellpadding=2 cellspacing=1>\n<tr><th>Filename</th><th>Total</th><th>Trans</th><th>Fuzzy</th><th>Untrans</th></tr>"

for i in $*
 do
# removing all deprecated (#~) and location (#:) lines
# removing all empty lines
# and comments
# replacing \n with ß
# replacing escaped quotes with ¤
# replacing quotes with §
# joining multiple line msg*s
# changing back ß to \n
# removing lines begining with #
# removing lines containing empty string or marked as fuzzy
# changing back escaped quotes

  cat $i | grep -v "^#[~:]" | grep -v "^$" | \
        grep -v "# " | tr "\n" "ß" | \
	sed "s/\\\\\"/¤/g" | \
        tr "\\\"" "§" | \
        sed "s/§\s*ß§//g" | \
        sed "s/ßmsgstr §//g" | \
	sed "s/ß#, fuzzy[^ß]*ßmsgid §/ßmsgidð§§ §/g" | \
        tr "ß" "\n" | \
        grep -v "^#" | \
        grep -v "msgid §§" | \
        sed "s/¤/\\\\\"/g" | \
        sed "s/§§/ß/g" > /tmp/count.db

#count total
  total=`cat /tmp/count.db |  cut -f 2 -d "§" | \
        sed "s/[ßð]//g" | wc -w`
  stotal=$stotal+$total

# count untranslated
  untrans=`cat /tmp/count.db | grep ß | grep -v ð | cut -f 2 -d "§" | \
        sed "s/[ßð]//g" | wc -w`
  suntrans=$suntrans+$untrans

# count fuzzy
  fuzzy=`cat /tmp/count.db | grep ð | cut -f 2 -d "§" | \
	sed "s/[ßð]//g" | wc -w`
  sfuzzy=$sfuzzy+$fuzzy

#count translated
  trans=`cat /tmp/count.db | grep -v ß | cut -f 2 -d "§" | \
        sed "s/[ßð]//g" | wc -w`
  strans=$strans+$trans

echo -e "<tr><td>" $i "</td><td align=right>" $total "</td><td align=right>" $trans "</td><td align=right>" $fuzzy "</td><td align=right>" $untrans "</td></tr>"
  done

  echo -e "<tr><td><b> Total </b></td><td align=right><b>" $stotal "</b></td><td align=right><b>" $strans "</b></td><td align=right><b>" $sfuzzy "</b></td><td align=right><b>" $suntrans "</b></td></tr>"
  echo -e "</table>\n</body>\n</html>" 

  
 

# removing the count.db file
# rm /tmp/count.db
