#!/bin/csh -f

if ($#argv == 0) then
  echo "Usage: lat file [extension ...]"
  exit(1)
endif

set fileroot = $1:r

if ($#argv == 1) then
  set exts = (aux glo idx index lof lot toc)
else
  set exts = ($argv[2-])
endif

set foundexts
foreach ext ($exts)
  if (-f $fileroot.$ext) then
    cp $fileroot.$ext $fileroot.${ext}hold
    set foundexts = ($foundexts $ext)
  endif
end

latex $1
set result = $status
if ($result) then
  echo "lat: exit status $result from latex; swapping auxiliary files"
  swapauxs $fileroot $foundexts
endif
