#! /bin/sh

#  debiandoc-gettextize   - create a PO file from 2 DebianDoc documents
#  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=
verbose=
nomarker=
fragment=
directory=
keep=
origfile=
transfile=
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 ;;
                -r | --r | --no-marked-section-delimiters ) nomarker=-r ;;
                -f | --f | --fragment ) prev=fragment ;;
                -f=* | --f=* | --fragment=* )
                     fragment=$optarg ;;
                -d | --d | --directory ) directory=1 ;;
                -k | --k | --keep ) keep=1 ;;
                -* ) echo "$0: unknown option: $option ...exiting" 1>&2
                     exit 1
                     ;;
                 * ) origfile=$option
                     prev=transfile
                     ;;

        esac
done

[ -n "$origfile" ] || fail=1
[ -n "$transfile" ] || fail=1
if [ "x$help" = x1 ] || [ "x$fail" = x1 ]; then
        cat <<EOT 1>&2
Usage: debiandoc-gettextize [-h] [-v] [-f LEVEL] [-k] [-r] orig.sgml trans.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)
  -k,  --keep           do not remove intermediate files
  -r,  --no-marked-section-delimiters
                        remove marked-section start and end strings
EOT
        exit $fail
fi

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

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

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

origflat=$origfile.flat.$$
transflat=$transfile.flat.$$
rm -f $origflat $transflat 2>/dev/null

[ -n "$keep" ] || trap "rm -f $origflat $transflat 2>/dev/null" 0 1 2 15

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 $nomarker $origfile |\
        nsgmls -l $iflag 2>/dev/null |\
        $DEBIANDOCPOLIB/sgmlspl-l $DEBIANDOCPOLIB/flat.pl -i $origfile |\
        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' > $origflat

$DEBIANDOCPOLIB/po-debiandoc-fix -N $fragment $nomarker $transfile |\
        nsgmls -l $iflag 2>/dev/null |\
        $DEBIANDOCPOLIB/sgmlspl-l $DEBIANDOCPOLIB/flat.pl -i $transfile |\
        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' > $transflat

$DEBIANDOCPOLIB/flat2po.pl $verbose -O $origfile -T $transfile $origflat $transflat

