/* MatCAM v2.0 by Mat Bettinson and Giorgos Stagakis :) 


   This version has byte progress display, nice input output, checks
   if the picture has been modified since the last visit and retries
   after 10 seconds if not. 

   I put a bit larger 'delay' for the picture rendering time,
   so that you don't get half pictures

   The Multiview now opens with a preset port named MATCAM.
   My Multiview was V39 (OS3.0) Yes, I'm still using 3.0.

   User-Agent is now MatCAM/1.0 instead of HTTPGet

   Note: It's almost rewritten and tested and it should work
         but then, it might not :)

   Mat: Yup, Giorgos added funky stuff, all the checking stuff
        stand alone etc. Coolness. Last minute; changed the timing,
        changed text and added quiet mode.


*/

delay           = '50'                 /* Delay between pictures in seconds */
busywait        = '30'                 /* Delay between pic change checking */
path            = 'sys:utilities/'     /* Path to Multiview                 */
host            = 'www.cu-amiga.co.uk' /* Remote Hostname                   */
file            = 'woacam.jpg'         /* Remote path/file                  */
local           = 't:woacam.jpg'       /* Local picture to save             */

signal on break_c

PARSE ARG quiet

quiet = strip(upper(quiet))

If quiet ~= '' & quiet ~= 'QUIET' then DO
  say ' Usage: [rx] woacam[.rexx] [QUIET]'
  say
  say ' Example: rx woacam quiet'
  say
  EXIT
END

if quiet = 'QUIET' then quiet = 1
ELSE quiet = 0

if ~quiet then DO
  say
  say '*** WOAcam 2.0 by Giorgos Stagakis and Mat Bettinson ***'
  say
END

i = 1

do forever

	i = 1-i
	do until secs = 0
		call time(r)
		secs = httpget(host,file,local'.'i)
		if secs ~= 0 then address command 'wait' secs
	end
	call view(local'.'i)
	ctim = time(r)
	if ctim < delay then address command 'wait' trunc(delay - ctim)

end

exit

view: procedure expose path quiet
	parse arg piccy

	if ~quiet then say d2c(13)'Viewing: 'piccy

	if ~show('p','MATCAM') then do
		address command 'run >NIL: 'path'MultiView 'piccy' WINDOW PORTNAME MATCAM'
	end
	else do
		address MATCAM 'OPEN NAME 'piccy
	end

return 1

httpget: procedure expose lmodif quiet busywait

	parse arg host,file,to

	siz = '???'
	em = d2c(13)
	len = 0

	if ~open(1,'TCP:'host'/80') then call sayx 'Unable to connect to 'host'!'
	call writeln(1,'GET /'file' HTTP/1.0')
	call writeln(1,'User-Agent: MatCAM/1.0')
	call writeln(1,'')

	if ~open(2,to,'W') then call sayx 'Unable to open output file!'

	do until str = ''
		str = compress(readln(1),em)
		parse var str pre' 'nex
		pre = upper(pre)
		select
			when pre = 'LAST-MODIFIED:' then do
				if nex = lmodif then do
					call close(1)
					call close(2)
					if ~quiet then say 'Picture not changed, retrying in 'busywait' seconds...'
					return busywait
				end
				else lmodif = nex
			end
			when pre = 'CONTENT-LENGTH:' then siz = nex
			otherwise nop
		end
	end
	str = ''
	if ~quiet then say 'Getting: 'file' as 'to' - 'siz' bytes'
	do until eof(1)
		st = readch(1,8192)
		len = len + length(st)
		str = str||st
                if ~quiet then call writech(stdout,em||len)
		if length(str) >= 8192 then do
			call writech(2,str)
			str = ''
		end
	end
	if length(str) > 0 then do
		call writech(2,str)
		str = ''
	end
	call close(1)
	call close(2)

return 0

sayx: procedure
	parse arg a
	say a
exit

break_c:
	say '***Break'
exit

