#!/bin/csh -f 
#
# Filename:   gethx   ---   a C shell program. 
#
# Author:     Yaoen Zhang  
#             e-mail:  zhang@csgrad.cs.vt.edu
#             Written: March 22, 1993
#
# Purpose:    "gethx" --- it gets "Hua Xia Wen Zai" file in PostScript 
#             format from an anonymous ftp server machine in your area,
#             process and print these files, and do the clean up. 
#
# Advantage:  It saves your time and effort.  And it also saves network
#             and server machine time.  
#
# Setup:      First, you have to do a little setup before you can run it. 
#             1. Create a file ".hxwz" in your home directory, and put 
#                the total release number in it.
#                e.g. "echo 101 > $HOME/.hxwz"
#             2. Change the 4th line of the following code.  Put your 
#                own e-mail address after "=".  
#                e.g. "set PASS   = liwang@lcs.ohio-state.edu"
#             Once you have finished setup, all you need to do is 
#             typing the command "gethx" every Friday.  That's it. 
#
# Usage:      gethx [-h] [-p] [-s]
#
#             The GB file of current issue of HXWZ will be retrieved. 
#
# Options:    -h    This help message will be echoed.
#             -p    Postscript file of current issue of HXWZ be retrieved. 
#             -s    Specific issue of GB file will be retrieved. The
#                   filename should follows "-s".
#                   e.g. "gethx -s zk9302a.gb"
#
# Notes:   You can do more with this program. 
#       1. IMPORTANT: You should change the SERVERHOST machine to the 
#          one near you site. (it is on the first line of code). 
#       2. For HXWZ editors, you can modify it to distribute cnd-cm's
#          GB format and Postcript files to all server machines.
#       3. You can modify it to get any file from any ftp server. 
#
# Credits: FTP command is based on getnettables, by John Dotts Hagan
#
# This program is in public domain. This is no copyright.  If you find 
# it useful, distribute it to your friends. Suggestions welcome.

set SERVERHOST	= ifcss.org     #please change to your local hxwz server
set DIRNAME     = cnd/hxwz/HXWZ-PS-NEW 
set DIRNAMEGB     = cnd/hxwz

#set SERVERHOST	= grumpy.ssc.gov    #another HXWZ server, only GB files
#set DIRNAMEGB     = pub/Chinese-Files/Hua-Xia-Digest

#set SERVERHOST	= lunix.met.kth.se  #another HXWZ server, only GB files
#set DIRNAMEGB     = pub/hxwz

set ACCT        = anonymous
set PASS	= zhang@csgrad.cs.vt.edu #put your own e-mail address

@ VALUE = `cat $HOME/.hxwz`
@ VALUE +=1 
set NUMBER = `cat $HOME/.hxwz`
set FNAME = HXWZ"$NUMBER"_1    #to get GB file,put sth like"cm9303a"
set FFNAME = HXWZ"$NUMBER"_2   #and change "get $FNAME.gb" in ftp. 

if( $1 == '-h') then
 echo ""
 echo "Usage:   gethx [-h] [-p] [-s]"
 echo "         The GB file of current issue of HXWZ will be retrieved." 
 echo ""
 echo "Options: -h  This help message will be echoed."
 echo "         -p  Postscript file of current issue of HXWZ be retrieved." 
 echo "         -s  Specific issue of GB file will be retrieved. The"
 echo "             filename should follows -s."
 echo "             e.g. 'gethx -s zk9302b1.gb zk9302b2.gb'"
  
else if( $1 == '-s') then
shift
if( $* == '') then
  echo "   No file names are specified."
  exit(0)
endif 
echo "---> getting files: $*"
echo "Trying to connect to machine $SERVERHOST"
unset noclobber
echo "open $SERVERHOST\
user $ACCT $PASS\
bin\
hash\
cd $DIRNAMEGB \
mget $* \
quit" | ftp -n -i -v -d

else if( $1 == '-p') then
echo "Trying to connect to machine $SERVERHOST"
unset noclobber
echo "open $SERVERHOST\
user $ACCT $PASS\
bin\
hash\
cd $DIRNAME \
get $FNAME.tar.Z \
get $FFNAME.tar.Z \
quit" | ftp -n -i -v -d

if ( -f $FNAME.tar.Z ) then
   echo "--->uncompress file $FNAME.tar.Z"
   uncompress $FNAME.tar.Z
   tar xvf $FNAME.tar
   rm $FNAME.tar

# to print directly and clean up, remove "#" at the beginning of the next
# 3 lines, and change the "printername" to your printer name.

#   echo "Printing the postcript files"
#   lpr -h -Pprintername PSpage??       
#   rm PSpage??
else 
 	echo "new hxwz file $FNAME.tar.Z was not created" 
        exit(-1) 
endif

if ( -f $FFNAME.tar.Z ) then
   echo "--->uncompress file $FFNAME.tar.Z"
   uncompress $FFNAME.tar.Z
   tar xvf $FFNAME.tar
   rm $FFNAME.tar

# to print directly and clean up, remove "#" at the beginning of the next
# 3 lines, and change the "printername" to your printer name.

#   echo "Printing the postcript files"
#   lpr -h -Pprintername PSpage??       
#   rm PSpage??
   echo $VALUE > $HOME/.hxwz  # update the total release number
else 
 	echo "new hxwz file $FNAME.tar.Z was not created" 
 	exit(-1)
endif

else                 #get GB file by default. 
echo "---> Getting the gb file "
set Today = `date +"%Y %m %d"`
set Year = `date +"%y"`
echo "This is `date | cut -c1-11,24-28`"
set Friday = `cal | tail -6 | head -5 | cut -c16-17 | cat `

if($Today[3] < $Friday[1]) then
    @ Prev = $Today[2]
    @ Prev -= 1
    set PFriday = `cal $Prev $Today[1] | tail -6 | head -5 | cut -c16-17 | cat`
    if($#PFriday == 4) then 
      set FileLetter = 'd'
    else
      set FileLetter = 'e'
    endif 
    if( $Prev < 10 ) then
      set FileName = `echo "$Year"0$Prev$FileLetter`
    else 
      set FileName = `echo $Year$Prev$FileLetter`
    endif
else if ($Today[3] < $Friday[2]) then 
    set FileName = `echo $Year$Today[2]a`
else if ($Today[3] < $Friday[3]) then 
    set FileName = `echo $Year$Today[2]b`
else if ($Today[3] < $Friday[4]) then 
    set FileName = `echo $Year$Today[2]c`
else if ($#Friday == 4) then 
    set FileName = `echo $Year$Today[2]d`
    echo "there are 4 fridays"
else 
    @ LastFriday = ($Friday[4] + 7) 
    if ($Today[3] < $LastFriday) then
      set FileName = `echo $Year$Today[2]d`
    else
      set FileName = `echo $Year$Today[2]e`
    endif 
endif
echo "The HXWZ filename in GB format is: cm$FileName.gb"
if (`echo cm$FileName.gb` == `cat $HOME/.hxwzgb`) then
  echo "You have already retrieved the most recent file: cm$FileName.gb"
  exit(-1)
endif
# using ftp to get the GB file 
echo "Trying to connect to machine $SERVERHOST"
unset noclobber
echo "open $SERVERHOST\
user $ACCT $PASS\
bin\
hash\
cd $DIRNAMEGB \
get cm$FileName.gb \
quit" | ftp -n -i -v -d
echo "cm$FileName.gb" > $HOME/.hxwzgb
#  If you want to process the GB file and print it, remove "#" at
#  the beginning of the next 7 lines.  The printing software I use is
#  gb2ps.  Change that command to what ever you are using.

#  echo "---> Printing the current issue of HXWZ"
#  gb2ps -t -b 1 -e 7 "cm$FileName.gb" "$FileName.ps1"
#  lpr -Pprintername -h "$FileName.ps1"
#  rm "$FileName.ps1"
#  gb2ps -t -b 8  "cm$FileName.gb" "$FileName.ps2"
#  lpr -Pprintername -h "$FileName.ps2"
#  rm "$FileName.ps2"

endif       # end of retrieving the GB format file
