:- ensure_loaded(qtest_header).
:- ensure_loaded(qtest).
:- ensure_loaded(html).
:- ensure_loaded(scrape_wiki).
:- ensure_loaded(generate_w3c_tr).

:- use_module(web_server).

go :-	run_qtests,
	web_server_run.
	
web_server_page(wikitr).

wikitr(Q, H) :-
	(   memberchk(search(QueryParms), Q),
	    memberchk(source=Source, QueryParms),
	    Source \= ''
	->  generate_page(Source, H)
	;   show_prompt(Q, H)
	).

generate_page(Source, H) :-
	scrape_data_page(Doc, Source, _Date),
	page(Doc, Page),
	H = Page.

show_prompt(_Q, H) :-
	H = html(head(title('Wiki-TR')),
		 body(+form)).

form(H) :-
	H = form(method="GET",
		 p('Enter web address of wiki key page:'),
		 input(value='http://www.w3.org/2005/rules/wg/wiki/UCR',
		       type='text',
		       size='72',
		       name='source'),
		 input(type='submit', name='Go', value='Go'),
		 p('NOTE: THIS TAKES ABOUT A MINUTE.  BE PATIENT.')
		).


web_server_page(gradual).
web_server_page(quick).

quick(_, [p(foo)]).

gradual(Q, []) :-
	format('Content-Type: text/html~n~n'),
	% memberchk(request_id(ReqId), Q),
	% web_server:ensure_content_type_sent(ReqId, 'text/html'),
	writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'),
	writeln('    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'),
	writeln('<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">'),
	writeln('<head><title>no title</title></head>'),
	writeln('<body>'),
	write_html(p('about to count:')),
	forall(between(1,10,N),
		(   write_html(p('Counting....', int(N))),
		    sleep(1)
	       )),
	writeln('</body></html>').


/*
_=/usr/bin/printenv
sandro@burns:~/semwalker$ LC_CTYPE=en_US.utf8
sandro@burns:~/semwalker$ export LC_CTYPE

*/
