/* $Id: genid.pl,v 1.13 2004/10/09 03:44:54 sandro Exp $

   genid.pl

   Like gensym, but returns a (unique, new) URL which we can answer
   for later.

   uses app_config(site_uri, 'http://roke.hawke.org:7116/').

   IF THE RDF DATABASE IS BEING SAVED, this state should be too!
   (maybe we should save this IN the rdf database?)
   write_genid_info can be used....

   Or just qsave_program(home-program).

   rdf(<foo> self:genidBaseCount literal(7)).    ?
   
   
*/  

/*
set_system_id :-
    get_time(Time),
    isotime(Time, _I),
    ( read_link('/proc/self', PID, _)
    ; PID=unknown ),
    % concat_atom(['http://semwalk.org/genid', I, PID], '/', ID),
    concat_atom(['genid_temp_hack:'], ID),
    flag(system_id, _, ID).

:- set_system_id.
*/

:- dynamic genid_base_used/1.
:- multifile genid_base_used/1.

genid(Base, URI) :-
   app_config(site_uri, SiteURI),
   concat_atom([SiteURI, 'resource/', Base], CounterBase),
   with_mutex(genid_mutex, 
	      (   genid_base_used(CounterBase)
	      ->  flag(CounterBase, Count, Count+1),
		  atom_number(CountAtom, Count),
		  atom_concat(CounterBase, CountAtom, URI)
	      ;   Count = 1,
		  flag(CounterBase, _, Count+1),
		  assert(genid_base_used(CounterBase)),
		  % atom_concat(CounterBase, '1', URI)
		  CounterBase = URI
	      )
	     ).
/*
  assume tell was used to redirect output

  this should be called as part of saving state, if state is being
  saved in some manual manner...
*/

write_genid_info :-
	listing(genid_base_used),
	(   genid_base_used(B),
	    flag(B, Count, Count),
	    format(':- flag(~q, _, ~a).~n', [B, Count]),
	    fail
	;   true
	).

write_genid_info(Filename) :-
	tell(Filename),
	write_genid_info,
	told.

	

   
   

