/*rx*/
/*
 * $VER: GUI-Archie 0.97 (20/03/95)
 *
 * Problems? Email: phil@curve.demon.co.uk
 *
 * Requirements: ARexx running, RexxReqTools installed, Archie 38.1
 *               FTPGet, an AmigaGuide viewer.
 *
 * Usage: GUI-Archie [HIRES=HR]
 *
 * Use the HIRES switch if you are running a HiRes Workbench (it'll
 * make the buttons fit, that's all)
 *
 * ToDo: change localdir
 *
 *
 *
*/

                   /**************************************/
/******************** Change these defaults, if you want *******************/
                   /**************************************/

HOSTNAME = 'archie.doc.ic.ac.uk'           /* default Archie host          */
CLIENTNAME = 'inet:bin/archie'             /* path of Archie client        */
VIEWER = 'dh0:utilities/multiview'         /* path of AmigaGuide viewer    */
FTP_CMD = 'inet:bin/ftpget'                /* path of FTPGet command       */

MAXHITS = '30'                             /* default maximum hits value   */

                           /********************/
/**************************** Leave the rest!! *****************************/
                           /******(please)******/

/* var setup */

parse arg arguments
if (find(upper(arguments), "HIRES")=1)|(find(upper(arguments), "HR")=1) then HIRES = 1
if HIRES ~=1 then HIRES = 0
if (find(upper(arguments), "?")=1)|(find(upper(arguments), "-?")=1) then HELP = 1

if exists("libs:rexxreqtools.library")>0 then do
   addlib('rexxreqtools.library',0,-30)
end
if (exists("libs:rexxreqtools.library")=0) then do
   say 'RexxReqTools is not installed! Install it and try again.'
   exit
end

if HELP = 1 then call proc_help()

VIEWER_PATH = 'dh0:utilities/'

TEMPIN = "t:gui-archie_in.tmp"
TEMPOUT = "t:gui-archie_out.tmp"
TEMPFLOW = "t:gui-archie_overflow.tmp"
TEMPREAD = "t:GUI-Archie_Textfile.tmp"
DIRLIST = "t:gui-archie_dirlist.tmp"
CHECKFILE = "t:gui-archie_check.tmp"
RXGET = "t:gui-archie_get.rexx"

SEARCHSTRING = ''
SEARCHSTRING_ARG = 'Not Defined'

ARCHIE_VER = 'Archie 38.1'
PROGNAME = 'GUI-Archie 0.97'
TAGS = 'rt_reqpos=reqpos_centerwin'

signal on syntax
signal on break_c

address command 'assign exists inet: >' CHECKFILE
CHECK = open('checkami',CHECKFILE, read)
CHECKASS = readln('checkami')
CHECKAMI = left(CHECKASS, 5)
CHECK = close('checkami')
address command 'delete >NIL:' CHECKFILE
if (CHECKAMI = 'inet:') then do
   call rtezrequest('AmiTCP/IP version 2 or later must be started first.','_Exit' PROGNAME,PROGNAME,TAGS,)
   exit
end

/* check exist */
call exister(CLIENTNAME)
call exister(VIEWER)
call exister(FTP_CMD)

NL = '0A'x

CASE_STAT = '_Case Insensitive'
MATCH_STAT = '_Match SubString'

CASE_ARG = '-s'
EXACT_ARG = ''
MAXHITS_ARG = '-m' MAXHITS

say PROGNAME
address command CLIENTNAME '-v'

/* main program */

proc_main:

do while MAIN_ANS ~= 0
   call update_main()
   call rtezrequest(MAIN_STATUS,'_Begin Search|_Enter Search String|_Change Host|_Options|E_xit',PROGNAME,TAGS,MAIN_ANS)
   if MAIN_ANS = 0 then call proc_end()
   if MAIN_ANS = 1 then call begin_search()
   if MAIN_ANS = 2 then call enter_string()
   if MAIN_ANS = 3 then call change_host()
   if MAIN_ANS = 4 then call options()
   drop MAIN_ANS
end

call proc_end()

exit

/* logical prog end */

/* search proc */

begin_search:

if SEARCHSTRING = "" then do
   NOSEARCH = rtezrequest('No search string defined','_Define One Then!|_Cancel Search',PROGNAME,TAGS,,)
   if NOSEARCH = 1 then
      call enter_string()
   else
      return
end
if SEARCHSTRING = "" then return

trace off
say 'Archie searching.  Please wait...'
address command CLIENTNAME '-o' TEMPIN  '-l -t -h' HOSTNAME CASE_ARG EXACT_ARG MAXHITS_ARG SEARCHSTRING
call archie_err()

call proc_get()
return

/* enter string proc */

enter_string:

OLDSEARCH = SEARCHSTRING
NEWSEARCH = rtgetstring(SEARCHSTRING,'Enter string to search for..',PROGNAME,'_OK|_Cancel',TAGS,)

if NEWSEARCH = "" then do
   SEARCHSTRING = OLDSEARCH
   return
end
SEARCHSTRING = NEWSEARCH
SEARCHSTRING_ARG = SEARCHSTRING
return

/* change host proc */

change_host:

OLDHOST = HOSTNAME
NEWHOST = rtgetstring(HOSTNAME,'Enter a new hostname..',PROGNAME,'_Accept|_Cancel',TAGS,)

if NEWHOST = "" then do
   HOSTNAME = OLDHOST
   return
end
HOSTNAME = NEWHOST
return

/* options proc */

options:

do while OPT_ANS ~= 0
   if HIRES = 1 then
      MAXRES = 'Max'
   else
      MAXRES = 'Alter Maximum'
   call rtezrequest('Click on an option to change it..',CASE_STAT'|'MATCH_STAT'|'MAXRES '_Hits ('MAXHITS')|Change _Viewer|_OK',PROGNAME,TAGS 'rtez_defaultresponse = 0',OPT_ANS)
   if OPT_ANS = 0 then do
      drop OPT_ANS
      return
   end
   if OPT_ANS = 1 then do
      if EXACT_ARG = '' then do
         if CASE_ARG = '-s' then do
            NEWCASE_STAT = '_Case Sensitive'
            NEWCASE_ARG = '-c'
         end
         if CASE_ARG = '-c' then do
            NEWCASE_STAT = '_Case Insensitive'
            NEWCASE_ARG = '-s'
         end
         CASE_STAT = NEWCASE_STAT
         CASE_ARG = NEWCASE_ARG
      end
   end
   if OPT_ANS = 2 then do
      if EXACT_ARG = '' then do
         NEWMATCH_STAT = '_Match Exact Word'
         NEWEXACT_ARG = '-e'
         CASE_STAT = 'Case Sensitive'
         CASE_ARG = '-c'
      end
      if EXACT_ARG = '-e' then do
         NEWMATCH_STAT = '_Match SubString'
         NEWEXACT_ARG = ''
         CASE_STAT = '_Case Insensitive'
         CASE_ARG = '-s'
      end
      MATCH_STAT = NEWMATCH_STAT
      EXACT_ARG = NEWEXACT_ARG
   end
   if OPT_ANS = 3 then do
      OLDMAXHITS = MAXHITS
      NEWMAXHITS = rtgetlong(MAXHITS,'Enter new ''maximum hits'' value..',PROGNAME,'_Accept|_Cancel',TAGS,)
      if NEWMAXHITS = "" then
         MAXHITS = OLDMAXHITS
      else
         MAXHITS = NEWMAXHITS
      MAXHITS_ARG = '-m' MAXHITS
   end
   if OPT_ANS = 4 then do
      OLDVIEWER = VIEWER
      NEWVIEWER = rtfilerequest(VIEWER_PATH,,'Choose an AmigaGuide viewer..',,TAGS 'rtfi_height=300',,)
      if NEWVIEWER = "" then
         VIEWER = OLDVIEWER
      else
         VIEWER = NEWVIEWER
   end
   drop OPT_ANS
end
return

/* main window update */

update_main:

if CASE_ARG = '-s' then MAINCASE_STAT = 'Insensitive'
if CASE_ARG = '-c' then MAINCASE_STAT = 'Sensitive'
if EXACT_ARG = '' then MAINEXACT_STAT = 'SubString'
if EXACT_ARG = '-e' then MAINEXACT_STAT = 'Exact Word'

MAIN_STATUS = ARCHIE_VER NL NL'Current SearchString:  ' SEARCHSTRING_ARG NL'Host:  ' HOSTNAME NL'Match' MAINEXACT_STAT NL'Case' MAINCASE_STAT NL'Maximum Hits:  ' MAXHITS NL'Viewer:  ' VIEWER
return

/** convert output **/

/* main conversion proc */

proc_get:

address command 'echo NOLINE "Post-processing..."'

INOP = open('in',TEMPIN, read)
OUTOP = open('out',TEMPOUT, write)
FLOWOP = open('overflow',TEMPFLOW,write)
DIROP = open('dirlist',DIRLIST,write)
RXGETOP = open('rxget',RXGET,write)

COUNT = 0
LCOUNT = 0
DCOUNT = 0

SETUP_LINE = writeln('out','@database GUI-Archie'NL'@node main'NL'@title "GUI-Archie Output (Click & Get Page)"'NL NL' @{b}Click on a file @{"" LINK main} to retrieve it@{ub}'NL' @{b}Click on a ASCII file @{"+" LINK main} to read it@{ub}'NL)
SETUP_FLOW = writeln('overflow','@database GUI-Archie Overflow'NL'@node main'NL'@title "GUI-Archie Output (Long lines)"'NL NL' @{b}@{u}Matches too long for click & get interface@{ub}@{uu}'NL)
SETUP_DIR = writeln('dirlist','@database GUI-Archie Directories'NL'@node main'NL'@title "GUI-Archie Output (Directories)"'NL NL' @{b}@{u}Directories Matched@{ub}@{uu}'NL)

SETUP_RXGET = writeln('rxget','/*rx*/'NL'parse arg arguments'NL'trace off'NL'NL=''0A''x'NL'S=word(arguments,1)'NL'F=word(arguments,2)'NL'R=word(arguments,3)'NL'say ''Attempting FTP connection to'' S''...'''NL'if R~=''R'' then do')
SETUP_RXGET2 = writeln('rxget','address command '''FTP_CMD''' S F 'NL'call e'NL'exit'NL'end'NL'if R=''R'' then do'NL'address command '''FTP_CMD''' S F ''local' TEMPREAD''''NL'call e'NL'end')
SETUP_RXGETR = writeln('rxget','address command '''VIEWER TEMPREAD''''NL'exit')
SETUP_RXGETE = writeln('rxget','e:'NL'if RC=0 then say ''Transfer successful'''NL'if RC=21 then say ''General error - email phil@curve.demon.co.uk with details.''')
SETUP_RXGETE1 = writeln('rxget','if RC=22 then say ''Password argument unacceptable - email phil@curve.demon.co.uk with details.'''NL'if RC=23 then say ''Could not open bsdsocket.library''')
SETUP_RXGETE2 = writeln('rxget','if RC=24 then say ''Remote site does not exist or cannot be resolved.'''NL'if RC=25 then say ''Remote server not responding.'''NL'if RC>25 then do'NL'if RC<29 then do'NL'say ''Anonymous login invalid.'''NL'end'NL'end')
SETUP_RXGETE3 = writeln('rxget','if RC=29 then say ''The remote file no longer exists!'''NL'if RC=30 then say ''The local file cannot be written.'''NL'if RC=31 then say ''User aborted transfer.'''NL'if RC>0 then exit'NL'return')
RXGETCLOSE = close('rxget')

do while eof('in')=0
   INLINE = readln('in')
   if COUNT = 0 then do
      if INLINE = "" then do
         call rtezrequest('No matches/Connection Error','_Huh!',PROGNAME,TAGS,)
         INCLOSE = close('in')
         OUTCLOSE = close('out')
         FLOWCLOSE = close('overflow')
         DIRCLOSE = close('dirlist')
         say 'done.'
         address command 'delete >NIL:' TEMPIN TEMPOUT TEMPFLOW DIRLIST TEMPREAD
         return
      end
   end
   if INLINE ~= "" then do
      call proc_line()
      if OUTLINE = LONG then do
         LCOUNT = LCOUNT + 1
         OUTFLOW = writeln('overflow',OVERFLOW)
      end
      if OUTLINE = DIR then do
         DCOUNT = DCOUNT + 1
         OUTDIR = writeln('dirlist',DIRECT)
      end
      if OUTLINE ~= LONG & OUTLINE ~= DIR then do
         OUTWRITE = writeln('out',OUTLINE)
      end
   end
   COUNT = COUNT + 1
end

if DCOUNT >0 then do
   writeln('out',NL' @{" Directories -' DCOUNT '" LINK' DIRLIST'/main}')
end

if LCOUNT >0 then do
   writeln('out',NL' @{" Long lines -' LCOUNT '" LINK' TEMPFLOW'/main}')
end

SETDOWN_LINE = writeln('out','@endnode')
SETFLOW_LINE = writeln('overflow',NL NL' @{" Back to ''Click & Get'' Page " LINK' TEMPOUT'/main}'NL'@endnode')
SETDIR_LINE = writeln('dirlist',NL NL' @{" Back to ''Click & Get'' Page " LINK' TEMPOUT'/main}'NL'@endnode')

INCLOSE = close('in')
OUTCLOSE = close('out')
FLOWCLOSE = close('overflow')
DIRCLOSE = close('dirlist')

say 'done.'

trace off
address command VIEWER TEMPOUT
call viewer_err()

address command "delete >NIL:" TEMPIN TEMPOUT TEMPFLOW DIRLIST RXGET

return

/* single line proc engine */

proc_line:

DATE = (left(INLINE,8))
TIME = (substr(word(INLINE, 1),9,6))

YEAR = (substr(DATE,3,2))
MONTH = (substr(DATE,5,2))
DAY = (right(DATE,2))

HOUR = (left(TIME,2))
MINS = (substr(TIME,2,2))
SECS = (right(TIME,2))

BSIZE = (word(INLINE,2))

numeric digits 4
SIZE = BSIZE / 1000

SITE = (word(INLINE,3))

FILE = (word(INLINE,4))

if (upper(right(FILE, 7))= ".README") then READ = 1
if (upper(right(FILE, 4))= ".TXT") then READ = 1
if (upper(right(FILE, 4))= ".DOC") then READ = 1
if (upper(right(FILE, 4))= ".REA") then READ = 1
if READ ~= 1 then READ = 0

if READ = 0 then do
   OUTLINE = '@{b}'DAY'/'MONTH'/'YEAR '@{ub}@{" 'SITE':'FILE'" RX "'RXGET SITE FILE'"} @{b}'SIZE'k@{ub}'
end

if READ = 1 then do
   OUTLINE = '@{b}'DAY'/'MONTH'/'YEAR '@{ub}@{"+ 'SITE':'FILE'" RX "'RXGET SITE FILE R'"} @{b}'SIZE'k@{ub}'
end

drop READ

if right(FILE,1) = "/" then do
   DIRECT = '@{b}'DAY'/'MONTH'/'YEAR '@{ub}'SITE':'FILE
   OUTLINE = DIR
end

if length(OUTLINE) >256 then do
   OVERFLOW = SITE':'FILE '@{b}'SIZE'k ('DAY'/'MONTH'/'YEAR')@{ub}'
   OUTLINE = LONG
end

return

/* existential checks, man */

exister:
arg CMD

if exists(CMD)=0 then do
   say CMD 'not found.'
   say 'Exiting.'
   call proc_end()
end
return

/* break/error handling routines */

archie_err:

if RC = 20 then
   say 'Archie abort.'
else
   say 'Archie returned.'
trace normal
drop RC
return

viewer_err:

if RC = 20 then do
   say 'Viewer error - check viewer can handle AmigaGuide files.'
end
trace normal
return

BREAK_C:

say 'Ctrl-C detected. Aborting..'
return

SYNTAX:

say 'General error.'
return

/** end / delete proc **/

proc_end:

address command 'delete >NIL:' TEMPIN TEMPOUT TEMPFLOW DIRLIST RXGET TEMPREAD
say PROGNAME 'out.'
exit

/** usage **/

proc_help:

say 'Usage: GUI-Archie [HIRES=HR]'
say 'Use the HIRES switch if you are running a HiRes Workbench.'
exit
