#!/bin/sh
# hhg - version 2.10
# Use hhcp to get directory listings and files via FT-RELAY from specific
#   site/directory combinations offering public access.
#
# Contents: 1. Installation and use
#           2. Programme
#           3. Installation data edited by users
#           4. Site/BaseDirectory data edited by users
#           5. History
# H.Boothroyd, Warwick Business School, October 1991; corrected April 1992
# bsrdp@warwick.ac.uk
#
# ==========================================================================
# 1. Installation and use
# =======================
# a) This is a unix executable almost ready to run.
#    Use a text editor on Section 3 to prepare  hhg  for use by you.
#    Use a text editor on Section 4 to add data on sites/basedirectories.
# b) In ordinary use (i.e. after Section 3 preparation) enter
#       hhg
#    for guidance on syntax and an up-to-date list of site names.
# c) If an  hhg  request
#    *  works, a suitably named file will arrive,
#    *  fails, hhcp will send an automatic report to your mailbox,
#    *  seems to have got lost, use commands of the form
#               hhq -u YourUsername               hhlog | less
#       to look for information about it.
#
# =========================================================================
# 2. Script
# =========
# This is a shell script containing and running a gawk script.

# Test for presence of required unix utilities, if present add marker file
# Not foolproof - older versions of gawk will probably not cope
[ -f $HOME/.hhgok ] || {
    for i in gawk hhalias hhstore hhcp
    do
        [ `which $i | wc -w` -ne 1 ] && {
            echo This utility needs $i. See your system administrator.
            exit 1 ;}
    done
    hhalias uk.ac.ft-relay ftb
    sort -u -o $HOME/.hhalias $HOME/.hhalias
    echo 'Required utilities for hhg are available' > $HOME/.hhgok ;}

# Send data, including this file, to gawk script for analysis and action
{ echo `hhstore -l ftb | wc -l` ; echo $* ; echo $0; cat $0 ;} | gawk '

# Start of gawk script
# Detailed reference for awk/gawk programming
#     "The AWK Programming Language", Aho,Kernighan,and Weinberg,
#      Addison Wesley

# Record whether hhcp transfer parameters are present for ft-relay
FNR==1 { if ( $0 ~ /^1$/ ) hhparam = "absent"
                     else  hhparam = "present"
         next
       }

# Record the command parameters
FNR==2 {
    site=$1 ; subdir=$2 ; file=$3
    if ( file=="-b" ) { swch="-b" ; file=$4 }
    if ( subdir=="" || ( swch=="-b" && file=="" )) { help="yes"; next }
    next
    }

# Record pathname of hhg
FNR==3 {
    hhgpath=$1
    next
    }

# Process installation data
/^# Installation data #$/,/^# Installation end #$/ {
    msg0 = "Read " hhgpath " and install it as directed!"
    msg1 = "Check installation section of hhg "
    msg2 = ": e-mail address?"
    msg3 = ": retain/delete?"
    msg4 = ": plain/extended?"
    if ( ictr == 3 ) { ictr++ ; next }
    if ( ictr >  3 ) { print msg0 ; exit 1 }
    getline ; ictr++
        if (( NF > 1 )||( $0 !~ /@/ )) { print msg1 msg2 ; exit 1 }
        # if (( $0 ~ /bsrdp/) && ( $0 ~ /warwick/ )) { print msg0 ; exit 1 }
        address = $1
    getline ; ictr++ ;
        if (( NF > 1 )||(( $0 !~ /retain/ )&&( $0 !~ /delete/ )) ) {
            print msg1 msg3 ; exit 1 }
        hhaction = $1
    getline ; ictr++ ;
        if (( NF > 1 )||(( $0!~/plain/ )&&( $0 !~ /extended/ )) ) {
            print msg1 msg4 ; exit 1 }
        namestyle = $1
    }

# Process site/directory data
/^# Site data #$/,/^# Site end #$/ {
    if ( sctr==0 ) { sctr++ ; getline }

    # Show help data
    if ( help=="yes" ) {
        if ( $0=="# Site end #" ) {
            print "Example: hhg wuarchive . "
            print "Example: hhg wuarchive arc-lbr -b fv138.zip"
            exit 1
            }
        if ( helpctr==0 ) {
            print  "Get file or directory list for " address
            printf "         " namestyle " filenames, "
            print              hhaction  " hhcp parameters"
            print  "Own use: hhg site subdir"
            print  "         hhg site subdir [-b] file"
            print  "             |"
            helpctr++
            }
        path=pathname($3,$4,"subdir","file")
        printf("%+12s %-12s - get %s\n","",$1,path)
        next
        }

    # Site name not in data set
    if ( $0=="# Site end #" ) {
        print "Unknown site: " site
        exit 1
        }

    # Construct commands to call FT-RELAY, and execute
    if ( $1==site ) {
        "echo $HOME/hhgtmp1$$"   | getline tmp1
        "echo $HOME/hhgtmp2$$"   | getline tmp2
        # Instal  parameters for hhcp
        if ( hhparam=="absent" ) {
            print "hhstore ftb<<eof" > tmp1
            print "anonymous" > tmp1 ; #transfer authorisation
            print address     > tmp1 ; #transfer password
            print ""          > tmp1 ; #account name
            print ""          > tmp1 ; #account password
            print ""          > tmp1 ; #file password
            print ""          > tmp1 ; #output device type
            print ""          > tmp1 ; #output device type qualifier
            print ""          > tmp1 ; #mode of access
            print "8"         > tmp1 ; #binary word size
            print ""          > tmp1 ; #special options
            print ""          > tmp1 ;
            print "eof"       > tmp1 ;
            system("sh " tmp1 " > " tmp2 )
            system("rm " tmp1 )
            }
        # Construct hhcp command
        path=pathname($3,$4,subdir,file)
        loc=localname($1,subdir,file,namestyle)
        if ( file=="" )
            c=sprintf("hhcp -L ftb:\"%s::(D)%s\" %s\n",$2,path,loc)
        else
            c=sprintf("hhcp -L %s ftb:\"%s::%s\" %s\n",swch,$2,path,loc)
        # Execute hhcp command
        system(c)
        # If required, de-install parameters for hhcp
        if ( hhaction=="delete" ) {
            system("hhstore -r ftb > " tmp2 )
            system( " [ -f " tmp2 " ] && rm " tmp2 )
            }
        exit 0
        }
    }

END { exit 0 }

# Remote pathname constructor: returns full pathname at distant site
function pathname( os, basedir, subdir, file,     path ) {
    if ( os=="unix"   )  { path=basedir
        if ( subdir!="." ) path=path "/" subdir
        if ( file!="" )    path=path "/" file
        }
    if ( os=="tops20" )  { path=basedir
        if ( subdir!="." ) path=path "." subdir
        path=path ">"
        if ( file!="" )    path=path file
        }
    if ( os=="vms"    )  { path=basedir
        if ( subdir!="." ) path=path "." subdir
        path=path "]"
        if ( file!="" )    path=path file
        }
    if ( path=="" ) {
        print "Unknown operating system: " os
        exit 1
        }
    return path
    }

# Local pathname constructor
function localname( site, subdir, file, style,    local ) {
    if (( file=="" )||( style=="extended" )) {
        local=site
        gsub("/",".",subdir)
        if ( subdir!="." ) local=local "." subdir
        if ( file!="" )    local=local "." file
                      else local=local ".dir"
        }
    else local=file
    return local
    }
'
# End of gawk script

exit 0
# End of shell script

# ==========================================================================
# 3. Installation
# ===============
  Before first using this executable, use a text editor to edit the three
  data lines in this section:
    a) replace my email address with yours;
    b) retain/delete hhcp transfer parameters after each call;
    c) plain/extended versions of filenames - i.e. normal names or names
       prefixed with site and directory info - directories are always
       given extended names;
  Preserve the # ... # head and tail lines exactly as they are.

# Installation data #
  bsrdp@warwick.ac.uk
  retain
  extended
# Installation end #

# ==========================================================================
# 4. Remote Sites: Data and Base Directory Data
# =============================================
  Use a text editor to add/modify/delete data lines, following the
  required format for each line:
    a) your name for the combination of site and base directory (not yet
       adapted to getting root directories),
    b) the site address on Internet in mnemonic or numeric form,
    c) the style of pathname at the site - unix, tops20, vms
    d) a name stem for what you want to use as the base directory.
  Items need not be aligned vertically.
  Preserve the # ... # head and tail lines exactly as they are.

# Site data #
simtel20   wsmr-simtel20.army.mil       tops20  pd1:<msdos
simtel20u  wsmr-simtel20.army.mil       tops20  pd6:<unix-c
oak        oak.oakland.edu              unix    /pub/msdos
wuarchive  wuarchive.wustl.edu          unix    /mirrors/msdos
garbo      garbo.uwasa.fi               unix    /pc
garbou     128.214.87.1                 unix    /unix
nic        nic.funet.fi                 unix    /pub/msdos
somewhere  123.45.678.9                 vms     [directory
# Site end #

# ==========================================================================
# 5. History
# ==========
  Version 2.1
    Added installation alternatives, checks, and vms sites.
    Deleted requirement for separate installation of hhcp parameters.
  Version 2.0
    Rewrote to provide simple data structure.
  Version 1.0
    For files/directories from simtel20/unix sites.

# End of hhg, Version 2.1
