Blame potomo

Packit fc043f
#!/bin/sh
Packit fc043f
# potomo - Convert a .po file to an utf-8 encoded .mo file.
Packit fc043f
# Copyright 2008 g10 Code GmbH
Packit fc043f
# Copyright 2010 Free Software Foundation, Inc.
Packit fc043f
#
Packit fc043f
# This file is free software; as a special exception the author gives
Packit fc043f
# unlimited permission to copy and/or distribute it, with or without
Packit fc043f
# modifications, as long as this notice is preserved.
Packit fc043f
#
Packit fc043f
# This file is distributed in the hope that it will be useful, but
Packit fc043f
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
Packit fc043f
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit fc043f
Packit fc043f
# This script is used to create the mo files for applications using
Packit fc043f
# the simple gettext implementation provided by libgpg-error.  That
Packit fc043f
# gettext can only cope with utf-8 encoded mo files; thus we make this
Packit fc043f
# sure while creating the mo.  A conversion is not done if the source
Packit fc043f
# file does not exist or if it is not newer than the mo file.
Packit fc043f
Packit fc043f
if [ "$1" = "--get-linguas" -a $# -eq 2 ]; then
Packit fc043f
   if [ ! -f "$2/LINGUAS" ]; then
Packit fc043f
       echo "potomo: directory '$2' has no LINGUAS file" >&2
Packit fc043f
       exit 1
Packit fc043f
   fi
Packit fc043f
   echo $(sed -e "/^#/d" -e "s/#.*//" "$2"/LINGUAS)
Packit fc043f
   exit 0
Packit fc043f
fi
Packit fc043f
Packit fc043f
if [ $# -ne 2 ]; then
Packit fc043f
  echo "usage: potomo INFILE.PO OUTFILE.MO" >&2
Packit fc043f
  echo "       potomo --get-linguas DIR"    >&2
Packit fc043f
  exit 1
Packit fc043f
fi
Packit fc043f
infile="$1"
Packit fc043f
outfile="$2"
Packit fc043f
Packit fc043f
if [ ! -f "$infile" ]; then
Packit fc043f
  echo "potomo: '$infile' not found - ignored" 2>&1
Packit fc043f
  exit 0
Packit fc043f
fi
Packit fc043f
Packit fc043f
if [ "$outfile" -nt "$infile" ]; then
Packit fc043f
  echo "potomo: '$outfile' is newer than source - keeping" 2>&1
Packit fc043f
  exit 0
Packit fc043f
fi
Packit fc043f
  
Packit fc043f
# Note that we could use the newer msgconv.  However this tool was not
Packit fc043f
# widely available back in 2008.
Packit fc043f
Packit fc043f
fromset=`sed -n '/^"Content-Type:/ s/.*charset=\([a-zA-Z0-9_-]*\).*/\1/p' \
Packit fc043f
         "$infile"`
Packit fc043f
Packit fc043f
case "$fromset" in 
Packit fc043f
    utf8|utf-8|UTF8|UTF-8) 
Packit fc043f
        echo "potomo: '$infile' keeping $fromset" >&2 
Packit fc043f
        msgfmt --output-file="$outfile" "$infile"
Packit fc043f
        ;;   
Packit fc043f
    *)
Packit fc043f
        echo "potomo: '$infile' converting from $fromset to utf-8" >&2
Packit fc043f
        iconv --silent --from-code=$fromset --to-code=utf-8 < "$infile" |\
Packit fc043f
            sed "/^\"Content-Type:/ s/charset=[a-zA-Z0-9_-]*/charset=utf-8/"|\
Packit fc043f
            msgfmt --output-file="$outfile" -
Packit fc043f
        ;;
Packit fc043f
esac