%
%
% start Linda servers & clients at the port specified by -lp
%
% -lp	specifies Linda server port no.
% -cl	specifies a client to run
% -ax	specifies the full path name of the XML file to write,
%	that contains the Linda server host & port
% -ap	specifies the full path name of the Prolog file to write
% -at	specifies the full path name of the text file to write
%

:- ['../prolog/linda/linda_server'].
:- ['../prolog/util/cmdline'].
:- ['../prolog/util/flatten'].
:- use_module(library(system)).


%
% starts the Linda server & launches clients
%

go :-
	get_parameter_value('-lp',Port),
	get_parameter_value('-ax',Xml_file),
	get_parameter_value('-ap',Prolog_file),
	get_parameter_value('-at',Text_file),
	findall(Client,cmdline:get_command_line('-cl',Client),Clients),		%get all clients

	linda(	(Host:Port)-(	user:print_startup(Port),			%print startup message
				user:write_address_xml_file(Xml_file,Host,Port),
				user:write_address_prolog_file(Prolog_file,Host,Port),
				user:write_address_text_file(Text_file,Host,Port),
				user:launch_clients(Host,Port,Clients))		%launch (any) clients
		).	%launch all clients

get_parameter_value(Param,Value) :-
	cmdline:get_command_line(Param,Value),
	!.
get_parameter_value(_,_).


print_startup(Port) :-
	format("*~n*~n* starting Linda server at port ~q~n*~n*~n",[Port]).	%print message

%
% launch clients w/ Linda server at Host:Port
%
launch_clients(_,_,[]).

launch_clients(Host,Port,[Command|Rest]) :-
	launch_single_client(Host,Port,Command),		%launch 1 client
	launch_clients(Host,Port,Rest).				%launch the rest

launch_single_client(Host,Port,Client_command) :-
	name(Host,Host_string),					%convert host to string
	name(Port,Port_string),					%convert port to string
	name(Client_command,Client_command_string),		%convert client command-line to string
	flatten([	Client_command_string,			%append host & port in strings
			" -lh ",Host_string,
			" -lp ",Port_string,
			" &"],Command_string),
	name(Command,Command_string),				%convert final command to atom
	format("Launching client:~q~n",[Command]),
	system(Command).					%execute command



%
% write Linda server host & port to an XML file
% & make it accesible by all
%
write_address_xml_file(Filename,Host,Port) :-
	nonvar(Filename),
	!,
	tell(Filename),
	write('<linda_server>'),
	nl,
	write('<host>'),
	write(Host),
	write('</host>'),
	nl,
	write('<port>'),
	write(Port),
	write('</port>'),
	nl,
	write('</linda_server>'),
	nl,
	told,
	name(Filename,Filename_string),
	flatten(["chmod a+r ",Filename_string],Command_string),
	name(Command,Command_string),
	system(Command).
write_address_xml_file(_,_,_).

%
% write Linda server host & port to a Prolog file
% & make it accesible by all
%
write_address_prolog_file(Filename,Host,Port) :-
	nonvar(Filename),
	!,
	tell(Filename),
	write('linda_host(\''),
	write(Host),
	write('\').'),
	nl,
	write(linda_port(Port)),
	write('.'),
	nl,
	told,
	name(Filename,Filename_string),
	flatten(["chmod a+r ",Filename_string],Command_string),
	name(Command,Command_string),
	system(Command).
write_address_prolog_file(_,_,_).

write_address_text_file(Filename,Host,Port) :-
	nonvar(Filename),
	!,
	tell(Filename),
	write(Host),
	nl,
	write(Port),
	nl,
	told,
	name(Filename,Filename_string),
	flatten(["chmod a+r ",Filename_string],Command_string),
	name(Command,Command_string),
	system(Command).
write_address_text_file(_,_,_).

%
% start running after loading
%

:- go.

