#!/bin/csh -f

if ($#argv == 0) then
  echo "Usage: faxtex dvi-or-ps-file-or-fileroot [sendfax-options]"
  exit 2
endif

set fileroot = $1:r

set tofaxnum = `sed -n 's/.*\\tofax{\([^}]*\)}.*/\1/p' $fileroot.tex | tail -1`
set tofaxname = `sed -n 's/.*\\faxhead{\([^}]*\)}.*/\1/p' $fileroot.tex | tail -1`
set docoptions = `sed -n 's/.*\\sendfaxoptions{\([^}]*\)}.*/\1/p' $fileroot.tex | tail -1`

if ("$tofaxnum" == "") then
  echo "faxtex: No destination fax number in $fileroot.tex"
  exit 3
endif

if (-r $fileroot.dvi) set file = $fileroot.dvi
if (-r $fileroot.ps) set file = $fileroot.ps
if ("$1" == "$fileroot.dvi" && -r $1) file = $1

if (! $?file) then
  echo "faxtex: Can't open $fileroot.ps or $fileroot.dvi"
  exit 4
endif

sendfax $docoptions $argv[2-] -d "$tofaxname@$tofaxnum" -n -R $file
