#!/bin/csh -f
#
# bibmake V1.00
#
# Script to extract entries from bibliographies and format 
#
# Written David Pascoe December 1989
#
# default is text output and alpha bibliography style
# see bibmake(l) for more info.

set file=0			# file name is not specified
set out=file			# default is text output
set key=0			# keys are empty
set num=0			# number of entries
set style=alpha			# default style name
set stat=0			# return status
set pages=0			# number of A4 pages generated
set sourcefiles=""		# source bibliography files
set bibtexfiles=""

if ($#argv < 3) then
er:
     echo "Usage: bibmake [-t|-p|-l] [-s style] -f bib_files .... -k keywords ....." 
     exit(1)
   endif
top:

if ($#argv >= 2) then
   switch ($argv[1])

  case -t:  		# output type is text
      set out=file
      shift argv
      goto top   

  case -p:		# output type is printed document
      set out=lw
      shift argv
      goto top

  case -l:		# output type is suitable for straight LaTeX
      set out=latex
      shift argv
      goto top

  case -s:		# specify the name of the style to use
      shift argv
      set style = $argv[1]
      shift argv
      goto top

  case -k:		# now read in the keywords
   set key=1
   if ($file != 0) then
lab:
     shift argv
     if ($argv[1] != "") then
	   partbibtex $argv[1] $sourcefiles >> /tmp/$$.bib
	      if ($#argv > 1) then
	        goto lab
              else
    	       set num = `grep -c \@ /tmp/$$.bib`
	       	if ($num == "0") then
	           echo "bibmake: No matching entries found"
		   exit(1)
                else
 		   if ($num == 1) then
                     echo $sourcefiles : 1 entry found
                   else
                     echo $sourcefiles : $num entries found 
                   endif
                endif
     endif
   endif
   goto top
   else
      # the filename for the bib file must be given first
      echo "bibmake: Database file name must be specified first"
      echo
      exit(1)
   endif
			
    case -f:		# read in the name of the bib file 
        shift argv
    loop:
	set file=$argv[1]
	if !(-e $file.bib) then
	   echo "bibmake: Bibliography File "$file.bib" Not Found"
	   echo
	   exit(1)
        else
           cp $file.bib /tmp
           set sourcefiles=($sourcefiles" "$file.bib)
	   if ($bibtexfiles == "") then
              set bibtexfiles=$file
           else
              set bibtexfiles=($bibtexfiles","$file)
	   endif
           shift argv
           if ($argv[1] == "-k") then
             goto top
           else
             goto loop
           endif
	endif
	shift argv
   echo $files
	goto top

    default:
        goto er

 endsw

endif
if ($key == 0) then
    echo "bibmake: no keywords specified"
    exit(1)
endif
if ($out == "file" || $out == "latex") then
#
# create an .aux file to run straight through BibTeX and then
# massage the .bbl file to output a "text" form of the database
#
    # put in the requested style types and bibfile location
    echo "\bibstyle{"$style"}" > /tmp/$$.aux
    echo "\bibdata{"$bibtexfiles"}" >> /tmp/$$.aux
    # change all of the key fields into citations
    sed -n -e 's/^.*@.*{\([^,]*\),\(.*\)$/\\citation{\1}/p' < /tmp/$$.bib >> /tmp/$$.aux
    # attempt to catch an unknown style file error in BibTeX
    set stat = `bibtex /tmp/$$ >& /dev/null`
    if (-z /tmp/$$.bbl) then 
      echo bibmake: BibTeX failed. possibly style file: $style not available
      echo bibmake: or execeeded BibTeX string limit.
      rm -f /tmp/$$.*
      exit(1) 
    endif
    if ($out == "latex") then
      cat /tmp/$$.bbl
    else
      sed -e 1d -e \$d -e 's/\\newblock//' -e 's/\\bibitem\(\[.*\]\).*}/\1/' -e 's/\~/ /g' -e 's/\\bibitem{.*}//' -e 's/{\([^}]\)}/\1/g' < /tmp/$$.bbl
    endif
    rm /tmp/$$.*
    rm /tmp/*.bib
else
#
#  create a .tex file to run twice through LaTeX and then BibTeX and twice
#  through LaTeX to incorporate the bibliography.
# 
    cd /tmp
    echo "\documentstyle[a4all]{article} \begin{document}\scriptsize" > /tmp/$$.tex
    echo "\bibliographystyle{"$style"}\bibliography{"$bibtexfiles"}" >> /tmp/$$.tex
    # find the key fields in the .bib file and change to nocites
    sed -n -e 's/^.*@.*{\([^,]*\),\(.*\)$/\\nocite{\1}/p' < /tmp/$$.bib >> /tmp/$$.tex
    echo "\end{document}" >> /tmp/$$.tex
    # run through twice to get the .aux file ok
    latex /tmp/$$  >& /dev/null
    latex /tmp/$$  >& /dev/null
    # attempt to catch an unkown style file error in BibTeX
    set stat = `bibtex /tmp/$$ >& /dev/null`
    # if the .bbl file is empty 
    if (-z /tmp/$$.bbl) then
      echo bibmake: BibTeX failed. possibly style file: $style not available
      echo bibmake: or exceeded BibTeX string limit.
      rm  /tmp/$$.*
      exit(1) 
    endif
    latex /tmp/$$  >& /dev/null
    latex /tmp/$$  >& /dev/null
    # find the number of pages that were generated by looking at the .log file
    set pages = `sed -n -e 's/Output written.*(\(.*\),.*)./\1/p' < /tmp/$$.log`
    dvi2ps /tmp/$$ -q | lpr -v -Plw
    echo $file.bib: $pages generated
    rm /tmp/$$.*
    rm /tmp/*.bib
  endif
endif

done:
