#!/bin/csh -f
# Does reasonably pleasant spelling checking on latex files (with \input's etc.)
# Usage: splat fileroot

set path = (~rig/bin/tex ~rig/bin/text /bin /usr/bin)

set spell_dir = ~rig/spell; set bib_dir = ~rig/tex

if ($#argv != 1) then
  echo "usage: splat fileroot"; exit(1)
endif

set input = $1; set jobname = $input:r

echo "splat: Making dict.spell ..."; (cd $spell_dir; make dict.spell)

if ("$input:e" != "bib") then
  echo "splat: Checking sources.bib ..."; (cd $bib_dir; make sources.spell)
else
  cp $spell_dir/bibtex.spell 9.spell
endif

cat $spell_dir/dict.spell $jobname.spell [1-9].spell /dev/null* \
    | sort >/tmp/splat_persdict.$$

rm -f /tmp/{errors,$jobname,tex,latex,bibtex,people,unix,general}.spell.$$ /dev/null*

set noglob

echo "splat: Checking $input..."

foreach i (`detex $input -l | sepwordsdepunc | ispell -l -W 0 -w"0123456789,.:@]['" -p /tmp/splat_persdict.$$ | sort -u`)
  if ($?scroll) then
    echo $i >>/tmp/errors.spell.$$
  else
  getaction:
    echo "$i ?"
    set action = $<
    switch ($action)
    case s:  # flow through to "" case
      set scroll
    case "":  # flow into here from s case
      echo $i >>/tmp/errors.spell.$$
      breaksw
    case a:
      breaksw
    case i:
      echo $i >>/tmp/$jobname.spell.$$
      breaksw
    case t:
      echo $i >>/tmp/tex.spell.$$
      breaksw
    case l:
      echo $i >>/tmp/latex.spell.$$
      breaksw
    case b:
      echo $i >>/tmp/bibtex.spell.$$
      breaksw
    case p:
      echo $i >>/tmp/people.spell.$$  # (and places)
      breaksw
    case u:
      echo $i >>/tmp/unix.spell.$$
      breaksw
    case g:
      echo $i >>/tmp/general.spell.$$
      breaksw
    case [1-9]:
      echo $i >>$action.spell
      breaksw
    case h:
      cat <<"endheredoc"
Type one of the follow actions; always end with carriage return

     (just a carriage return) a real error to go into final output of splat
s    scroll all further output; treat them all as real errors
a    accept the word for just this run
i    insert the word into a dictionary with same root name as first check file
t    insert the word into your dictionary of tex artifacts
l    insert the word into your dictionary of latex artifacts
b    insert the word into your dictionary of bibtex artifacts
p    insert the word into your dictionary of person and place names
g    insert the word into your general dictionary
1-9  insert the word into one dictionary among 1.spell - 9.spell
h    print out this help message

Note: At end of run, you must confirm any i, t, l, p, \& g additions to dictionaries.
"endheredoc"
      goto getaction
    default:
      echo "splat: Unrecognized action.  Use 'h' for help."
      goto getaction
    endsw
  endif
end

echo "Update tex, latex, bibtex, person/place, unix, general, specific spelling lists? (y, n, or u)"
getanswer:
set answer = $<
switch ($answer)
case y:
  foreach update (tex latex bibtex people unix general jobnameplaceholder)
    set old = $spell_dir/$update.spell
    set additions = /tmp/$update.spell.$$
    if ("$update" == "jobnameplaceholder") then
      set old = $jobname.spell
      set additions = /tmp/$jobname.spell.$$
    endif
    if (-f $additions) then
      cat $additions >>$old
      sort -df -o $old $old
      rm $additions
    endif
  end
case n:
  rm -f /tmp/{tex,latex,bibtex,people,unix,general,$jobname}.spell.$$
case u:
  breaksw
default:
  echo "Please answer y, n, or u for yes, no, or undecided (leave new things on /tmp)."
  goto getanswer
endsw

if (-e /tmp/errors.spell.$$) then
  cat /tmp/errors.spell.$$
  rm -f /tmp/errors.spell.$$
endif

rm -f /tmp/splat_persdict.$$
