tool_mgr(initialize):-
	get_env_var('IFE_HOME', _Ife_Home),
	name(_Ife_Home, _Str1),
	append(_Str1, "lib/ac/ac/kbs/", _Str2),
	append(_Str2, "tools", _Str3),
	name(_TL_fname, _Str3),
	[-_TL_fname].


	/* Assuming all tools need data, get it. Then data_for predicate will start the tool */
start(_Application):-
	tool(_Application,_Prog,_Args),		/* check tool exists */
/*	to_bb(data_model, get_data_for, _Application),   << probable error >> */
	to_bb(application_dialog, get_data_for, _Application),
	assert(starting(_Application)).

start(_Application):-
	not_found(_Application).


	/* Data_model returns requested data (filename in _Data; later step file/actual data)
	 * data requested is in a format suitable for the tool, _Application
	 */
data_for(_Application, _Data):-
	starting(_Application),	/* check still trying to start the tool */
	tool(_Application, _Prog, _Args),		/* check tool exists */
	to_bb(application_dialog, new_application, _Prog, _Args, _Data),
	to_bb(transaction_mgr,started,_Application).
data_for(_Application, _Data):-
	to_bb(transaction_mgr,error,['no-one wanted ', _Application, ' data'] ).


finished(_Prog):-
	tool(_Application, _Prog, _Args),
	retract(starting(_Application)),
	to_bb(application_dialog, [[_Application, '_complete']],
			[  ['"', _Application, '" tool fininshed']]),
	to_bb(transaction_mgr, stopped, _Application).
finished(_Prog):-
	tool(_Application, _Prog, _Args),
	to_bb(application_dialog, [[_Application, '_complete']],
			[ ['"', _Application,
			   '" tool fininshed - but I dont recall starting it!!']
			]),
	to_bb(transaction_mgr,error,[['unstarted tool, "', _Application, '" finished']]).

not_found(_Prog):-
	tool(_Application, _Prog, _Args),
	to_bb(application_dialog, [[_Application, '_aborted']],
			[['I dont know about any "', _Application, '" tool']]).

aborted(_Prog, _Exit_code):-
	tool(_Application, _Prog, _Args),
	retract(starting(_Application)),
	to_bb(application_dialog, [[_Application, '_aborted']],
			[['"', _Application, '" tool aborted: exit code ', _Exit_code]]),
	to_bb(transaction_mgr,stopped,_Application).
aborted(_Prog, _Exit_code):-
	tool(_Application, _Prog, _Args),
	to_bb(application_dialog, [[_Application, '_aborted']],
			[ ['"', _Application, '" tool aborted: exit code ',
			    _Exit_code, ' - but I dont recall starting it!!']
			]),
	to_bb(transaction_mgr,error,[['unstarted tool, "', _Application, '" finished']]).

