#! /bin/sh

#  debiandoc2pot  - create a POT file from a DebianDoc document
#  Copyright (C) 2001-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-debiandoc

: ${DEBIANDOCPOLIB=/usr/share/po-debiandoc}

help=
origfile=
fragment=
verbose=
fail=0

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

        esac
done

[ -n "$origfile" ] || fail=1
if [ "x$help" = x1 ] || [ "x$fail" = x1 ]; then
        cat <<EOT 1>&2
Usage: debiandoc2pot [-h] [-v] [-f LEVEL] orig.sgml
Options:
  -h,  --help           display this help message
  -v,  --verbose        enable verbose mode
  -f,  --fragment=LEVEL use when input is a fragment file
                        (chapter or appendix)
EOT
        exit $fail
fi

[ -f $origfile ] || {
    echo "File $origfile does not exist" 1>&2
    exit 1
}

[ -n "$fragment" ] && fragment="-f $fragment"

iflag=`grep '<!\[[[:blank:]]*%' $origfile | awk '{for(i=2;i<NF;i++) {printf "-i %s ", $i}}' | sed -e 's/%//g'`
$DEBIANDOCPOLIB/po-debiandoc-fix -N $fragment $origfile |\
        nsgmls -l $iflag 2>/dev/null |\
        $DEBIANDOCPOLIB/sgmlspl-l $DEBIANDOCPOLIB/flat.pl -i $origfile |\
        $DEBIANDOCPOLIB/flat2pot.pl $verbose |\
        sed -e 's/{po-ddf-amp}/\&/g' \
            -e 's/{po-ddf-lt}/</g' -e 's/{po-ddf-gt}/>/g' \
            -e 's/ name=\\"\\\\|\\\\|\\"//g' -e 's/\(> *\) \\n/\1\\n/g' \
            -e 's/ name="\\\\|\\\\|"//g'

