#!/bin/csh -f

# @(#)Collect and pack news with tin. V. 3.1 Sep 1994 josdb@xs4all.nl (Jos den Bekker)

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

if ( -e ~/.newsrc ) then
  cp ~/.newsrc ~/.newsrc.prev
else
  echo ""
  echo "       WARNING! File .newsrc is gone!"
  echo ""
  exit 1
endif

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

tin -r -n -S -s $workdir -c </dev/null >/dev/null

if ( `/bin/ls $workdir | wc -l` == 0 ) then
  echo ""
  echo "      WARNING! No unread news found..."
  echo ""
  cd
  rm -rf $workdir
endif

zip -rq news$date *
if ( $status != 0 ) then
  echo ""
  echo "      An error occurred while zipping your news files."
  echo "      Files can be found in $workdir."
  echo ""
  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 "      WARNING! File news$date.zip could not be moved to $polldir"
  echo "      File can be found in $workdir"
  echo ""
  exit 1
endif

cd
rm -rf $workdir
