/*************************************************/
/*                                               */
/*  MegaCDLister.rexx v1.04 © David Huckett..    */
/*    Xenolink 1.95 CD-Rom Door.                 */
/*   By David Huckett  12-Aug-95 3:640/800 Fido  */
/*************************************************/

/*
 *  Short instructions and doc..
 *
 *  Simply use RX MCDList from the CLI once you are in the current
 *  directory that this prog is in, or if you have it in your REXX:
 *  assign, you can use RX MCDList from any CLI window..
 *
 *  Make a text file called MCD.Header and place it in your MegaCD
 *  directory..  MCDLister uses this header for info in the top
 *  of your filelists, or in an about button in Amigaguide..
 *  Check out the example..
 *  Alternativly just double click on the MCDList Icon..
 *
*/

/*
 *  8-Jul-95   Started to add the index function to this to create Index
 *              files to allow searches and quick find using the offline
 *              request door..
 *
 *  6-Aug-95   Changed over to CLI version of this program
 *
 *
 * 12-Aug-95   Made it also have the option to create the list in
 *              AmigaGuide© format..
 *
 * 14-Sep-95   Finished off and testing for release
*/
           
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/'      /* This is where the search index  */
BBSNAME   = 'MegaTech BBS'              /* files are written               */
DATAPHONE = '(077)-835397'
SYSOP   = 'David Huckett'

/*** NO FURTHER USER ADJUSTABLE VALUES 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
CR = D2C('13');LF = D2C('10');CRLF = CR || LF;minbyte = 200000

if storage() < minbyte then do
   pr 'Not enough memory to run MCDlist safely!'
   signal QUIT
end

signal on error;signal on syntax;signal on IOERR;signal on BREAK_C
 if ~show('L','rexxsupport.library') then do
  if ~addlib('rexxsupport.library',0,-30,0) then do
    say 'Support library not available.'
     call error
    signal Quit
   end
end

Allfiles = '0';CALL PRAGMA('W','N')

START:
say ''
tit1 = '                  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+'
tit2 = '                  +  MegaCD Xenolink1.97 CD-Rom Door  +'
tit3 = '                  +        File list creator          +'
tit4 = '                  +          version 1.04             +'
tit5 = '                  +    by David Huckett 14-Sep-95..   +'
tit6 = '                  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+'

say tit1;say tit2;say tit3;say tit4;say tit5;say tit6;say ''

if ~exists(CDcfg) then do
	say ' You do not have a config file!'
	call ERROR
	end
else do
	Call open(cfg,CDcfg,'R')
		numcd=readln(cfg)
		do i=1 to numcd
	lock.i=readln(cfg)
	title.i=readln(cfg)
	config.i=readln(cfg)
	desc.i=readln(cfg)
	junk=readln(cfg)
	junk=readln(cfg)
			if  ~Exists(lock.i) then do
			title.i='Not Online'
			iterate i
			end
	say left(i'.',4)||left(title.i,15)||left('LOADED',8)||left(desc.i,45)
	end
	call close(cfg)
END


MAIN:
  INDEX = 'FALSE'
  say ''
  x=writech(stdout," [Select # <Q>uit Ctrl-C to abort] > ")
  pull cmd
  cmd=strip(cmd)
  if left(cmd,1) = 'Q' then signal QUIT
  if datatype(cmd,'NUMERIC')~=1 then signal main
  say ''

  x=writech(stdout, '(I)bm (A)miga or Inde(X) CD? > ')
  pull answ
  answ = strip(answ)

    if answ = '' then signal MAIN
    if answ = 'X' then do INDEX = 'TRUE';signal indexing;end
    if answ = 'I' then do type='I';ELN =D2C('13');signal next;end
    if answ = 'A' then do type='A';ELN = '';signal guide;end
    if left(answ,1)='Q' then signal quit
    say 'Unkown Command 'answ
signal main

GUIDE:
  Aguide = 0
  x = writech(stdout, 'Do you want AmigaGuide list? Y/n ')
  pull gui
  gui = strip(gui)
    if gui = '' then do;Aguide= 'YES';signal next;end
    if gui = 'Y' then do;Aguide= 'YES';signal next;end
    if gui = 'N' then do;Aguide= 'NO';signal next;end
signal Guide


Next:
if cmd='' then signal main
if value(cmd)<=numcd then do
		if title.cmd = 'Not Online' then do
			say ''
			say ' Volume not currently loaded.. Load and try again.'
			call delay 100
			signal start
		end
		else do
			if Aguide = 'YES' then signal MAKEGUIDE
			else signal Selected
		end
	end
if left(cmd,1)='Q' then signal quit
say 'Unkown Command 'cmd
signal main


SELECTED:

say ''
say '          Creating filelist for 'title.cmd
say ''

if ~exists(config.cmd) then do
say ' config file: 'config.cmd 'not found. Please check path or create one.'
call delay 100
signal start
end

call open(fllist,filedir||title.cmd,'W')
call writeLN(fllist,tit1||ELN)
call writeLN(fllist,tit2||ELN)
call writeLN(fllist,tit3||ELN)
call writeLN(fllist,tit4||ELN)
call writeLN(fllist,tit5||ELN)
call writeLN(fllist,tit6||ELN)
call writeLN(fllist,''ELN)

		if exists(filedir'Mcd.header') then do
			call open(header,filedir'Mcd.header','R')
			do while ~EOF(header)
			banner = readln(header)
			call writeln(fllist,banner||ELN)
			end
			call close(header)
		END

call writeLN(fllist,' Complete list of files for:   ---===<< 'title.cmd' >>===---'ELN)
call writeLN(fllist,' Available at 'BBSNAME DATAPHONE||ELN)
call writeLN(fllist,' Sysop: 'SYSOP||ELN)
call writeLN(fllist,''ELN)
call writeLN(fllist,' Filelist created on: 'Date()ELN)
call writeLN(fllist,''ELN)

say ''
call open(cdcfg,config.cmd, 'R')
cdpath      = readln(cdcfg)
indexpath   = readln(cdcfg)
displayfile = readln(cdcfg)
skipstart   = readln(cdcfg)
totdir = 0
allfiles = 0
do j = 1 while ~EOF(cdcfg)
dirs.j    = readln(cdcfg)
if dirs.j = '' then iterate j
totdir = totdir+1
say '[1A Dir #:'j dirs.j'[K'
Call writeLN(fllist,' Dir #:'j dirs.j||ELN)
Call writeLN(fllist,left('-',70,'-')ELN)
file = Indexpath||word(dirs.j,1)||displayfile
If ~Exists(FILE) then do
	say ' Index file not found..'file
	say ''
        call delay 200
	iterate j
end
	else do

	Call Open(disp, FILE, 'R')
	f = 0
	do h = 1 to skipstart
	junk.h = readln(disp)
	end
	do k =1 While ~EOF(disp)
	fl.k = Readln(disp)
	if word(fl.k,1) == D2C(26) then Iterate k
	if word(fl.k,1) == '' then iterate k
	if word(fl.k,1) == '|' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		Call writeLN(fllist,Fl.k,73||ELN)
		k = K-1
	        END
		iterate k
	end
else do
	if substr(fl.k,1,1) == ' ' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		Call writeLN(fllist,left('',11)||Fl.k||ELN)
		k = K-1
	        END
		iterate k
	end
end
if substr(fl.k,1) ~= ' ' then do
f=f+1
		if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
		if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
		if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
	Call writeLN(fllist,Left(Word(Fl.k,1),24)Subword(left(Fl.k,73),2)||ELN)
	end
end
call writeLN(fllist,left('=',70,'=')ELN)
call writeLN(fllist,' Total files in 'dirs.j':' f||ELN)
call writeLN(fllist,''ELN)
Call writeLN(fllist,''ELN)
call close(disp)
allfiles = allfiles + f
end

end
call writeLN(fllist,'Total Directories: 'totdir'   Total Files: 'Allfiles||ELN)

call close(fllist)
call close(cdcfg)
say ''
say 'Total Directories: 'totdir'   Total Files: 'Allfiles
say ''
say ' Complete.........'
say ''
x=writech(stdout, 'Hit any key...')
pull char
SIGNAL START



MAKEGUIDE:

say ''
say '          Creating AmigaGuide list for 'title.cmd
say ''

if ~exists(config.cmd) then do
say ' config file: 'config.cmd 'not found. Please check path or create one.'
call delay 100
signal start
end
listname = space(title.cmd,0)'.guide'
call open(fllist,filedir||listname,'W')

call writeLN(fllist,'@database MegaCD')
call writeLN(fllist,'@master 'listname)
call writeLN(fllist,'@index Main')
call writeLN(fllist,'')
call writeLN(fllist,'@node "Main" "MegaCD ©1995 Filelist creator by D.Huckett"')
call writeLN(fllist,'MegaCD CD-Rom Door Filelist creator Version 1.03')
call writeLN(fllist,'              By David Huckett 3:640/800@Fidonet')
call writeLN(fllist,'')
call writeLN(fllist,' Complete list of files for:   ---===<< 'title.cmd' >>===---')
call writeLN(fllist,' Available at 'BBSNAME DATAPHONE)
call writeLN(fllist,' Sysop: 'SYSOP)
call writeLN(fllist,' Filelist created on: 'Date()'   @{"  Volume Stats  " link "stats" 0}')
call writeLN(fllist,'')

nodetot=0
		if exists(filedir'Mcd.header') then do
		nodetot = nodetot + 1
		node.nodetot = '@node "About"'
		call writeLN(fllist,'                      @{"'center('About 'BBSNAME,30)'" link "About" 0}')

		end



call open(cdcfg,config.cmd, 'R')
cdpath      = readln(cdcfg)
indexpath   = readln(cdcfg)
displayfile = readln(cdcfg)
skipstart   = readln(cdcfg)
totdir = 0
allfiles = 0
twin = 0
do j = 1 while ~EOF(cdcfg)
dirs.j    = readln(cdcfg)
if dirs.j = '' then iterate j
nme = center(subword(dirs.j,2),30)
totdir = totdir+1
twin = twin + 1
	IF twin=1 then call writech(fllist,'    @{"'nme'" link "dir'j'" 0}')
	IF twin=2 Then do
		call writech(fllist,'     @{"'nme'" link "dir'j'" 0}'LF)
		twin = 0
	end
end
call writeLN(fllist,'')
call writeLN(fllist,'@endnode')
call close(cdcfg)

		if exists(filedir'Mcd.header') then do

			call writeln(fllist,node.1)
			call open(header,filedir'Mcd.header','R')
			do while ~EOF(header)
			banner = readln(header)
			call writeln(fllist,banner)
			end
			call close(header)
			call writeln(fllist,'@endnode')
			call writeln(fllist,'')

		END
 
say ''
call open(cdcfg,config.cmd, 'R')
junk      = readln(cdcfg)
junk   = readln(cdcfg)
junk = readln(cdcfg)
junk   = readln(cdcfg)
totdir = 0
allfiles = 0
do j = 1 while ~EOF(cdcfg)
dirs.j    = readln(cdcfg)
if dirs.j = '' then iterate j
totdir = totdir+1
say ' Dir #:'j dirs.j'[K[1A'
call writeln(fllist,'@node "dir'j'"')
Call writeLN(fllist,' Dir #:'j dirs.j)
Call writeLN(fllist,left('-',70,'-'))
file = Indexpath||word(dirs.j,1)||displayfile
If ~Exists(FILE) then do
	say ' Index file not found..'file
	say ''
        call delay 200
	iterate j
end
	else do

	Call Open(disp, FILE, 'R')
	f = 0
	do h = 1 to skipstart
	junk.h = readln(disp)
	end
	do k =1 While ~EOF(disp)
	fl.k = Readln(disp)
	if word(fl.k,1) == D2C(26) then Iterate k
	if word(fl.k,1) == '' then iterate k
	if word(fl.k,1) == '|' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		Call writeLN(fllist,Fl.k,73)
		k = K-1
	        END
		iterate k
	end
else do
	if substr(fl.k,1,1) == ' ' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		Call writeLN(fllist,left('',11)||Fl.k)
		k = K-1
	        END
		iterate k
	end
end
if substr(fl.k,1) ~= ' ' then do
f=f+1
		if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
		if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
		if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
	Call writeLN(fllist,Left(Word(Fl.k,1),24)Subword(left(Fl.k,73),2))
	end
end
call writeLN(fllist,left('=',70,'='))
call writeLN(fllist,' Total files in 'dirs.j':' f)
call writeLN(fllist,'')
Call writeLN(fllist,'')
call close(disp)
allfiles = allfiles + f
call writeln(fllist,'@endnode')
Call writeLN(fllist,'')

end

end
call writeln(fllist,'@node "stats"')
Call writeLN(fllist,'')
Call writeLN(fllist,'')
call writeLN(fllist,'   MegaCD Statistics for CD/Volume/Area : 'title.cmd)
Call writeLN(fllist,'')
Call writeLN(fllist,'            Total Directories: 'totdir)
Call writeLN(fllist,'                  Total Files: 'allfiles)
Call writeLN(fllist,'')
Call writeLN(fllist,'Thank you for using MegaCDList, and Supporting Shareware.')
call writeln(fllist,'@endnode')
Call writeLN(fllist,'')

call close(fllist)
call close(cdcfg)
say ''
say 'Total Directories: 'totdir'   Total Files: 'Allfiles
say ''
say ' Complete.........'
say ''
x=writech(stdout, 'Hit any key...')
pull char
SIGNAL START


INDEXING:
IndTIT = SPACE(Title.cmd,1,'_')
if exists(SearchPath||IndTIT||'.dex') then do
say ''
x=writech(stdout, 'Index file for this CD already exists.. Overwrite? (N/y) ')
pull over
over = strip(over)
if over = 'N' then signal start
if over = 'Y' then signal goon
say 'Answer with Y or N..'
signal indexing
end

GOON:
say ''
say '          Creating Search Index for 'title.cmd
say ''

if ~exists(config.cmd) then do
say ' config file: 'config.cmd 'not found. Please check path or create one.'
call delay 100
signal start
end
call open(fllist,SearchPath||IndTIT||'.dex','W')
say ''
call open(cdcfg,config.cmd, 'R')
cdpath      = readln(cdcfg)
indexpath   = readln(cdcfg)
displayfile = readln(cdcfg)
skipstart   = readln(cdcfg)
lines = 1
words = 0
totdir = 0
fullline.lines = ''
allfiles = 0
ttl = 0
call writeln(fllist,strip(cdpath))
do j = 1 while ~EOF(cdcfg)
	dirs.j    = readln(cdcfg)
	if dirs.j = '' then iterate j
	totdir = totdir+1
	say '[4;1HIndexing Dir #:'j dirs.j'[K'
	file = Indexpath||word(dirs.j,1)||displayfile
	If ~Exists(FILE) then do
		say ' Index file not found..'file
		say ''
        	call delay 200
		iterate j
	end
	else do

	Call Open(disp, FILE, 'R')
	f = 0
	do h = 1 to skipstart
	junk.h = readln(disp)
	end
	do k =1 While ~EOF(disp)
	fl.k = Readln(disp)
	if word(fl.k,1) == D2C(26) then Iterate k
	if word(fl.k,1) == '' then iterate k
	if word(fl.k,1) == '|' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		k = K-1
	        END
		iterate k
		end

	if substr(fl.k,1,1) == ' ' then do
		if k < 1 then Iterate k
		else do
			if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
			if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
			if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)
		k = K-1
	        END
		iterate k
	end




if substr(fl.k,1) ~= ' ' then do
	f=f+1
		if right(Fl.k,1) = LF Then FL.k = Strip(FL.k,'T',LF)
		if right(Fl.k,1) = CR Then FL.k = Strip(FL.k,'T',CR)
		if right(Fl.k,1) = CRLF Then FL.k = Strip(FL.k,'T',CRLF)

	word = strip(upper(Word(Fl.k,1)))
	extens = upper(right(word,4))
				SELECT
					When extens = '.LHA' then word = left(word,(length(word)-4))||LHA
					When extens = '.ZIP' then word = left(word,(length(word)-4))||ZIP
					When extens = '.DMS' then word = left(word,(length(word)-4))||DMS
					When extens = '.MOD' then word = left(word,(length(word)-4))||MOD
					When extens = '.LZH' then word = left(word,(length(word)-4))||LZH
					When extens = '.TXT' then word = left(word,(length(word)-4))||TXT
					When extens = '.EXE' then word = left(word,(length(word)-4))||EXE
					When extens = '.JPG' then word = left(word,(length(word)-4))||JPG
					When extens = '.GIF' then word = left(word,(length(word)-4))||GIF


				OTHERWISE NOP
end
ttl = ttl + 1
say '[6;6HFile #:'ttl'[K'

	words = words + 1
	if words//100 = 0 then do;lines = lines +1;fullline.lines ='';end

	fullline.lines  = Fullline.lines||word
	end
end
call close(disp)
allfiles = allfiles + f

		do l = 1 to lines
			Call writeLN(fllist,j Fullline.l)
		FullLine.l = ''
		end
lines = 1
words = 0
end
end

call close(fllist)
call close(cdcfg)
say ''
say 'Total Directories: 'totdir'   Total Files: 'Allfiles
say ''
say ' Complete.........'
say ''
x=writech(stdout, 'Hit any key...')
pull key
SIGNAL START


BREAK_C:
call close(cfg)
call close(header)
call close(disp)
call close(fllist)
call close(cdcfg)
SIGNAL START


QUIT:
Exit 0

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

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

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