#!/bin/bash
# refdbjade - creates formatted output from a DocBook SGML source with
# RefDB bibliography data
# Markus Hoenicka <markus@mhoenicka.de> 990902
# $Id: refdbjade.in,v 1.6 2003/11/04 23:25:07 mhoenicka Exp $
# OPTIONS: -d (stylesheet) -h (invoke help), -i (variable), -t (output format)
# relies on these external programs: (open)jade, jadetex, pdfjadetex, dvips

### start user-customizable section

# the Jade/OpenJade command
myjade=""

# the SGML declaration
sgmldecl="/usr/share/refdb/declarations/docbook.dcl"

# the RefDB master catalog
refdbcat="/usr/share/refdb/refdb.cat"

### end user-customizable section

# some defaults
outformat="tex"

# this allows to include or ignore marked sections in the SGML file
includearg=""

# this is an optional prefix for output filenames
prefix=""
htmlprefixarg=""

# read the command line options
while getopts ":hi:p:s:t:" opt; do
  case $opt in
    h  ) echo "creates formatted output from a DocBook SGML source with RefDB bibliography data"
	 echo 'usage: refdbjade [-h] [-i name] [-p prefix] [-s stylesheet] [-t outformat] file1 [file2...]'
	 echo "Options: -h print this help and exit"
	 echo "         -i set variable name in OpenJade"
	 echo "         -p use prefix for output filenames"
	 echo "         -s select stylesheet"
	 echo "         -t select the output format. Possible values are html,"
	 echo "            rtf, dvi, pdf, ps, tex, tps, tpdf. Default is tex."
	 exit 0 ;;
    i  ) includearg=$includearg" -i "$OPTARG;;
    p  ) prefix=$OPTARG;;
    s  ) stylesheet=$OPTARG;;
    t  ) outformat=$OPTARG;;
    \? ) echo 'usage: refdbjade [-h] [-i name] [-p prefix] [-s stylesheet] [-t outformat] file1 [file2...]'
	 echo 'type refdbjade -h to invoke help'
	 exit 1;;
  esac
done

# correct the index so the filename argument is always $1
shift $(($OPTIND - 1))

# test for valid arguments
if [ ! $outformat = html ] && [ ! $outformat = htmlr ] && [ ! $outformat = rtf ] && [ ! $outformat = dvi ] && [ ! $outformat = pdf ] && [ ! $outformat = ps ] && [ ! $outformat = tex ] && [ ! $outformat = tps ] && [ ! $outformat = tpdf ]; then
  echo "specify one of 'html', 'htmlr', 'rtf', 'dvi', 'pdf', 'ps', 'tex', 'tps', 'tpdf' with the -t option"
  exit 1
fi

# assemble stylesheet invocation
htmlsheet=$stylesheet\#html
printsheet=$stylesheet\#print

# Unfortunately TeX does not indicate with an appropriate return
# value whether an additional run is necessary. To determine whether
# we need an additional tex run, we can look at file.aux and compare
# it to the previous run (we could also try to parse the output of tex
# on stdout, but that does not seem to be easier). So we create a
# backup of the existing file.aux (we create an empty file.aux if it
# does not exist) by appending the process number to the filename. We
# then run jadetex/pdfjadetex once which will create a new
# file.aux. We compare this version with the previous version and run
# jadetex until diff returns 0 indicating no differences. diff --brief
# prevents that the differences are listed on the screen. Somewhat
# kludgy, but it works. We might consider adding a counter to break
# infinite loops.

# loop over all filename arguments
for filename in $*; do
  # on Win32-cygwin, the native Win32 TeX tools want the DOS path, and
  # the cygwin TeX tools accept this too
  case $(uname) in
    CYGWIN*) mypath=$(cygpath -w $filename);;
    *) mypath=$filename;;
  esac

  # extract the basename from the argument
  basename=${filename%.*}

  # extract basename w/o full path
  sbasename=${basename##*/}

  if [ -n "$prefix" ]; then
    # put the prefix in place
    outbasename=$prefix$sbasename

    htmloutarg="-V %html-prefix%="$prefix
    rtfoutarg="-o "$outbasename.rtf
    texoutarg="-o "$outbasename.tex
    pdfoutarg="-o "$outbasename.pdf

    # get the name only - output files will be generated in PWD
    myfile=$prefix${filename##*/}
  else
    htmloutarg=""
    rtfoutarg="-o "$sbasename.rtf
    texoutarg="-o "$sbasename.tex
    pdfoutarg="-o "$sbasename.pdf

    # get the name only - output files will be generated in PWD
    myfile=${filename##*/}
  fi

  # the jade calls contain the -c option to make sure the stylesheets
  # are found even if the user was too lazy to set SGML_CATALOG_FILES
  case $outformat in
    html ) $myjade $jadevars $htmloutarg $includearg -t sgml -c $refdbcat -d $htmlsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    htmlr ) $myjade $jadevars $htmloutarg $includearg -t sgml-raw -c $refdbcat -d $htmlsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    rtf  ) $myjade $jadevars $rtfoutarg $includearg -t rtf -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    dvi  ) $myjade $jadevars $texoutarg $includearg -t tex -V tex-backend -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
	   if [ ! -e ${myfile%.*}.aux ]; then
             touch ${myfile%.*}.aux
	   fi
	   cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	   jadetex ${myfile%.*}.tex
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
           until diff --brief ${myfile%.*}.aux ${myfile%.*}.aux.$$; do
	     cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	     jadetex ${myfile%.*}.tex
	   done
	   rm ${myfile%.*}.aux.$$;;
    pdf  ) $myjade $jadevars $texoutarg $includearg -t tex -V tex-backend -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
           if [ ! -e ${myfile%.*}.aux ]; then
	     touch ${myfile%.*}.aux
	   fi
	   cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	   pdfjadetex ${myfile%.*}.tex
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
	   until diff --brief ${myfile%.*}.aux ${myfile%.*}.aux.$$; do
	     cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	     pdfjadetex ${myfile%.*}.tex
	   done
	   rm ${myfile%.*}.aux.$$;;
    ps   ) $myjade $jadevars $texoutarg $includearg -t tex -V tex-backend -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
           if [ ! -e ${myfile%.*}.aux ]; then
	     touch ${myfile%.*}.aux
	   fi
	   cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	   jadetex ${myfile%.*}.tex
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
           until diff --brief ${myfile%.*}.aux ${myfile%.*}.aux.$$; do
	     cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	     jadetex ${myfile%.*}.tex
	   done
	   rm ${myfile%.*}.aux.$$
	   dvips -o ${myfile%.*}.ps ${myfile%.*}.dvi
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    tex  ) $myjade $jadevars $texoutarg $includearg -t tex -V tex-backend -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    tps  ) if [ ! -e ${myfile%.*}.aux ]; then
	     touch ${myfile%.*}.aux
	   fi
	   cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	   jadetex ${myfile%.*}.tex
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
	   until diff --brief ${myfile%.*}.aux ${myfile%.*}.aux.$$; do
	     cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	     jadetex ${myfile%.*}.tex
	   done
	   rm ${myfile%.*}.aux.$$
	   dvips -o ${myfile%.*}.ps ${myfile%.*}.dvi
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    tpdf ) if [ ! -e ${myfile%.*}.aux ]; then
	     touch ${myfile%.*}.aux
	   fi
	   cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	   pdfjadetex ${myfile%.*}.tex
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi
           until diff --brief ${myfile%.*}.aux ${myfile%.*}.aux.$$; do
	     cp ${myfile%.*}.aux ${myfile%.*}.aux.$$
	     pdfjadetex ${myfile%.*}.tex
	   done
	   rm ${myfile%.*}.aux.$$;;
  esac
done

exit 0