#!/bin/sh
# Filename: mailhx 
#
# Purpose:  it extracts the HXWZ file from mail. You can also process
#           and print it.  It has the capability to exact more than one
#           HXWZ file.  For example, if HXWZ editor distribute "zengkan"
#           "cm9202y1.gb, cm9202y2.gb" in one mail message, this program
#           will extract both of them.               
#  
# Author:   Yaoen Zhang
#           e-mail: zhang@csgrad.cs.vt.edu
#           May 1, 1993
#
# Setup:    It works on most machines without any setup.  
#           1. If it does not work on your systme, you have to find 
#              out where your mailbox is, and change the setting on 
#              the first line of the following code. 
#
# Usage:    mailhx [-r]  
#           
#           [-r] Use this option to run the program automatically.             
#                It will automatically rerun every Friday morning at
#                7 o'clock.
#
#           It extracts the current issue of HXWZ from your mail.
# Notes:  1.It can also retrieve other uuencoded GB file like the poems
#           distributed by "Chinese Poem Exchange and Discussion List".
#         2.It has been tested on DEC and Sun workstations.
#
# 
# This program is in public domain. This is no copyright.  If you find
# it useful, distribute it to your friends. Please distribute only
# original verison of this program.  Suggestions welcome.
 
file=/usr/spool/mail/`whoami`    # change it your own mail box
echo $file
echo "---> Trying to extract encoded HXWZ files"
echo 
cat  $file | sed -n -e '/^begin [0-9][0-9][0-9] .*\.gb/,/^end/p' \
 > /tmp/hxwzmail.tmp
while [ -s /tmp/hxwzmail.tmp ]
do 
  filename=`sed -n -e '1 p' /tmp/hxwzmail.tmp | cut -d' ' -f3` 
  echo "--->Uudecoding HXWZ file:  $filename "
  echo 
  uudecode /tmp/hxwzmail.tmp 
  #if you want to process it and print it, put the commands here.
  #take the character "#" off at the beginning of the next 7 lines.
#  echo "--->Transfer $filename to PS format" 
#  echo
#  gb2ps -b 1 -e 7 $filename $filename.ps # gb2ps, written by Mr. W. Sun.
#  lpr -Pprintname $filename.ps #change 'printername to a real one'
#  gb2ps -b 8 $filename $filename.ps 
#  lpr -Pprintname $filename.ps 
#  rm $filename.ps
  # The reason I use two gb2ps commands instead of one is that some
  # printer can not take a very large Postscript file.

  sed -e '1 d' /tmp/hxwzmail.tmp | \
  sed -n -e '/^begin [0-9][0-9][0-9] .*\.gb/,/^end/p' > /tmp/hxwzmail1.tmp
  mv /tmp/hxwzmail1.tmp /tmp/hxwzmail.tmp
done
rm /tmp/hxwzmail.tmp
echo
case $1 in
 -r)
  echo "It will automatically run again every Friday"
  echo "mailhx" | at 7am fr 
esac
#end
