#!/bin/csh -f

# @(#)Send mail and news v. 5.0 Oct 1994 josdb@xs4all.nl (Jos den Bekker)

set polldir=~/poll
set workdir=/tmp/send.$$
set prg=`basename $0`

if ( ! -d $polldir ) then
  set polldir=~
endif

set repfile=$polldir/reply.zip

if ( ! -e $repfile ) then
  echo "${prg}: no reply file"
  exit 1
endif

if ( -d $workdir ) then
  echo "${prg}: working directory $workdir already exists."
  exit 1
endif

mkdir $workdir
if ( $status != 0) then
  echo "${prg}: error creating working directory $workdir"
  exit 1
endif

cd $workdir

onintr interrupt

unzip -qq $repfile
if ( $status != 0 ) then
  echo "${prg}: zip error"
  cd
  rm -rf $workdir
  exit 1
endif
  
foreach file ( * )
  if ( "`head -1 $file | cut -c-4`" == "From" ) then
    sendmail -t < $file
    if ( $status != 0 ) then
      echo "${prg}: sendmail error; aborting"
      cd
      rm -rf $workdir
      exit 1
    endif 
  else
    inews -h < $file
    if ( $status != 0 ) then
      echo "${prg}: inews error; aborting"
      cd
      rm -rf $workdir
      exit 1
    endif
  endif
end

cd
rm -rf $workdir
mv $repfile $repfile.prev
exit 0

interrupt:
onintr -
cd
echo "${prg}: interrupted"
rm -rf $workdir
exit 1
