#!/bin/csh -f
# Does reasonably pleasant spelling checking on latex files.
# Usage: splat file ...
#    OR  splat [-d dict_file ... | -f file ...]*
# If there is just a list of file names, they are files to be spell checked.
# If the -d and -f flags are used, they specify files containing words
# to be added to the dictionary or to be spell checked respectively.
# In any case, for any file foo that is spell checked, foo.spell is added
# to the dictionary if it exists.

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

if (-d $HOME/spell) then
  set spell_dir = $HOME/spell
else
  set spell_dir = ~rig/spell
endif

set checkfiles
set dictfiles
foreach i ($*)
  switch ("$i")
  case -d:
    set getdictfile
    breaksw
  case -f:
    unset getdictfile
    breaksw
  case -*:
    echo splat: '' unrecognized option: $i
    echo "usage: splat [-d dict_file ... | -f file ...]*"
    exit(1)
    breaksw
  default:
    if ($?getdictfile) then
      set dictfiles = ($dictfiles $i)
    else
      if ("$checkfiles" == "") set jobname = $i:r
      set checkfiles = ($checkfiles $i:r.tex)
      if (-e $i:r.spell) set dictfiles = ($dictfiles $i:r.spell)
    endif
  endsw
end

cat $spell_dir/{tex,latex,people,general}.spell [1-9].spell /dev/null* $dictfiles \
    | sort >/tmp/splat_wlist.$$

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

set noglob

foreach i (`splatex +/tmp/splat_wlist.$$ $checkfiles`)
  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 p:
      echo $i >>/tmp/people.spell.$$  # (and places)
      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
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, person/place, general, specific spelling lists? (y, n, or u)"
getanswer:
set answer = $<
switch ($answer)
case y:
  foreach update (tex latex people 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,people,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_wlist.$$
