/*
 * Search.Rexx, a search function for Cd-Roms
 * that are configured for use with MegaCD © David Huckett 1995
 * This program can only be called by MegaCD and resides in the
 * same directory as MegaCD..
*/

/* Search.rexx v1.01   Xenolink 1.95 CD-Rom Door.
 *   By David Huckett  12-Sep-95 3:640/800 Fido
*/

FILEDIR   = 'Doors:MegaCD/'		/* Where this util is, Filelists   */
					/* are written and your header file*/
					/* is located..                    */
CDCFG     = 'Doors:MegaCD/CD.cfg'
SEARCHPATH= 'Doors:MegaCD/Search/'

/************** PLEASE DO NOT CHANGE ANYTHING BELOW HERE *************/

LHA = 'ED'x; DMS = 'E0'x; LZH = 'E1'x; TXT = 'E2'x; DOC = 'E3'x; MOD = 'E4'x
ZIP = 'E5'x; ZOO = 'E6'x; ARC = 'E7'x; EXE = 'E8'x; LZX = 'E9'x; ARJ = 'EA'x
GIF = 'EB'x; TIF = 'EC'x; BMP = 'EE'x; JPG = 'EF'x; PCX = 'F0'x

options results
PR = 'PRINT'

arg Node_Number CDName ConfigCD
host = 'XenolinkRexxPort'Node_Number
address value host
signal on error
signal on syntax
signal on IOERR

 if ~show('L','rexxsupport.library') then do
  if ~addlib('rexxsupport.library',0,-30,0) then do
    PR 'Support library not available.'
     call error
    signal Quit
   end
end
Allfiles = '0'
CALL PRAGMA('W','N')  /* Disk Requesters OFF */

MAIN:

wild = ''
pr 'Wildcards are allowed in the following formats only..:'
pr '*foo.lha , foo* , *foo* .. any others and file will not be found.'
pr
prompt 25 normal 'Enter the full name to search for: '
find1 = strip(result)
find = find1
if index(find,'*')= 0 then do;wild='NO';find =upper(find);end 
if index(find,'*')= 1 then do;wild ='F';find = substr(upper(find),2);end
if index(find,'*')> 1 then do;if wild ='F' then wild='FR';else wild ='R';ln=length(find)-1;find=upper(substr(find,1,ln));end
find2 = find
NoExt = 0
extens = right(find,4)
			SELECT

	When extens = '.LHA' then find = left(find,(length(find)-4))||upper(LHA)
	When extens = '.ZIP' then find = left(find,(length(find)-4))||upper(ZIP)
	When extens = '.DMS' then find = left(find,(length(find)-4))||upper(DMS)
	When extens = '.MOD' then find = left(find,(length(find)-4))||upper(MOD)
	When extens = '.LZH' then find = left(find,(length(find)-4))||upper(LZH)
	When extens = '.TXT' then find = left(find,(length(find)-4))||upper(TXT)
	When extens = '.EXE' then find = left(find,(length(find)-4))||upper(EXE)
	When extens = '.JPG' then find = left(find,(length(find)-4))||upper(JPG)
	When extens = '.GIF' then find = left(find,(length(find)-4))||upper(GIF)

			OTHERWISE NoExt = 1
end

IndTIT = CDName
If ~exists(SearchPath||IndTIT||'.dex') then do
pr
pr 'Cant find an Search index file for that CD-Rom...'
pr 'Ask the sysop to create one...'
msg 'Any key to continue...'
getchar
Exit 0
end
cls
pr
pr 'Searching for file : 'find2 'and 'find
if wild = 'NO' then do
if NoExt = 0 then find = left(find,(length(find)-1))
end
call open(fllist,SearchPath||IndTIT||'.dex','R')
	Findpath = Readln(fllist)
	do l =1 While ~EOF(fllist)
msg '.'
	findln.l = Readln(fllist)
	findln.l = Upper(findln.l)
	t= index(findln.l,find)
	if t ~= 0 then do


		Pr
		Pr 'Matching File Found...'
		msg 'Locating description..'
fnd = 0
configCD = strip(configCD)
		call open(ccfg,configCD, 'R')
		cdpathname  = readln(ccfg)
		indexpath   = readln(ccfg)
		displayfile = readln(ccfg)
		skipstart   = readln(ccfg)

		do j = 1 to value(word(findln.l,1))
		directory    = word(readln(ccfg),1)
		end
		call close(ccfg)
txtfile = Indexpath||directory||displayfile
call open(list,txtfile,'R')
	do g = 1 to skipstart
	junk.g = readln(list)
	end
	do g =1 While ~EOF(list)
msg '.'
	filname = Readln(list)
	if index(upper(word(filname,1)),find2) ~=0 then do
		fulfile = word(filname,1)
		descp   = subword(filname,2)
		call close(list)
		fnd = 1
		leave g		
		end
	end
if fnd ~= 1 then do
		descp = 'No description found'
		fulfile = find2
		call close(list)
	end

		pr
		pr 'Found :'left(fulfile,20)||descp
		pr
		QueryYN 'Do you wish to mark file for download? '
		reply = upper(result)
		if reply = 'Y' then do;call close(fllist);signal Download;end
		if reply = 'N' then do
			QueryNY 'Continue search? '
			reply = upper(result)
			if reply = 'N' then do;call close(fllist);Exit 0;end
			if reply = 'Y' then NOP
			end
		end
end

call close(fllist)
pr
pr 'File not found..'
getchar
Exit 0


DOWNLOAD:
pr
Pr 'Adding 'fulfile' to download list...'
call delay 100
filepaths = findpath||directory
MarkedFile = fulfile
if ~exists('T:MRXDL'node_Number'.sfl') Then do
	call open(tempdl,'T:MRXDL'node_Number'.sfl','W')
	Call WriteLn(Tempdl,filepaths word(MarkedFile,1))
	Call Close(Tempdl)
end
else do
	Call open(Tempdl,'T:MRXDL'node_Number'.sfl','A')
	Call WriteLN(tempdl,filepaths word(MarkedFile,1))
	Call Close(tempdl)
end


Exit 0



QUIT:
Exit 0

error:
a = SIGL;message 'Error!!!  Line # 'a;call delay 200
EXIT 10

syntax:
a = SIGL;message 'Syntax Error!!!  Line # 'a;call delay 200
EXIT 10

ioerr:
message 'I/O Error!!!';call delay 200
EXIT 10
