#!/bin/sh
##########################################################################
#     UQWK_SOUP grab script v0.95 for THOR.  Author: Kjell Irgens.       #
# Should be safe to use, but I take *no* responsibilities for lost mail. #
# grab -h for help.                                                      #
##########################################################################

grab=soup                  # modify if the grab in THOR name is not "soup"
wdir=soup                  # name of work directory (UN*X side)

       UQ_HOME_DIR=. UQ_DO_NEWS=1 UQ_SOUP_MODE=1 UQ_NRC_FILE=$HOME/.newsrc
export UQ_HOME_DIR   UQ_DO_NEWS   UQ_SOUP_MODE   UQ_NRC_FILE

repliesfile=$grab\.out
grabfile=$grab`date +%H%M%S`.zip

mkdir 2>/dev/null $HOME/$wdir
cd $HOME/$wdir

no_rz=false no_sz=false restore=false UQ_DO_MAIL=1

for i in $*
do
  case $i in
  -r ) no_rz=true ;;
  -s ) no_sz=true ;;
  -n ) UQ_DO_NEWS=0 ; export UQ_DO_NEWS ;;
  -m ) UQ_DO_MAIL=0 ; export UQ_DO_MAIL ;;
  -u ) UQ_MAX_BLKS=0 ; export UQ_MAX_BLKS;;
  -x ) if [ -r .mail_backup ]
       then echo "Restoring mail..."
       else echo "No mail to restore"
       fi
       cat 2>/dev/null .mail_backup >>$MAIL && rm 2>/dev/null .mail_backup
       restore=true;;
  -z ) if [ -r .newsrc_backup ]
       then echo "Restoring news..."
       else echo "No news to restore"
       fi
       mv 2>/dev/null .newsrc_backup $UQ_NRC_FILE
       restore=true;;
  *  )
       echo "Usage: $0 [option] ..."
       echo "[-r]  no rz";echo "[-s]  no sz";echo "[-n]  don't grab news"
       echo "[-m]  don't grab mail";echo "[-u]  unlimited grab size"
       echo "[-x]  restore mail";echo "[-z]  restore news"
       echo "[-h]  this text"
       exit 0 ;;
  esac
done

if [ $restore = true ]
then exit 0
fi

# backups never hurt anyone
if [ $UQ_DO_MAIL = 1 ]
then cp 2>/dev/null $MAIL .mail_backup
fi
if [ $UQ_DO_NEWS = 1 ]
then cp 2>/dev/null $UQ_NRC_FILE .newsrc_backup
fi


if [ $no_rz = false ]
then rz
fi

if [ -r $repliesfile ]
then unzip -U $repliesfile
fi

if [ -r REPLIES ]
then uqwk -RREPLIES
else uqwk
fi

rm 2>/dev/null $repliesfile

if [ -r AREAS ]
then
  zip -m $grabfile AREAS *.MSG
  if [ $no_sz = false ]           # assumes you use zmodem, you could also
  then                            # use: kermit -i -s $grabfile && ...
    sz -br $grabfile && rm $grabfile
  fi
fi
