#!/bin/csh -f

# Collecting mail and news in SOUP file and sending replies with uqwk.
# @(#)soup Jos den Bekker josdb@xs4all.nl
# $Id$

set workdir=/tmp/uqwk.$$
set polldir=~/poll
set date=`date +%d%m`
set args="+x +L -H$workdir -B0"
set prg=`basename $0`
set nonomatch

# N.B.: Customize the following variable according to you preference,
# or according to the availability of either procmail's lockfile,
# or /usr/ucb/mail ("ucbmail") on your Unix system.

set lockfile=ucbmail

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

set repfile=$polldir/reply.zip

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

if ( -e $repfile ) then
  unzip -Uqq $repfile
  if ( $status != 0 ) then
    echo "${prg}: error unzipping reply file; replies cannot be sent."
  else
    if ( ! -e $workdir/REPLIES && -e $workdir/replies ) then
      foreach f ( *.msg )
        mv $f `echo $f | tr '[a-z]' '[A-Z]'`
        mv replies REPLIES
      end
    endif
    set args="-RREPLIES $args"
    set doreply
  endif
endif
                                           
if ( $?MAIL && -e $MAIL && ! -z $MAIL ) then
  switch ( $lockfile )
    case "ucbmail":
      ( echo 's1-$' $workdir/qwk$$ ; echo q ) | /usr/ucb/mail >/dev/null
      if ( $status == 0 ) then
        set domail
      endif
      breaksw
    case "lockfile":
      lockfile -ml
      mv $MAIL $workdir/qwk$$ >& /dev/null 
      lockfile -mu
      set domail
      breaksw
    default:
      echo "{prg}: no mail locking; mail is skipped"
      breaksw
  endsw
endif

if ( $?domail ) then
  set args="+m -f$workdir/qwk$$ $args"
else
  set args="-m $args"
endif  

if ( -e ~/.newsrc ) then
  cp ~/.newsrc ~/.newsrc.prev
  cp ~/.newsrc $workdir
  set args="+n $args"
  set donews
else
  echo "${prg}: Warning! .newsrc does not exist; news cannot be collected."
endif

if ( ! $?domail && ! $?donews && ! $?doreply ) then
  echo "${prg}: nothing to do."
  cd
  rm -rf $workdir
  exit 1
endif

uqwk $args >/dev/null
if ( $status != 0 ) then
  echo "${prg}: uqwk error; output is in $workdir"
  exit 1
else if ( -e $repfile ) then
  mv $repfile $repfile.prev
endif

zip -mbqj /tmp soup$date AREAS *.MSG
if ( $status != 0 ) then
  echo "${prg}: zip error; output is in $workdir
  echo ""
  echo "    Attention! If news is collected, .newsrc will be changed."
  echo "    Old record in .newsrc.prev"
  echo ""
  if ( -e $workdir/.newsrc ) cp $workdir/.newsrc $home 
  exit 1
endif
             
rm $polldir/soup*.zip.* >& /dev/null

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

mv soup$date.zip $polldir
if ( $status != 0 ) then
  echo "${prg}: error moving $workdir/soup$date.zip to $polldir"
  if ( -e $workdir/.newsrc ) cp $workdir/.newsrc $home
  exit 1
endif

if ( -e $workdir/.newsrc ) cp $workdir/.newsrc $home
cd
rm -rf $workdir
exit 0

interrupt:
onintr -
if ( $lockfile == "lockfile" ) then
  lockfile -mu
endif
cd
echo -n "${prg}: interrupted... "
if ( `/bin/ls $workdir | wc -l` == 0 ) then
  echo "no output"
  rm -rf $workdir
else 
  echo "output is in $workdir"
endif
exit 1
