#!/bin/csh -f

# Collecting mail and news in SOUP file and sending replies with uqwk.
# @(#)UQWKCollect V. 3.2 Sep 1994 Jos den Bekker josdb@xs4all.nl
# Thanks to H.Shrikumar shri@cs.umass.edu for the mail-lock function

set workdir=/tmp/uqwk.$$
set polldir=~/poll
set date=`date +%d%m`
set repfile=~/poll/reply.zip
set nonomatch

if ( -d $workdir ) then
  echo ""
  echo "          Error! Working-directory $workdir already exists."
  echo ""
  exit 1
else
  mkdir $workdir
  if ( $status != 0 ) then
    echo ""
    echo "          Error! Unable to create working-directory $workdir"
    echo ""
    exit 1
  else
    cd $workdir
  endif
endif

if ( -e $repfile ) then
  unzip -Uqq $repfile
  if ( $status != 0 ) then
    echo ""
    echo "           An error occurred while unzipping your reply file."
    echo "           Replies cannot be sent."
    echo ""
  endif
endif

( echo 's1-$' $workdir/qwk$$ ; echo q ) | /usr/ucb/mail >/dev/null
if ( $status != 0 ) then
  echo ""
  echo "           WARNING! An error occurred while"
  echo "           locking your mail file."
  echo "           Mail will not be collected"
  echo ""
  goto newssection
endif

if ( -e $workdir/REPLIES ) then
  uqwk +L -H$workdir -f$workdir/qwk$$ -RREPLIES -B0 >/dev/null
  if ( $status != 0 ) then
    echo ""
    echo "          WARNING! An error occurred while"
    echo "          collecting mail and sending reply file."
    echo "          Script will be aborted.
    echo "          Mail box is moved to $workdir/qwk$$"
    echo ""
    exit 1
  else
    mv $repfile $repfile.prev
    goto mailzip
  endif
else
  uqwk +L -H$workdir -f$workdir/qwk$$ -B0 >/dev/null
  if ( $status != 0 ) then
    echo ""
    echo "           WARNING! An error occurred while collecting mail"
    echo "           Script will be aborted.
    echo "           Mail box is moved to $workdir/qwk$$"
    echo ""
    exit 1
  endif
endif

mailzip:
zip -mbqj /tmp mail$date AREAS *.MSG
if ( $status != 0 ) then
  echo ""
  echo "          An error occured while zipping your files."
  echo "          Mail-output of uqwk in $workdir"
  echo ""
  exit 1
endif

if ( -e $polldir/mail*.zip.* ) rm $polldir/mail*.zip.*

if ( -e $polldir/mail$date.zip ) mv $polldir/mail$date.zip $polldir/mail$date.zip.$$

mv mail$date.zip $polldir
if ( $status != 0 ) then
  echo ""
  echo "        File mail$date.zip could not be moved to $polldir"
  echo "        File is in $workdir"
  echo ""
  exit 1
endif  

newssection:
if ( -e ~/.newsrc ) then
  cp ~/.newsrc ~/.newsrc.prev
  cp ~/.newsrc $workdir
else
  echo ""
  echo "           WARNING! File .newsrc does not exist."
  echo "           News cannot be collected."
  echo ""
  cd
  rm -rf $workdir
  exit 1
endif

if ( -e $workdir/REPLIES ) then
  uqwk -m +n +x +L -H$workdir -RREPLIES -B0 >/dev/null
  if ( $status != 0 ) then
    echo ""
    echo "          WARNING! An error occurred while collecting"
    echo "          news and sending reply file."
    echo "          File .newsrc will be unchanged."
    echo ""
    cd
    rm -rf $workdir
    exit 1
  else
    mv $repfile $repfile.prev
    goto newszip
  endif
else
  uqwk -m +n +x +L -H$workdir -B0 >/dev/null
  if ( $status != 0 ) then
    echo ""
    echo "           WARNING! An error occurred while collecting news"
    echo "           File .newsrc will be unchanged."
    echo ""
    cd
    rm -rf $workdir
    exit 1
  endif
endif

newszip:
zip -mbqj /tmp news$date AREAS *.MSG
if ( $status != 0 ) then
  echo ""
  echo "        An error occured while zipping your files."
  echo "        News-output of uqwk in $workdir"
  echo ""
  echo "        Attention! File .newsrc will be changed."
  echo "        Old record in .newsrc.prev"
  echo ""
  cp $workdir/.newsrc $home
  exit 1
endif

if ( -e $polldir/news*.zip.* ) rm $polldir/news*.zip.*

if ( -e $polldir/news$date.zip ) mv $polldir/news$date.zip $polldir/news$date.zip.$$

mv news$date.zip $polldir
if ( $status != 0 ) then
  echo ""
  echo "        File news$date.zip could not be moved to $polldir"
  echo "        File is in $workdir"
  echo ""
  echo "        Attention! File .newsrc will be changed."
  echo "        Old record in .newsrc.prev"
  echo ""
  cp $workdir/.newsrc $home
  exit 1
endif

cp $workdir/.newsrc $home
cd
rm -rf $workdir
