#! /bin/sh

LC_ALL=C
export LC_ALL


xgettext --from-code ISO-8859-1 -c -iC --foreign-user -kTranslate -k_ -kN_ -o- $(find . \( -name "tests" -prune -o -name "resource" -prune -o -name "specs" -prune -o -name "testgames" -prune -o -name "*.cc" -o -name "*.h" \) -type f) resource/core.q |
    iconv -f utf-8 -t latin1 >po/messages.po

# So, what do we do with Unicode? The sourcecode contains some (but
# few) non-ASCII characters. I don't have UTF-8 capable editors on
# all of my systems, and they wouldn't be able to display our fancy
# extra glyphs anyway. So, this translates the UTF-8 sequences into
# pseudo-Unicode escapes (\u). Since nobody but us is able to handle
# such sequences in message strings, they are double-escaped to
# actually appear as is in the text.
perl -mbytes -i -pe 'm/\\u/ and warn("file contains unicode escape!");
             s/([\xC2-\xDF])([\x80-\xBF])/sprintf("\\\\u%04X", 64*(ord($1) & 0x1F) + (ord($2) & 0x3F))/ge;
             s/([\xE0-\xEF])([\x80-\xBF])([\x80-\xBF])/sprintf("\\\\u%04X", 64*64*(ord($1) & 0x0F) + (ord($2) & 0x3F) * 64 + (ord($3) & 0x3F))/ge;
             m/[\x80-\xFF]/ and warn("unparsed UTF-8 in file");' po/messages.po
