#! /bin/sh

#  debconf-updatepo  - update PO files with Debconf templates files content
#  Copyright (C) 2002  Denis Barbier <barbier@debian.org>
#
#  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
#
#   This script is part of po-debconf

: ${PODEBCONF_LIB=/usr/share/intltool-debian}
INTLTOOL_EXTRACT=${PODEBCONF_LIB}/intltool-extract
export INTLTOOL_EXTRACT

: ${PODEBCONF_HEADER=/usr/share/po-debconf/pot-header}

#   Prevent automatic conversion to UTF-8 by Perl
unset LANGUAGE LANG LC_ALL LC_CTYPE

help=
verbose=
podir=

for option
do
        if [ -n "$prev" ]; then
                eval "$prev=\$option"
                prev=
                shift
                continue
        fi
        optarg=`expr "x$option" : 'x[^=]*=\(.*\)'`
        case $option in
            -h | --h | --help )
                help=1
                shift ;;
            -v | --v | --verbose )
                verbose=--verbose
                shift ;;
            --podir )
                prev=podir
                shift ;;
            --podir=* )
                podir=$optarg
                shift ;;
            -* )
                echo "$0: unknown option: $option ...exiting" 1>&2
                exit 1 ;;
            * ) break ;;
        esac
done

if [ "x$help" = x1 ]; then
        cat <<EOT 1>&2
Usage: debconf-updatepo [OPTIONS]
Options:
  -h,  --help          display this help message
  -v,  --verbose       enable verbose mode
       --podir=DIR     specify PO directory (searched by default in
                       debian/po, ./po and ../po)
EOT
        exit 0
fi

[ -n "$podir" ] || {
        for d in ../po po debian/po
        do
                [ -d $d ] && podir=$d
        done
}
[ -n "$podir" ] || {
        echo "No PO directory found, use the --podir flag to tell where it is... exiting" 1>&2
        exit 1
}
[ -d $podir ] || {
        echo "Directory $podir does not exist... exiting" 1>&2
        exit 1
}
[ -f $podir/POTFILES.in ] || {
        echo "File $podir/POTFILES.in does not exist... exiting" 1>&2
        exit 1
}

[ -n "$verbose" ] && echo "PO directory is $podir" 1>&2

cd $podir
domain=templates
[ -f debian.pot ] && domain=debian

$PODEBCONF_LIB/intltool-update $verbose --gettext-package=$domain --pot || exit 1
for lang in *.po
do
        [ "x$lang" = "x*.po" ] && break
        if sed -e '1,/^msgid/d' -e ':t /^msgstr/,${N;/\nmsgid/q;bt;};' $lang | grep charset=CHARSET >/dev/null 2>&1; then
                echo "$podir/$lang: Missing charset... file skipped" 1>&2
                continue
        fi
        lang=`echo $lang | sed 's/\.po$//'`
        $PODEBCONF_LIB/intltool-update --dist $verbose --gettext-package=$domain $lang || exit 1
done

#   Update POT header
if [ -f $PODEBCONF_HEADER ]; then
        if grep '^# */usr/share/doc/po-debconf/README-trans' $domain.pot >/dev/null 2>&1; then
                :
        else
                ( cat $PODEBCONF_HEADER; sed -e '/^#$/,$!d' $domain.pot ) \
                        > $domain.pot.new && mv $domain.pot.new $domain.pot
        fi
fi

rm -f messages.mo 2>/dev/null

