#!/bin/csh -f

#   This script will deliver mail and news with uqwk (SOUP packet).
#   A file called "reply.zip" is expected in directory $home/poll,
#   or, if that doesn't exist, in directory $home
#   $Id$

set workdir=/tmp/uqwkrep.$$
set polldir=~/poll
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 -Uqq $repfile
if ( $status != 0 ) then
  echo "${prg}: error unzipping reply file."
  cd
  rm -rf $workdir
  exit 1
endif

if ( ! -e $workdir/REPLIES && -e $workdir/replies ) then
  foreach f ( * )
    mv $f `echo $f | tr '[a-z]' '[A-Z]'`
  end
endif

uqwk -m +L -RREPLIES -H$workdir -N$home/.newsrc -B0
if ( $status != 0 ) then
  echo "${prg}: uqwk error"
  cd
  rm -rf $workdir
  exit 1
endif

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

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