#!/bin/csh -f

set debug = 0

set fullprogname = $0
set progname = $fullprogname:t

set tempbase = $progname.$USER.$$

switch ($progname)
  case latcite:
    set bibstyle = plain
    set bibcommand = "bibtex $tempbase"
    breaksw
  case hlatcite:
    set bibstyle = hplain
    set bibcommand = "bibtex $tempbase"
    breaksw
  case htmlcite:
    set bibstyle = empty
    set bibcommand = "bib2xhtml -s $bibstyle $tempbase.aux $tempbase.html"
    breaksw
endsw

if ($?BIBSTYLE) then # need then/endif to avoid evaluating unset BIBSTYLE
  set bibstyle = $BIBSTYLE
endif

if ($debug) echo progname: "$progname", bibstyle: "$bibstyle"
if ($debug) echo bibcommmand: "$bibcommand"

# \bibstyle below ignored by bib2html/bib2xhtml
echo -n "\relax\
\bibstyle{$bibstyle}\
\bibdata{sources}\
\citation{" > $tempbase.aux
set args = ($*)
if ($#argv == 0) set args = `cat`
set count = 1
foreach key ($args)
  if ($count > 1) echo -n "," >> $tempbase.aux
  echo -n $key >> $tempbase.aux
  @ count++
end
echo -n "}" >> $tempbase.aux
if ($debug) cat $tempbase.aux
$bibcommand >`tty`
  # Sends warning/error messages to terminal even if output redirected
echo '' >`tty`
  # Separates warning/error messages from results when output not redirected
${progname}output $tempbase
rm $tempbase.*
