#!/bin/bash
# refdbjade - creates formatted output from a DocBook SGML source with
# RefDB bibliography data
# Markus Hoenicka <markus@mhoenicka.de> 1999-09-02

# OPTIONS: -d (stylesheet) -h (invoke help), -i (variable),
#          -j (jade command-line arguments), -t (output format)
# relies on these external programs: (open)jade, jadetex, pdfjadetex, dvips

### start user-customizable section

# the Jade/OpenJade command
myjade="/usr/bin/openjade"

# 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
jadeincludearg=""

# this allows to pass additional options to (open)jade
jadeargs=""

# this allows to set variable values on the (open)jade command line
jadevariablearg=""

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

# the path of the global configuration file
globalconfig="/etc/refdb/refdbjaderc"

# determine configuration files
if [ ! -r "$globalconfig" ] && [ -n "$REFDBLIB" ]; then
    globalconfig=$REFDBLIB/refdbjaderc
fi
userconfig=$HOME/.refdbjaderc

if [ -n "$globalconfig" ] && [ -r "$globalconfig" ]; then
    allconfigs=$globalconfig
fi

if [ -r "$userconfig" ]; then
    allconfigs=$allconfigs" "$userconfig
fi

# read the settings in the configure file(s)
for config in $allconfigs; do
    while read refdbvar refdbval; do
	if [ -n "$refdbvar" ]; then
	    if [ $refdbvar = jade_args ]; then
		jadeargs=$refdbval
	    fi
	    if [ $refdbvar = jade_includearg ]; then
		jadeincludearg=$jadeincludearg" -i "$refdbval
	    fi
	    if [ $refdbvar = jade_variable ]; then
		jadevariablearg=$jadevariablearg" -V "$refdbval
	    fi
	    if [ $refdbvar = outformat ]; then
		outformat=$refdbval
	    fi
	fi
    done < $config
done

# read the command line options
while getopts ":hI:j:p:s:t:v:" 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] [-j jade-args] [-p prefix] [-s stylesheet] [-t outformat] [-v variable[=value]] file1 [file2...]'
	 echo "Options: -h print this help and exit"
	 echo "         -I set name of INCLUDE section in OpenJade"
	 echo "         -j additional command-line options for 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."
	 echo "         -v set variable to #t or to value for OpenJade"
	 exit 0 ;;
    I  ) jadeincludearg=$jadeincludearg" -i "$OPTARG;;
    j  ) jadeargs=$OPTARG;;
    p  ) prefix=$OPTARG;;
    s  ) stylesheet=$OPTARG;;
    t  ) outformat=$OPTARG;;
    v  ) jadevariablearg=$jadevariablearg" -v "$OPTARG;;
    \? ) echo 'usage: refdbjade [-h] [-I name] [-j jade-args] [-p prefix] [-s stylesheet] [-t outformat] [-v variable[=value]] 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 $jadeargs $jadevariablearg $htmloutarg $jadeincludearg -t sgml -c $refdbcat -d $htmlsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    htmlr ) $myjade $jadeargs $jadevariablearg $htmloutarg $jadeincludearg -t sgml-raw -c $refdbcat -d $htmlsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    rtf  ) $myjade $jadeargs $jadevariablearg $rtfoutarg $jadeincludearg -t rtf -c $refdbcat -d $printsheet $sgmldecl $mypath
	   if [ $? -ne 0 ]; then
	     exit 1
	   fi;;
    dvi  ) $myjade $jadeargs $jadevariablearg $texoutarg $jadeincludearg -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 $jadeargs $jadevariablearg $texoutarg $jadeincludearg -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 $jadeargs $jadevariablearg $texoutarg $jadeincludearg -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 $jadeargs $jadevariablearg $texoutarg $jadeincludearg -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
