#!/bin/sh 
# 
# bib2dvi - generates a .dvi file from a .bib file
#
# Version 0.13 (October 22, 1992)
#
# Copyright (c) L.L. Frederiks, 1992
# 
# Usage: bib2dvi [<file.bib>] [-d|-e] [-s <style>] [-h] [-v]
#
# Purpose: Converts a BibTeX .bib file directly into a by LaTeX
#          formatted .dvi file.
#
# Output: file.dvi 
#
# History: 0.10  June 22, 1992     original version 0.11.
#          0.12  October 21, 1992  options -d,-e and -s.
#          0.13  October 22, 1992  improved interpretation of option parameters,
#                                  they can used in any order. Extra -h option for
#                                  providing help information and -v option which
#                                  disables the suppression of latex and bibtex
#                                  messages.
#
# Please report bugs to: L.L. Frederiks
#                        Runmolen 88A
#                        1823 GM Alkmaar
#                        The Netherlands
#                        e-mail: loek@nt.eltn.utwente.nl
#
         
USAGE='Usage: '`basename $0`' [<file.bib>] [-d|-e] [-s <style>] [-h] [-v]'
BIB2DVIDIR=//dino/users/cad/loek/postscript/bib2ps
TEXINPUTDIR=//dino/arbortext/inputs
STYLE=english
BSTYLE=plain
NAMES="abbrv alpha plain unsrt"
VERBOSE=false

if [ "$1" =  "" ]
then
    echo bib2dvi 0.13 \(October 22, 1992\)
    echo Copyright \(c\) L.L. Frederiks, 1992
	echo "$USAGE" 1>&2
    echo "       "where options are:
    echo "       "-d  Dutch    
    echo "       "-e  English \(default\)
    echo "       "-s  Bibtex style: abbrv,alpha,unsrt,plain \(default\)
    echo "       "-h  help
    echo "       "-v  verbose
	exit 1
fi

if [ -f $1 ]
then
	BIBALL=$1
else
	echo "Argument not an existing file." 1>&2
	echo "$USAGE" 1>&2
    echo "       "where options are:
    echo "       "-d  Dutch
    echo "       "-e  English \(default\)
    echo "       "-s  Bibtex style: abbrv,alpha,unsrt,plain \(default\)
    echo "       "-h  help
    echo "       "-v  verbose
	exit 1
fi

for arg in $*
do
        case $arg in
                -h) echo bib2dvi 0.13 \(October 22, 1992\)
                    echo Copyright \(c\) L.L. Frederiks, 1992
                 	echo "$USAGE" 1>&2
                    echo "       "where options are:
                    echo "       "-d  Dutch    
                    echo "       "-e  English \(default\)
                    echo "       "-s  Bibtex style: abbrv,alpha,unsrt,plain \(default\)
                    echo "       "-h  help
                    echo "       "-v  verbose
                   	exit 1;;
                -d) STYLE=dutch;;
                -e) STYLE=english;;
                -v) VERBOSE=true;;
                -s) for NAME in $* ; do
                      for SUBNAME in $NAMES ; do
                          if [ $SUBNAME = $NAME ]
                          then
                              BSTYLE=$SUBNAME
                          fi
                      done
                    done
        esac
done

BIBLIO=`basename $BIBALL .bib`

echo bib2dvi, version 0.12 \(October 22, 1992\)....

sed -e "s/xxx/${BIBALL}/" -e "s/yyy/${USER}/" -e "s/ddd/${STYLE}/" > $BIBLIO.tmp1 << PART1
\documentstyle[11pt,twoside,${BIB2DVIDIR}/bib2dvi,${BIB2DVIDIR}/ddd]{report}
\pagestyle{plain}

\begin{document}
\noindent

\title{xxx}
PART1

sed -e "s/ggg/${USER}/" > $BIBLIO.awk1 << PART2
{
   creator="ggg";\\
   FS = ":,";\\
   str= "\author{";\\
   acco3 = "}";\\
   if (\$1 == creator)
     {print \$5};\\
}
PART2

cat /etc/passwd > $BIBLIO.pass

awk -f $BIBLIO.awk1 $BIBLIO.pass > $BIBLIO.tmp0

awk -f $BIB2DVIDIR/bib2dvi2.awk $BIBLIO.tmp0 > $BIBLIO.tmp2
                                              
sed -e "s/zzz/${BIBLIO}/" -e "s/uuu/${BSTYLE}/" > $BIBLIO.tmp3 << PART2

\bibliographystyle{${TEXINPUTDIR}/uuu}
\bibliography{zzz}
                                                 
\end{document}
PART2

echo Generating .tex file....
awk -f $BIB2DVIDIR/bib2dvi1.awk $BIBALL > $BIBLIO.tmp4

cat $BIBLIO.tmp1 $BIBLIO.tmp2 $BIBLIO.tmp4 $BIBLIO.tmp3 > $BIBLIO.tex

echo Latex....
                
if [ $VERBOSE = false ]
then
    echo bye | latex $BIBLIO > /dev/null
else
    echo bye | latex $BIBLIO
fi

echo Bibtex....

if [ $VERBOSE = false ]
then
    echo bye | bibtex $BIBLIO > /dev/null
else
    echo bye | bibtex $BIBLIO
fi

echo Latex....

if [ $VERBOSE = false ]
then
    echo bye | latex $BIBLIO > /dev/null
else
    echo bye | latex $BIBLIO
fi

# cleaning up ....

rm -f $BIBLIO.tmp0 $BIBLIO.tmp1 $BIBLIO.tmp2 $BIBLIO.tmp3 $BIBLIO.tmp4 \
      $BIBLIO.pass $BIBLIO.tex $BIBLIO.aux $BIBLIO.bbl $BIBLIO.blg \
      $BIBLIO.log $BIBLIO.awk1 

echo Done....

