#!/bin/sh
#
# Simple Offline News/Mail Reader
#
# Using SOUP format, upload and send mail and news article replies;
# package and download new mail and news articles using menu-based 
# system with uqwk.
#
# Copyright 1995 by Ken Gresham (kgresham@america.net)
# Permission is hereby granted to use, copy, modify and distribute this 
# software and it's documentation for any purpose and without fee provided
# that the above copyright notice appears in all copies and that the above 
# copyright notice and this permission notice appear in all supporting
# documentation. This software is provided "as is" and without expressed 
# or implied warranty.
# 
# --------------------------
# User Configuration Section
# --------------------------
# Reply packet name (name must end in .zip)
ReplyFile="reply.zip"
# New message packet name
NewFile="new.zip"
# Size of download packet in blocks (4000 blocks = ~500kb unzipped, or
# set to 0 for unlimited size [be careful!!])
BlockSize=4000
# Temporary working directory
TempWork="$HOME/Tempdir"
# SOUP Options
SOUPOPTS=" -r +L -H$TempWork -B$BlockSize "
# ---------------------------------
# End of User Configuration Section
# ---------------------------------

# Get a character from the terminal
GetChar()
{
stty raw
reply=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -raw
echo "" 
}

Transfer()
{
while :
do
clear
echo ""
echo ""
echo ""
echo "                  Simple Offline News/Mail Reader"
echo ""
echo "                  Select a transfer protocol"
echo ""
echo "                  X) Xmodem"
echo "                  Y) Ymodem"
echo "                  Z) Zmodem"
echo "                  F) ftp"
GetChar
case $reply in
[XxYyZzFf]*) Protocol=$reply
             export Protocol
             return;;
          *) echo ""
             echo "               $reply is not valid - try again"
             sleep 1;;
esac
done
}

Upload()
{
case $Protocol in
     X|x) xmodem -rb $TempWork/$ReplyFile;;
     Y|y) rb $TempWork/$ReplyFile;;
     Z|z) rz;;
     F|f) clear
          echo ""
          echo ""
          echo ""
          echo ""
          echo "                  Transfer $ReplyFile via ftp now..."
          echo "                  then press enter in this screen"
          echo "                  to continue..."
          GetChar;;
esac
}

Download()
{
case $Protocol in
     X|x) xmodem -sb $NewFile
          read junk;;
     Y|y) sb $NewFile
          read junk;;
     Z|z) sz -w 8192 $NewFile
          read junk;;
     F|f) clear
          echo ""
          echo ""
          echo ""
          echo ""
          echo "                  Transfer $NewFile via ftp now..."
          echo "                  then press enter in this screen"
          echo "                  to continue..."
          GetChar;;
esac
}

RmFiles()
{
# Look for temporary files and directory - remove if found
if [ -f $TempWork/* ] 
then rm $TempWork/* 
fi
if [ -f $TempWork/.newsrc ]
then rm $TempWork/.newsrc
fi
rmdir $TempWork
# Remove old reply file if found
if [ -f $ReplyFile ]
then rm $ReplyFile
fi
# Remove old message file if found
if [ -f $NewFile ]
then rm $NewFile 
fi
}

# Main Program
#
# Clean up old files from aborted session
# Prompt to send old reply file if found, then remove
if [ -f $ReplyFile ]
then clear
     echo "\n\n\n"
     echo "               Old $ReplyFile found - send now? (y/n)"
     GetChar
     case $reply in
       Y|y) Upload;;
     esac
     rm $ReplyFile
fi
# Prompt to download old message file if found, then remove
if [ -f $NewFile ]
then clear
     echo "\n\n\n"
     echo "               Old $NewFile found - download now? (y/n)"
     GetChar
     case $reply in
       Y|y) Download;;
     esac
     rm $NewFile 
fi
# Create temporary working directory if needed
if [ ! -d $TempWork ]
then mkdir $TempWork
fi
# Get file transfer protocol
Transfer
while :
do
# Main menu
clear
echo ""
echo ""
echo ""
echo "                     Simple Offline News/Mail Reader"
echo ""
echo "                  1) Upload reply packet ($ReplyFile)"
echo "                  2) Download new mail"
echo "                  3) Download newsgroup articles"
echo "                  4) Download mail and newsgroup articles"
echo ""
echo "                  Q) Quit"
GetChar
Selection=$reply
# Copy .newsrc to working directory
cp $HOME/.newsrc $TempWork/.newsrc
case $Selection in
  1) Upload
     unzip -Uj $ReplyFile
     clear; echo "Sending messages..."; echo ""
     uqwk -m -n +L -RREPLIES
     sleep 1;;
  2) clear; echo "Packaging messages..."; echo ""
     uqwk +m -n $SOUPOPTS
     zip -j $NewFile $TempWork/AREAS $TempWork/*.MSG
     sleep 1
     Download;;
  3) clear; echo "Packaging messages..."; echo
     uqwk -m +n $SOUPOPTS
     zip -j $Newfile $TempWork/AREAS $TempWork/*.MSG
     sleep 1
     Download;;
  4) clear; echo "Packaging messages..."; echo
     uqwk +m +n $SOUPOPTS
     zip -j $NewFile $TempWork/AREAS $TempWork/*.MSG
     sleep 1
     Download;;
Q|q) clear
     # Clear out temporary files
     RmFiles
     exit;;
  *) ;;
esac
# Copy updated $TempWork/.newsrc to $HOME/.newsrc
cp $TempWork/.newsrc $HOME/.newsrc
done
