#!/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.$$

if ( -d $workdir ) then
  echo ""
  echo "         ERROR! Working directory $workdir already exists."
  echo ""
  exit 1
else
  if ( -e $polldir/reply.zip ) then
    mkdir $workdir
    if ( $status != 0) then
      echo ""
      echo "         WARNING! Unable to create working directory $workdir"
      echo ""
      exit 1
    endif
    cd $workdir
    unzip -qq $polldir/reply
    if ( $status != 0 ) then
      echo ""
      echo "         WARNING! An error occurred while unzipping reply file."
      cd
      rm -rf $workdir
      exit 1
    endif
  else
    echo ""
    echo "          No reply file in $polldir"
    echo ""
    exit 1
  endif
endif
  
foreach file ( $workdir/* )
  if ( "`head -1 $file | cut -c-4`" == "From" ) then
    sendmail -t < $file > /dev/null
    if ( $status != 0 ) then
      echo ""
      echo "    WARNING! An error occurred while sending mail."
      echo "    Script will be aborted and reply file left in $polldir"
      echo ""
      cd
      rm -rf $workdir
      exit 1
    endif 
  else
    inews -h < $file > /dev/null
    if ( $status != 0 ) then
      echo ""
      echo "    WARNING! An error occurred while sending news."
      echo "    Script will be aborted and reply file left in $polldir"
      echo ""
      cd
      rm -rf $workdir
      exit 1
    endif
  endif
end

cd
rm -rf $workdir
rm $polldir/reply.zip
