/*
 $VER:  CDS_Setup.rexx v1.0a © (25.12.98) by Craig Hutchison.
*/

options results

Call NewSTDERR('Mail:Logs/Trace/CDS_Setup.rexx.trc')
/*trace r*/

/*              Takes ONE argument: "Disk"                  */

/* Being the VOLUME name of the Aminet Disk you're reading  */
/* from. Builds a complete directory structure and writes a */
/* Files.BBS file for each area, as well as generating an   */
/*            "Areas.cfg" file for FList.                   */

/* Also needs an up-to-date "TREE" file in the desination   */
/* directory (the one on Aminets CDs is usually outdated)   */


Parse Arg Disk

If Disk = '' then Call Usage

/***** Change these to suit your setup *****/

Dest = 'BBS:File/CD/'    /* Where you keep all ya CD configs for CDScroller */
RdMe = 'N'               /* Add the .readme for each file to the Files.BBS  */

/*******************************************/
Call Time('R')
$VER =  'CDS_Setup.rexx v1.0a © (25.12.98) by Craig Hutchison'
Config = Disk||':AMINET/INDEX'

If ~Show('L',"rexxsupport.library") then
	If ~Addlib("rexxsupport.library",0,-30,0) then do
		Say 'Cannot open rexxsupport.library!'
		Exit 20
	End

ACnt = 0
Say ' '||$ver||'[1m Started[0m'
Say
If ~exists(dest) then do
	Say ''
	Say ' You''ll have to change the variable: "Dest" in the ARexx script'
	Say ' or at least make the directory 'dest
	Exit 10
End
If exists(Dest||Disk) then do
	Say ' The destination directory '||dest||disk||' already exists!'
	Say ' Are you sure you want to continue?'
	Pull Resp ; Resp = Upper(Resp)
	If Resp ~= 'Y' then Exit 11
	Else do
		Say ' Cleaning out some workspace'
		Address Command 'Delete '||Dest||Disk||' ALL QUIET'
	End
End
If ~exists(Dest||Disk) then Address Command 'Makedir 'Dest||Disk
If ~exists(Dest||Disk||'Aminet') then Address Command 'MakeDir 'Dest||Disk||'/Aminet'
If ~open(cfg,Config,'R') then do
	Say ' Sorry, I couldn''t open the file: '||config||' for building the directories'
	Say ' Do you [1mHAVE[0m the CD [1m"'||Disk||'"[0m in the drive?'
	Exit 20
End
do until eof(cfg)
	line = readln(cfg)
	If word(pos('|',line),1) = 1 then iterate
	If line = '' then leave
	comm = DELSTR(Line,1,39)
	Parse Var line fname pathfile junk junk
	If ~exists(Disk||':Aminet/'||pathfile||'/'||fname) then iterate
	pathfile = strip(pathfile)
	cutpath = pos('/',pathfile) - 1
	fpath = strip(substr(pathfile,1,cutpath))
	If ~exists(Dest||Disk||'/Aminet/'||fpath) then do
		Address Command 'Makedir 'Dest||Disk||'/Aminet/'||fpath
		Say '[1A Making Directory '||DEst||Disk||'/Aminet/'||fpath'                  '
	End
	If ~exists(dest||Disk||'/Aminet/'||pathfile) then do
		Say '[1A Making Directory '||Dest||Disk||'/Aminet/'||pathfile'                  '
		Address Command 'Makedir 'Dest||Disk||'/Aminet/'||pathfile
		If ~Open(List,dest||Disk||'/AreaList','A') then Call Open(List,dest||Disk||'/AreaList','W')
		Call Writeln(List,LEFT('Aminet/'||Pathfile,26)Pathfile)
		Call Close(List)
		ACnt = ACnt + 1
	End
	pathfile = strip(pathfile)
	fcomm = Statef(Disk||':AMINET/'||pathfile||'/'||fname)
	Parse Var fcomm junk size junk junk datum junk
	filename = substr(fname,1,22)
	size = right(size,9)
	comm = strip(left(comm,40))
	datef = right(date('s',datum),6)
	If ~open(list,dest||Disk||'/Aminet/'||pathfile||'/Files.BBS','A') then Call Open(list,dest||Disk||'/Aminet/'||pathfile||'/Files.BBS','W')
	Call writeln(list,filename||size||' '||datef||' '||comm)
	Say '[1A Writing file: '||Disk||'/Aminet/'||pathfile||'/'||filename'          '
	Call Close(list)
End
Call Close(cfg)
Address Command 'C:Copy 'Config Dest||Disk
Say ' Created '||ACnt||' directories.'
Areas = 0
Say ' Creating '||Dest||Disk||'/DIRS.TXT'
Say
If ~OPEN(DTXT,Dest||Disk||'/DIRS.TXT','A') then Call OPEN(DTXT,Dest||Disk||'/DIRS.TXT','W')

If ~OPEN(List,Dest||Disk||'/Arealist','R') then do
	SAY ' Something is TERRIBLY wrong.'
	SAY ' Can''t open the AreaList!'
	EXIT 11
End
Else do i = 1 to ACnt
	DirLine = READLN(List)
	Parse Var Dirline Fullpath Pathline
	Fullpath = STRIP(Fullpath)
	Pathline = STRIP(Pathline)
	Call Open(AList,Dest||'TREE','R')
	Do forever
		ALine = Readln(AList)
		Parse Var ALine Tpathline ADesc
		APathline = strip(TPathline)
		If Pathline = TPathline then leave
	End
	ADesc = Strip(ADesc)
	Cut = POS('/',TPathline) - 1
	SDIR = STRIP(Upper(Substr(APathline,1,Cut)))
	Say '[1A 'LEFT(i,4)'Writing '||SDIR||' 'ADesc'                  '
	Call WRITELN(DTXT,LEFT('Aminet/'||pathline,30)STRIP(LEFT(SDIR||': '||ADesc,32)))
	Call CLOSE(AList)
End
Call Close(List)
Call Close(DTXT)

If EXISTS(Dest||Disk||'/Arealist') then call DELETE(Dest||Disk||'/Arealist')

Say ' '||$ver||'[1m Finished[0m'
Say ' And it only took [1m'||Time('E')||'[0m seconds!'
Exit

Usage:
Say ''
Say ' Hey dummy, get it [1;3mRIGHT![0m'
Say ''
Say ' Usage: Rx CDS_Setup[.rexx] <AminetCD>'
Say ''
Say ' Where <AminetCD> would be eg. [1mAminet24[0m'
Say ''
Exit 10

NewSTDERR:
Call CLOSE('STDERR')
If OPEN('STDERR',arg(1),W) THEN Call pragma('*','STDERR')
ELSE DO
 Call PRAGMA('*')
 Call OPEN('STDERR','*',W)
End
RETURN
