function ext = generate_extension(type,name,par)

% generate_extension - generate extension skeleton
% ------------------------------------------------
%
% ext = generate_extension(type,name,par)
%
% Input:
% ------
%  type - extension type
%  name - extension name
%  par - parent name
%
% Output:
% -------
%  ext - extension

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1600 $
% $Date: 2005-08-18 17:41:06 -0400 (Thu, 18 Aug 2005) $
%--------------------------------

%----------------------------------------------------------
% HANDLE INPUT
%----------------------------------------------------------

%--
% check and normalize type
%--

type = type_norm(type);

%--
% check extension name
%--

% TODO: implement some naming conventions

%--
% get parent using name and type
%--

if (nargin > 2)
	
	par_ext = get_extensions(type,'name',par);

	if (isempty(par_ext))
		error(['Unable to find parent ''', par, ''' to generate new extension.']);
	end 
	
	par = func2str(par_ext.fun.main);
	
end

%----------------------------------------------------------
% GENERATE DIRECTORIES
%----------------------------------------------------------

%--
% get extension root from type and name
%--

root = [extensions_root, filesep, type_to_dir(type), filesep, name];

% NOTE: if root directory exists, assume extension exists and return

% TODO: try to create extension instead

if (exist(root,'dir'))
	ext = []; return;
end

%--
% create extension directory tree 
%--

disp(' ');

% NOTE: add extension root to path immediately so we can create extension

mkdir(root); path(path,root);

child = { ...
	'Docs', ...
	'Helpers', ...
	'Presets', ...
	'private' ...
};

% TODO: consider adding these to path

for k = 1:length(child)
	mkdir(root,child{k}); rel_path([root, filesep, child{k}]);
end

%----------------------------------------------------------
% GENERATE MAIN
%----------------------------------------------------------

%--
% get name from extension name
%--

main_name = get_main_name(name);

%--
% create main
%--

temp = [root, filesep, main_name, '.m']; rel_path(temp);

fid = fopen(temp,'w');

if (nargin < 3)
		
	fprintf(fid,'%s\n\n%s\n', ...
		['function ext = ', main_name], ...
		'ext = extension_create;' ...
	);

else
	
	fprintf(fid,'%s\n\n%s\n', ...
		['function ext = ', main_name], ...
		['ext = extension_inherit(', par, ');'] ...
	);
	
end

fclose(fid);

%----------------------------------------------------------
% CREATE EXTENSION
%----------------------------------------------------------

% NOTE: move to extension root in case name is not unique, this should be avoided

curr = pwd; cd(root);

ext = eval(main_name);

cd(curr);

%----------------------------------------------------------
% GENERATE COMPUTE
%----------------------------------------------------------

% NOTE: default compute is lazy. an identity filter is lazy, a null detector is lazy

generate_function(ext,'compute');

%--
% get updated extension
%--

% NOTE: we test that get extensions works on the generated extension

% NOTE: refresh extensions cache

ext = get_extensions('!','name',ext.name);

%--
% peek into extension
%--

if (nargout < 1)
	
	% extension_peek(ext);
	
	disp(' ');
	disp('Extension succesfully created.');
	disp(' ');
	
end

%----------------------------------------------------------
% UPDATE EXTENSION CACHE AND MENUS
%----------------------------------------------------------

update_extensions; return;


% NOTE: this ties low level code generation with high level gui, separate and make optional

%--
% update extensions cache
%--

get_extensions('!');

%--
% update menus if needed
%--

h = get_xbat_figs('type','sound');

if (isempty(h))
	return;
end

% NOTE: we only update the menu that displays the type

switch (type)
	
	%--
	% update filter menu
	%--
	
	case ({'signal_filter', 'image_filter'}), update_filter_menu;
		
	%--
	% update detect menu
	%--
	
	case ('detector'), update_detect_menu;
	
	%--
	% update measure menu
	%--
	
	case ({ ...
		'event_measure' ...
	})

	otherwise
		
end


%----------------------------------------------------------
% GET_MAIN_NAME
%----------------------------------------------------------

function main_name = get_main_name(name)

% get_main_name - compute main function name from extension name
% --------------------------------------------------------------
%
% main_name = get_main_name(name)
%
% Input:
% ------
%  name - extension name
%
% Output:
% -------
%  main_name - main function name

%--
% lower and strip punctuation
%--

main_name = lower(strip_punctuation(name));

%--
% check for name clashes and possibly add suffix
%--

% NOTE: this could clearly fail, if it does we probably have larger problems

if (~isempty(which(main_name)))
	main_name = [main_name, '_ext'];
end
