#! /bin/sh

#  debconf2pot  - create a POT file from a Debconf templates file
#  Copyright (C) 2002-2005  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}

#  See intltool-extract
INTLTOOL_DEBIAN_TYPE=po-debconf
export INTLTOOL_DEBIAN_TYPE

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

help=
quiet=--quiet
podir=po

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 )
                quiet=
                shift ;;
            --podir )
                prev=podir
                shift ;;
            --podir=* )
                podir=$optarg
                shift ;;
            -* )
                echo "$0: unknown option: $option ...exiting" 1>&2
                exit 1 ;;
            * ) break ;;
        esac
done
[ $# -eq 0 ] && set templates

fail=0
[ -n "$podir" ] || fail=1
if [ "x$help" = x1 ] || [ "x$fail" = x1 ]; then
        cat <<EOT 1>&2
Usage: debconf2pot [OPTIONS] [template [template...]]
Options:
  -h,  --help          display this help message
  -v,  --verbose       enable verbose mode
       --podir=DIR     specify PO output directory (default: po)
EOT
        exit $fail
fi

[ -d $podir ] || mkdir $podir

hfiles=
for origfile
do
        [ -f $origfile ] || {
                echo "Error: file $origfile does not exist... exiting" 1>&2
                exit 1
        }

        [ -z "$quiet" ] && echo "Processing $origfile..." 1>&2
        $PODEBCONF_LIB/intltool-extract $quiet --update --type=gettext/rfc822deb $origfile
        hfiles=$hfiles" "$origfile.h
done

xgettext --add-comments --output=$podir/templates.pot --keyword=N_ $hfiles

rm -f $hfiles
[ -z "$quiet" ] && echo "Removed $hfiles" 1>&2

exit 0
