head	1.5;
access;
symbols;
locks; strict;
comment	@# @;


1.5
date	2001.04.27.16.08.40;	author werner;	state Exp;
branches;
next	1.4;

1.4
date	2000.10.06.15.57.35;	author wkoch;	state Exp;
branches;
next	1.3;

1.3
date	2000.05.26.14.26.56;	author wkoch;	state Exp;
branches;
next	1.2;

1.2
date	2000.05.16.10.00.31;	author wkoch;	state Exp;
branches;
next	1.1;

1.1
date	2000.05.10.09.06.07;	author wkoch;	state Exp;
branches;
next	;


desc
@@


1.5
log
@Add Texinfo targets
@
text
@#!/bin/sh
# db2any - Docbook to html/ps/info rendering
#
#	Copyright (C) 2000, 2001 Free Software Foundation, Inc
#
# This 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 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
#
# Note: This requires a Posix shell
#
# $Id: db2any,v 1.4 2000/10/06 15:57:35 wkoch Exp $


pgm="db2any"
version="0.7.2"

usage () {
    echo 'usage: db2any [--help] [options] filename' >&2
    exit 1
}

show_banner () {
	cat <<EOF
$pgm $version
Copyright (C) 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

EOF
}

show_help () {
    show_banner
    cat <<EOF
usage: db2any [options] docbookfile

Options:
    --mode    select the mode (one of: ${all_modes})
    --help
    --nosplit
    --copyfiles
    --systemcheck
    --verbose
    --draft
    --manvolume n
    --usestyle	file
    --ignore-xref      [not yet implemented]

EOF
    exit 0
}

# a list of all possible stylesheet locations
stylesheet_dirs='
/usr/local/lib/dsssl/stylesheets/docbook
/usr/local/share/dsssl/stylesheets/docbook
/usr/local/lib/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/local/share/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/lib/dsssl/stylesheets/docbook
/usr/share/dsssl/stylesheets/docbook
/usr/lib/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/share/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/lib/sgml/stylesheets/nwalsh-modular
/usr/share/sgml/stylesheets/nwalsh-modular
'

all_modes="check tex html man texinfo"
input=
verbose=no
draft=no
nosplit=no
copyfiles=no
systemcheck_only=no
mode=none
manvolume="man"
usestyle=""
expected_args=1
ignore_xref=no
while test "`echo $1 | head -c1`" = "-"; do
    case $1 in
      --version)
	echo "$pgm $version"
	exit 0
	;;
      --help|-h|-help)
	show_help
	exit 0
	;;
      --nosplit)
	nosplit=yes
	;;
      --copyfiles)
	copyfiles=yes
	;;
      --systemcheck)
	systemcheck_only=yes
	expected_args=0
	;;
      --mode)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --mode" >&2
	   exit 1
	fi
	mode="$1"
	;;
      --manvolume)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --manvolume" >&2
	   exit 1
	fi
	manvolume="$1"
	;;
      --usestyle)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --usestyle" >&2
	   exit 1
	fi
	usestyle="$1"
	;;
      --verbose)
	verbose=yes
	;;
      --draft)
	draft=yes
	;;
      --ignore-xref)
        ignore_xref=yes
        ;;
      --)
	shift
	break
	;;
      *)
	echo "$pgm: invalid option $1" >&2
	exit 1
	;;
    esac
    shift
done

if [ $# = $expected_args ]; then
    if [ $# = 1 ]; then
       input="$1"
    fi
else
   usage
fi

# check the mode
case "$mode" in
    html|HTML)
	mode=html
	;;
    tex|TEX|TeX|dvi)
	mode=dvi
	;;
    man|MAN)
	mode=man
	;;
    texi|TEXI|texinfo|TEXINFO)
	mode=texinfo
	;;
    check)
	;;
    none)
	if [ $systemcheck_only != yes ]; then
	    echo "$pgm: no mode specified; use one of: ${all_modes}" >&2
	    exit 1
	fi
	;;
    *)
	echo "$pgm: invalid mode $mode" >&2
	echo "$pgm: valid modes are: ${all_modes}"  >&2
	exit 1
	;;
esac

if [ -n $usestyle ]; then
   if [ ! -f $usestyle ]; then
      echo "$pgm: cannot access local stylesteet" >&2
      exit 1
   fi
fi

#######################################
#   Options are all parsed here       #
#######################################

# check whether the given program is availbale in the path
check_prog () {
    tmp="$1"
    save_ifs="$IFS"; IFS=":"
    for i in $PATH; do
        test -z "$i" && i=.
        if test -f $i/$tmp; then
             IFS="$save_ifs"
             return 0
        fi
    done
    IFS="$save_ifs"
    return 1
}


# Figure out all what we need to know about the system we are
# running on and where the Docbook tools are installed.
# Tell about it when running in verbose mode.
do_systemcheck () {
    # look for Jade
    jade_version=`jade -v </dev/null 2>&1 | \
		   sed -n 's/.*:I:.*Jade version "\([0-9.]*\)"/\1/p'`
    if [ -z "$jade_version" ]; then
	echo "$pgm: error: jade not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: Jade version $jade_version found" >&2

    # look for JadeTeX
    if ! jadetex -v 2>/dev/null | grep -q '^TeX' ; then
	echo "$pgm: error: jadetex not found" >&2
	exit 1
    fi

    # look for the docbook-to-man script. Fixme: we should check that it
    # it is a recent version.
    if ! docbook-to-man 2>&1 | grep -q '^usage' ; then
	echo "$pgm: error: docbook-to-man not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: docbook-to-man found" >&2

    # look for the docbook-to-texi scripts.
    if ! check_prog docbook2texi ;  then
	echo "$pgm: error: docbook2texi not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: docbook2texi found" >&2
    if ! sgml2xml -v /dev/null 2>&1 | grep 'SP version' >/dev/null 2>&1 ; then
	echo "$pgm: error: sgml2xml not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: sgml2xml found" >&2

    # figure out where our stylesheets are
    tex_stylesheet=none
    for d in ${stylesheet_dirs}; do
	file=${d}/print/docbook.dsl
	if [ -f $file ]; then
	    tex_stylesheet=$file
	    break
	fi
    done
    [ $verbose = yes ] && echo "$pgm: TeX stylesheet: ${tex_stylesheet}" >&2
    html_stylesheet=none
    for d in ${stylesheet_dirs}; do
	file=${d}/html/docbook.dsl
	if [ -f $file ]; then
	    html_stylesheet=$file
	    break
	fi
    done
    [ $verbose = yes ] && echo "$pgm: HTML stylesheet: ${html_stylesheet}" >&2
    if [ $tex_stylesheet = none -o $html_stylesheet = none ]; then
	echo "$pgm: error: stylesheets not found" >&2
	exit 1
    fi

}


# Render the docbook as HTML
render_html () {
    output="`basename $input| sed 's/\.sgml$//'`.html"

    if [ -n "$usestyle" ]; then
	tmpstyle="`pwd`/`basename $usestyle`-html.tmp"
	if [ ! -f $tmpstyle -o $usestyle -nt $tmpstyle ]; then
	    sed "s%@@DOCBOOK_DSL@@%$html_stylesheet%" $usestyle > $tmpstyle
	fi
    else
	tmpstyle="$tex_stylesheet"
    fi


    # --nosplts creates just one HTML file
    if test $nosplit = yes; then
	echo "running jade on '$input' ..." >&2
	jade -d $tmpstyle -t sgml -i html -V nochunks $input > $output
	echo "$output created"
	return 0
    fi

    # Make sure that we have a html subdir
    if test -d html ; then
	:
    else
	if mkdir html; then
	    echo "'html' directory created" >&2
	else
	    echo "failed to create 'html' directory" >&2
	    exit 1
	fi
    fi

    outputdir="html/`basename $input| sed 's/\.sgml$//'`"

    if test -d $outputdir ; then
	:
    else
	if mkdir $outputdir; then
	    echo "'$outputdir' created" >&2
	else
	    echo "failed to create '$outputdir'" >&2
	    exit 1
	fi
    fi
    echo "creating html pages in '$outputdir' ..." >&2
    if test "$input" = "`basename $input`"; then
	inp="../../$input"
    else
	inp="$input"
    fi

    [ $verbose = yes ] && echo "running jade on '$inp' ..." >&2
    (cd $outputdir && jade -t sgml -i html -d $tmpstyle $inp )
    [ $verbose = yes ] && echo "html version in '$outputdir' created" >&2

    # break out all filerefs and copy them to the outputdirectory
    # fixme: handling of path components is wrong
    if test $copyfiles = yes; then
	echo "looking for filerefs ..." >&2
	for file in `nsgmls -i html $input \
			| awk '/^AFILEREF[ \t]+CDATA/ {print $3}'`; do
	    d=$outputdir/`basename $file`
	    if cat $file > $outputdir/`basename $file` ; then
		echo "  $file -> $d" >&2
	    fi
	done
    fi

    mainfile=`ls $outputdir/${doctype}* | head -1`

    # create a html index file for it, so that we can more easy
    # find the rendred pages
    cat > $output <<EOF
<html><title>$output</title>
<body>

<a href="$mainfile">$mainfile</a>

</body>
</html>
EOF

    [ $verbose = yes ] && echo "$output created with link to '$mainfile'" >&2
}


# This function expects the source file in $texfile and
# the name of the logfle in $logfile
run_jadetex () {
    [ -f $logfile ] && rm $logfile
    jadetex $texfile
    if ! tail $logfile | grep -q '^Output written on'; then
	echo "JadeTeX failed" >&2
	exit 1
    fi
}

# Render the docbook to DVI
render_dvi () {
    output="`basename $input| sed 's/\.sgml$//'`.dvi"
    texfile="`basename $input| sed 's/\.sgml$//'`.tex"
    logfile="`basename $input| sed 's/\.sgml$//'`.log"
    auxfile="`basename $input| sed 's/\.sgml$//'`.aux"

    if [ -n "$usestyle" ]; then
	tmpstyle="`basename $usestyle`-tex.tmp"
	if [ ! -f $tmpstyle -o $usestyle -nt $tmpstyle ]; then
	    sed "s%@@DOCBOOK_DSL@@%$tex_stylesheet%" $usestyle > $tmpstyle
	fi
    else
	tmpstyle="$tex_stylesheet"
    fi
    [ $verbose = yes ] && echo "running jade on '$input' ..." >&2
    jade -t tex -i tex -d $tmpstyle $input
    if ! tail $texfile | grep -q '\\endFOT{}'; then
	echo "Jade failed" >&2
	exit 1
    fi

    # Better delete the aux file first
    [ -f $auxfile ] && rm $auxfile
    # The first run won't get the references right, so we have to
    # run it 2 or 3 times.  JadeTex doesn't indicate whether a third
    # run is required, so we do it always.
    run_jadetex
    if [ $draft = no ]; then
	if tail -100 $logfile \
	   | grep -q '^LaTeX Warning: There were undefined references'; then
	   echo 'running JadeTeX a second and third 2time' >&2
	   run_jadetex
	   run_jadetex
	fi
    fi

    [ $verbose = yes ] && echo "$output created as '$output'" >&2
}

# Render the docbook to troff
render_man () {
    output="`basename $input| sed 's/\.sgml$//'`.$manvolume"

    [ $verbose = yes ] && echo "running docbook-to-man on '$input' ..." >&2
    docbook-to-man $input > $output
    [ $verbose = yes ] && echo "man page '$output' created" >&2
}

# Render the docbook to texinfo
render_texinfo () {
    output="`basename $input| sed 's/\.sgml$/.texi/'`"
    tmpxml="`basename $input| sed 's/\.sgml$/.xml/'`"

    [ $verbose = yes ] && echo "running sgml2xml on '$input' ..." >&2
    sgml2xml -x lower $input > $tmpxml
    [ $verbose = yes ] && echo "running docbook2texi on '$tmpxml' ..." >&2
    docbook2texi $tmpxml | sed 's,--,---,' >$output
    rm $tmpxml 
    [ $verbose = yes ] && echo "man page '$output' created" >&2

}

#######################################
#	 main function		      #
#######################################

do_systemcheck
[ $systemcheck_only = yes ] && exit 0

if [ ! -f "$input" ]; then
    input="$input.sgml"
    if [ ! -f "$input" ]; then
	echo "$pgm: '$input': no such file" >&2
	exit 1
    fi
fi

# grep the document type
doctype=`grep -i '\<doctype' $input|awk 'NR==1 {print $2}'| tr '[A-Z]' '[a-z]'`
if test -z "$doctype"; then
    echo "$pgm: error: no DOCTYPE declaration found" >&2
    exit 1
fi
[ $verbose = yes ] &&  echo "$input: DOCTYPE is '$doctype'" >&2

case $mode in
    check)
	nsgmls -vs $input
	exit $?
	;;
    html)
	render_html
	;;
    dvi)
	render_dvi
	;;
    man)
	render_man
	;;
    texinfo)
	render_texinfo
	;;
esac


exit 0@


1.4
log
@Minor changes to the German version to make it online available.
@
text
@d4 1
a4 1
#	Copyright (C) 2000 Free Software Foundation
d22 1
a22 1
# $Id: db2any,v 1.3 2000/05/26 14:26:56 wkoch Exp $
d26 1
a26 1
version="0.7.1"
d36 1
a36 1
Copyright (C) 2000 Free Software Foundation, Inc.
a45 1

d59 1
d78 1
a78 1
all_modes="check tex html man"
a163 1
all_modes="dvi html man"
d174 3
d203 16
d221 1
a221 1
# Tell about it when runnng in verbose mode.
d244 13
d433 14
d484 3
d490 1
a490 2
exit 0

@


1.3
log
@Added man page support
@
text
@d2 1
a2 1
# db2any - Docbook to html/ps/info rendering (wk 2000-05-10)
d22 1
a22 1
# $Id$
d26 1
a26 1
version="0.7"
a39 1
stylesteet: $stylesheet
d57 3
a59 1
    --manvolume
d74 2
d87 1
d89 1
d126 8
d140 3
d190 7
d250 1
d258 10
d271 1
a271 1
	jade -d $html_stylesheet -t sgml -i html -V nochunks $input > $output
d276 1
a276 1
    # Make syre that we have a html subdir
d306 1
d308 1
a308 1
    (cd $outputdir && jade -t sgml -i html -d $html_stylesheet $inp )
d360 8
d369 1
a369 1
    jade -t tex -i tex -d $tex_stylesheet $input
@


1.2
log
@Introduced the catch-all script db2any ;-)
@
text
@d21 3
d26 1
a26 1
version="0.6"
d58 1
d83 1
d113 8
d272 1
a272 1
    echo "running jade on '$inp' ..." >&2
d274 1
a274 1
    echo "html version in '$outputdir' created" >&2
d303 1
a303 1
    echo "$output created with link to '$mainfile'" >&2
d325 1
a325 1
    echo "running jade on '$input' ..." >&2
d347 10
a356 1
    echo "$output created as '$output'" >&2
d392 3
@


1.1
log
@New script
@
text
@d54 1
d59 1
a59 1
# a list of all possible stylesheetlocations
d74 1
d111 3
d135 1
a135 1
all_modes="tex html man"
d140 2
a141 2
    tex|TEX|TeX)
	mode=tex
d217 119
d352 1
a352 1
# grep the document` type
d360 12
a371 72
if [ $mode = check ]; then
    nsgmls -vs $input
    exit $?
fi

output="`basename $input| sed 's/\.sgml$//'`.html"


if test $nosplit = yes; then
    echo "running jade on '$input' ..." >&2
    jade -d $html_stylesheet -t sgml -i html -V nochunks $input > $output
    echo "$output created"
    exit 0
fi

if test -d html ; then
    :
else
    if mkdir html; then
	echo "'html' directory created" >&2
    else
	echo "failed to create 'html' directory" >&2
	exit 1
    fi
fi

outputdir="html/`basename $input| sed 's/\.sgml$//'`"

if test -d $outputdir ; then
    :
else
    if mkdir $outputdir; then
	echo "'$outputdir' created" >&2
    else
	echo "failed to create '$outputdir'" >&2
	exit 1
    fi
fi
echo "creating html pages in '$outputdir' ..." >&2
if test "$input" = "`basename $input`"; then
    inp="../../$input"
else
    inp="$input"
fi
echo "running jade on '$inp' ..." >&2
(cd $outputdir && jade -t sgml -i html -d $html_stylesheet $inp )
echo "html version in '$outputdir' created" >&2

# break out all filerefs and copy them to the outputdirectory
# fixme: handling of path components is wrong
if test $copyfiles = yes; then
    echo "looking for filerefs ..." >&2
    for file in `nsgmls -i html $input \
		    | awk '/^AFILEREF[ \t]+CDATA/ {print $3}'`; do
	d=$outputdir/`basename $file`
	if cat $file > $outputdir/`basename $file` ; then
	    echo "  $file -> $d" >&2
	fi
    done
fi

mainfile=`ls $outputdir/${doctype}* | head -1`

cat > $output <<EOF
<html><title>$output</title>
<body>

<a href="$mainfile">$mainfile</a>

</body>
</html>
EOF
a372 1
echo "$output created with link to '$mainfile'" >&2
@

