/*  $VER: cache.ibrx 1.0 	
	Made by Sören Forsberg : sorenf@hem1.passagen.se
  
*/
/*
fixa case oberoende i filtyps-sparning
koll om någon anger "http://" som url,eller ingen url alls
*/

OPTIONS RESULTS
OPTIONS FAILAT 15
call addlib('rexxreqtools.library',0,-30)


 NL			='0a'x		/* newline */
 zero		='00'x		/* zero */

 list    		='<PRE>'||NL||'<B>The following files were saved:</B>'||NL||NL
 tot     		=20000
 last    		=0 
 fonttag 		= "rt_font=" || 'topaz' || "/" || 8
 cachepath	='IBrowse:cache'	
 title		='Dumpfiles by sorenf@hem1.passagen.se 1998'
 savepath		='Ram:wwwfiles/'
 levels		=3
 levels_txt	='Enter number of subdirectories:'
/* path_txt		='Enter new save-path' */
 what_but		='_All|_Html|_Gif|_Jpg|_Settings|Cancel'
 tags		='rt_pubscrname=IBROWSESCREEN'  /* Change here the name of the screen IBROWSE runs */
 noconfig_txt	='I cannot find the config-file'||NL||'so I guess this is the first time'||NL||'I need you to locate your chache-drawer'
 set_but		='_Cache|_Save|_Levels|Cancel'	
 edit_txt		='Choose setting to edit'

/* Read configuration, get new cachepath if missing or wrong */

 ok=open(configfile,'IBROWSE:REXX/dumpfiles.config','R')
IF ok THEN DO
	cachepath=readln(configfile)
	savepath=readln(configfile)
	levels=readln(configfile)
	close(configfile)
END
IF ~exists(cachepath'globalcache') THEN ok=0
IF ~ok THEN DO
	DO FOREVER
	sel=rtezrequest(noconfig_txt,,title, tags)
	dirname = rtfilerequest('Work:', , "Pick IBrowse Cache directory", ,"rtfi_flags = freqf_nofiles")
	IF rtresult == 0 THEN DO
		call rtezrequest("You didn't pick a directory.", "Exit", ,)
		exit
	END
	if rtresult ~= 0 THEN DO
		cachepath=dirname
		IF exists(cachepath'globalcache') THEN leave
	END
	END      
END


address 'IBROWSE'
'QUERY URL'
url = result

IF LENGTH(url)<13 THEN DO
sel=rtezrequest('Please use a valid cache-url',,title, tags)
EXIT 
END
/* url='http://hem1.passagen.se/sorenf' */


htmtest=POS('htm',url)
if htmtest>0 THEN DO
	ch=LASTPOS('/',url)
	url=LEFT(url,ch)
END
/* IF RIGHT(url,1)~='/' THEN url=url||'/' */
urllen= LENGTH(url)

parent=LASTPOS('/',url,urllen-1)
IF urllen<8 THEN EXIT
/* Ask the user what to do and let him alter settings if he want */

DO FOREVER
	where_txt='Which files should be saved?'||NL||'Cache-path= '||cachepath||NL||'Save-path = '||savepath||NL||'Sub-directorylevels= '||levels||NL||'URL= '||url
	sel=rtezrequest(where_txt,what_but,title,'rtez_defaultresponse=1' tags)

	IF sel=5 then Do
		sela=rtezrequest(edit_txt,set_but,title,'rtez_defaultresponse=2' tags)
		IF sela=1 THEN DO
			DO FOREVER
			dirname = rtfilerequest('Work:', , "Pick IBrowse Cache directory", ,"rtfi_flags = freqf_nofiles")
			IF rtresult == 0 THEN DO
				call rtezrequest("You didn't pick a directory.", "Exit", ,)
				exit
			END
			IF rtresult ~= 0 THEN DO
				cachepath=dirname
				IF exists(cachepath'globalcache') THEN leave
			END
			END      
		END

		IF sela=2 THEN DO
			dirname = rtfilerequest('work:', , "Pick a directory", ,"rtfi_flags = freqf_nofiles")
			IF rtresult == 0 THEN DO
				call rtezrequest("You didn't pick a directory.", "Bummers", , )
			END
			IF rtresult ~= 0 THEN DO
				savepath=dirname
				call rtfreefilebuffer()
			END
		END
	
		IF sela=3 THEN DO
		long = rtgetlong(levels, , "Enter a number:",  , "rtgl_min = 1 rtgl_max = 20")
		if rtresult ~=0 then levels=long
		END
	END
	if sel=0 then exit
	if sel<5 then leave
END

/* Check with globalcache and save appropiate files */

ok=open(infile,cachepath'globalcache','R')
if ~ok then call ErrorMsg("Can't open file!")
input=readch(infile,tot)
	
Do FOREVER
IF last=0 THEN last=1	

	hit=POS(url,input,last)					/* Find a url in cachelist */
	If hit>0 then Do
		last=POS(zero,input,hit)
		mid=last
		last=POS(zero,input,last+1)
		len=last-hit
		yeah=SUBSTR(input,hit,len)
		check=compare(url,yeah)				/* Check if we want it */
		yeahupp=UPPER(yeah)
		IF sel=2 THEN DO
			checkfix=pos('.HTM',yeahupp)
			IF checkfix=0 then check=0
		END
		IF sel=3 THEN DO
			checkfix=pos('.GIF',yeahupp)
			IF checkfix=0 then check=0
		END
		IF sel=4 THEN DO
			checkfix=pos('.JPG',yeahupp)
			IF checkfix=0 then check=0
		END

		IF check=urllen+1 THEN DO			/* Yeah let`s take this one */
			bug=SUBSTR(input,hit+urllen,mid)
			bughit=POS('.',bug)
				IF (SUBSTR(input,mid-1,1)='/') | (bughit=0) THEN DO	/* Add index.html if missing */
					yeah=SUBSTR(input,hit,mid-hit)				/* /index.html if bug */
					IF bughit~=0 THEN yeah=yeah||'index.html'
			 		IF bughit=0 THEN yeah=yeah||'/index.html'
					yeah=yeah||SUBSTR(input,mid,last-mid)
				END
				ylen=LENGTH(yeah)
				yeah=RIGHT(yeah,ylen-parent)
				ylen=LENGTH(yeah)
				ADDRESS 'COMMAND'
				split=POS(zero,yeah)
				target=savepath||LEFT(yeah,split-1)
				source=cachepath || RIGHT(yeah,ylen-split)
				rc=0
				target=TRANSLATE(target,'TQH','~?#')	/* Exchange characters not allowed in amiga path's */
				'copy >NIL:' source 'to' target 
				/*say target*/
				list=list||'<A HREF=file://localhost/'||target||'> '||target||'</A>'||NL
				IF rc~=0 THEN DO
					test=0
					DO levels								/* Make X sub-directories */
						test=POS('/',target,test+1)
						IF test~=0 then Do
							drawer=LEFT(target,test-1)
						IF ~EXISTS(drawer) THEN 'makedir >NIL:' drawer					
						END
					END
				'copy >NIL:' source 'to' target
				
 				END
		END
	END
	IF hit>(tot-200) | (hit=0) THEN DO					/* Take a new bit of cachelist */
		old=RIGHT(input,200)						/* if needed				*/
		new=READCH(infile,tot-200)
		input=old||new
		last=last-(tot-200)
		IF last<1 THEN last=1
	END

	IF EOF(infile) & (hit=0) THEN LEAVE				/* Are we done yet?			*/
 
END
ADDRESS 'AREXX' 
   close(infile)

/* Save current configuration */
config=cachepath||NL||savepath || NL || levels || NL
ok=open(configfile,'IBROWSE:REXX/dumpfiles.config','W')
new=writeln(configfile,config)
 close(configfile)

/* Save the list of files and send to IBrowse */
list=list||'</PRE>'
ok=open(resultfile,'RAM:T/dump.html','W')
new=writeln(resultfile,list)
 close(resultfile)
address 'IBROWSE' 'GOTOURL file://localhost/RAM:T/dump.html'

EXIT