#!/bin/bash

#  debiandoc2dbxml   - translate debiandoc sgml file into
#  docbook-xml file.
#  Copyright (C) 2002 2003  Philippe Batailler <philippe.batailler@free.fr>
#
#  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
#

progname=${0##*/}

version=0.96

BIN="/usr/bin"
LIB="/usr/share/sgml/debiandoc2dbxml"

# package xsltproc
#
xsltcmd=$BIN/xsltproc

## ----------------------------------------------------------------------
## print error message
function usage_error
{
    echo >&2 "${progname}: ${@}";
    exit 2;
}

##----------------------------------------------------------------
## get command line options
#
opt_a=false
opt_b=false
opt_h=false
opt_k=false
opt_s=false
opt_S=false
type=book
locale=
infile=

while getopts ":abhvtkl:sS" opt
do
    case $opt in
      a ) type="article" ;;
      b ) type="book" ;;
      h ) opt_h=true ;;
      v ) { echo "$progname: version "$version; exit 0;} ;;
      t ) set -x ;;
      k ) opt_k=true ;;
      l ) locale="${OPTARG}" ;;
      s ) opt_s=true ;;
      S ) opt_S=true ;;
      \? ) usage_error "unknown option \`${OPTARG}'"
    esac
done
shift $((${OPTIND} - 1))

## ----------------------------------------------------------------------
## check remaining command line options
[ ${#} = 1 ] || usage_error "need exactly one input filename"

## ----------------------------------------------------------------------
## get input file name
infile="${1}"

## ----------------------------------------------------------------------
# set default locale
if [ -z "$locale" ];
then
locale=en
fi

##------------------------------------------------------------
## Display help message
#
if $opt_h; then
    cat <<END
$progname version $version

Usage: $progname [options] filename
Options: -h     print this help message
         -k     keep intermediate files
         -s     subset of the dtd
         -t     set trace
         -v     version number
         -a     article format
         -b     book format
         -S     splitting the result in many files 
         -l     locale (fr, en, es...)
END
    exit 0
fi

##-----------------------------------------------------------------
## temporary filenames 
#
basename=${infile%.sgml}
headerfile=$basename.head
outfile=$basename.xml
outfile_int=main.$locale.xml

##-----------------------------------------------------------------
## Declarations
#

xml_declaration='<?xml version="1.0" encoding="iso-8859-1"?>'

XMLdtd_declaration='<!DOCTYPE '$type' PUBLIC "-//Norman Walsh//DTD DocBk XML V4.2//EN" "/usr/share/sgml/docbook/dtd/xml/4.2/docbookx.dtd"'

dtd_declaration='<!DOCTYPE debiandoc-xml PUBLIC "-//DebianDoc//DTD DebianDocXml//EN" "/usr/share/sgml/debiandoc2dbxml/debiandoc-xml.dtd"'


##-----------------------------------------------------------------
#
$opt_k || trap "rm -f $basename.head sgml.error xml.error $source_dir/main1.xml>/dev/null 2>&1; exit 1" 1 2 15

##-----------------------------------------------------------------
## Where are we ?
#
source_dir=${1%/*}
dir=$(pwd)
if [ -f $source_dir ]
    then 
       source_dir=$dir
fi

##-----------------------------------------------------------------
## get the prolog of the document, change it, append xml declaration and
## create a new prolog
## Usually, debiandoc files start with a line like this : 
## <!DOCTYPE debiandoc SYSTEM>. We must add a system identifier.


echo $xml_declaration > $headerfile
echo $xml_declaration > $outfile

if $opt_s         ## Is there any subset?

then
        echo $dtd_declaration >> $headerfile
        sed -n '/^<!DOCTYPE /,/<debiandoc>$/p'  $infile | \
        sed -e 's/<!DOCTYPE .* \[/\[/' -e '$d' >> $headerfile
        echo $XMLdtd_declaration >> $outfile        
        sed -n '/^<!DOCTYPE /,/<debiandoc>$/p'  $infile | \
        sed -e 's/<!DOCTYPE .* \[/\[/' -e '$d' >> $outfile
        cat $outfile > $outfile_int

else
        echo $dtd_declaration >> $headerfile
        sed -n 's/<!DOCTYPE .*>/>/p' $infile >> $headerfile
        echo $XMLdtd_declaration'>' >> $outfile
        cat $outfile > $outfile_int

fi


##----------------------------------------------------------------
## generating the file that follows the new dtd

	echo "====== sgml2xml ======="

$BIN/sgml2xml -b iso-8859-1 -x empty -x lower -x comment -x no-nl-in-tag -x cdata $infile 2>$source_dir/sgml.error | sed -e 1d -e "s/debiandoc/debiandoc-xml/g" >> $headerfile


##----------------------------------------------------------------
## generating the xml file

	echo "------ xslt --------"
   
   xslt_format=$LIB/book.xml

     $xsltcmd --stringparam style $type $xslt_format $headerfile >> $outfile 2>$source_dir/xml.error


##--------------------------------------------------------------
## Creating xml files from the bookinfo (titletoc) and from each chapter
## and appendix. ( if the -S or --split option is chosen)

   if $opt_S
       then
       mkdir $source_dir/$locale
       split=$LIB/split.xml
       $xsltcmd --stringparam locale $locale --stringparam dir $source_dir/$locale -o $source_dir/main1.xml $split $outfile
       cat $source_dir/main1.xml >> $outfile_int
       rm -f $outfile
       else
       rm -f $outfile_int
   fi

##--------------------------------------------------------------
## If xml.error is not empty, there is a problem !!
## we shoud error out.
   
   if [ -s $source_dir/xml.error ];
       then
       echo "Some errors occured! Please read '$source_dir/xml.error' 
for more information"
       else
       rm -f $source_dir/xml.error
   fi


##--------------------------------------------------------------
## removing intermediate files

$opt_k || rm -f $basename.head sgml.error $source_dir/main1.xml
