+
+error () {
+ warning $*
+ exit 1
+}
+
+
+# $1 is a string like
+# '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
+# Any one of these substrings may not appear if the associated number is 0.
+#
+# $2 is the word following the number to be extracted,
+# ie, 'translated', 'fuzzy', or 'untranslated'.
+#
+# extract_number fills var $number with this number, or sets it to zero if the
+# word is not found in the string.
+extract_number () {
+ test $# -eq 2 || error 'extract_number expects 2 args'
+
+ number=0
+ echo $1 | grep $2 >/dev/null || return
+ # It /is/ safe to use 'Z' as a delimiter here.
+ number=`echo $1 | sed "s/\([0-9]*\)[ ]*$2/Z\1Z/" | cut -d 'Z' -f 2`
+}
+
+
+# $template is used by run_msgfmt, below, to fill $output. The function extracts
+# the appropriate values from the data.
+template="array ( 'langcode' => 'LC',
+\"msg_tr\" => TR, \"msg_fu\" => FU, \"msg_nt\" => NT,
+\"translator\" => \"AUTHOR\", \"email\" => \"EMAIL\",
+\"date\" => \"DATE\" )"
+readonly template
+
+
+# $1 is the name of the po file.
+#
+# The function runs msgfmt on it and fills var $output.
+# All other variables created in the function are unset on exit.
+run_msgfmt () {
+ test $# -eq 1 || error 'run_msgfmt expects 1 arg'
+
+ output=
+ test -f $1 || {
+ warning "File $1 does not exist"
+ return
+ }
+
+ origdir=`pwd`
+ dir=`dirname $1`
+ pofile=`basename $1`
+ gmofile=`echo $pofile | sed 's/po$/gmo/'`
+ test $pofile != '' -a $pofile != $gmofile || {
+ warning "File $1 is not a po file"
+ unset origdir dir pofile gmofile
+ return
+ }
+
+ cd $dir
+ unset dir
+
+ langcode=`echo $pofile | sed 's/\.po$//'`
+
+ # Searching for a string of the form
+ # '"PO-Revision-Date: 2003-01-18 03:00+0100\n"'
+ date=`grep 'Revision-Date' $pofile | sed 's/ */ /g' | cut -d ' ' -f 2`
+
+ # Searching for a string of the form
+ # '"Last-Translator: Michael Schmitt <Michael.Schmitt@teststep.org>\n"'
+ translator=
+ email=
+ input=`grep "Last-Translator" $pofile` && {
+ input=`echo $input | sed 's/ */ /g' | cut -d ' ' -f 2-`
+
+ translator=`echo $input | cut -d '<' -f 1 | sed 's/ *$//'`
+ email=`echo $input | cut -d '<' -f 2 | cut -d '>' -f 1`
+ }
+ unset input
+
+ # Run msgfmt on the pofile, filling $message with the raw info.
+ message=`$msgfmt --statistics -o $gmofile $pofile 2>&1 | grep "^[1-9]"` || {
+ warning "Unable to run msgfmt successfully on file $1"
+ cg $origdir
+ unset origdir pofile gmofile
+ return
+ }
+ unset pofile gmofile
+
+ extract_number "$message" 'translated'
+ translated=$number
+
+ extract_number "$message" 'fuzzy'
+ fuzzy=$number
+
+ extract_number "$message" 'untranslated'
+ untranslated=$number
+ unset message number
+
+ output=`echo "$template" | sed "s/LC/$langcode/; \
+ s/TR/$translated/; s/FU/$fuzzy/; s/NT/$untranslated/; \
+ s/AUTHOR/$translator/; s/EMAIL/$email/; s/DATE/$date/"`
+
+ unset langcode date translator email untranslated fuzzy translated
+ cd $origdir
+ unset origdir
+}
+
+
+# The head of the generated php file.
+dump_head () {