function [] = duplicate_template(name)

startpath=pwd;
fcnpath=fileparts(mfilename('fullpath'));
algopath=strcat(fcnpath,'/../',name);

fprintf('Creating folder %s\n',algopath);
system(sprintf('mkdir -p %s',algopath));

fprintf('Copying files ...\n');
filelist={...
    'algo_template.slx',...
    'algo_template_harness.slx',...
    'algo_template_referenced.slx',...
    'algo_template_harness_run.m',...
    'algo_template_test.m',...
    'algo_template_loadtp.m',...
    'algo_template_loadfp.m',...
    'algo_template_signal_buses.m',...
    'algo_template_outBus_def.m',...
    'algo_template_inBus_def.m',...
    'algoobj_template.m',...
    };
for i=1:numel(filelist)
    cpcmd=sprintf('cp -vf %s/%s %s/%s',fcnpath,filelist{i},algopath,strrep(filelist{i},'template',name));
    system(cpcmd);
end

eval(sprintf('cd %s',algopath));

fprintf('Changing models data dictionary ...\n');
modellist={...
    'algo_template',...
    'algo_template_harness',...
    };
ddname=sprintf('algo_%s.sldd',name);
for i=1:numel(modellist)
    modelname=strrep(modellist{i},'template',name);
    load_system(modelname);
    set_param(modelname, 'DataDictionary', ddname);
    save_system(modelname);
    close_system(modelname);
end

% The way I found to get block diagrams 
% programmatically (of opened system) is via:
% proplist = find_system
% paramstruct = get_param(proplist{1},'DialogParameters')
% and then
% get_param(proplist{1},<paramstruct field>)
%
% hence I do not see other options than
% keeping a list like this
% updated (manually ...) with the template content:
fprintf('Changing block diagrams properties ...\n');
list={{'algo_template/realtime'     ,'SampleTime',}, ...
      {'algo_template/signal_in'    ,'OutDataTypeStr',}, ...
      {'algo_template/signal_out'   ,'OutDataTypeStr',}, ...
      {'algo_template/Gain'         ,'Gain'}, ...
      {'algo_template/Constant'     ,'OutDataTypeStr'}, ...  
      {'algo_template/Model'        ,'ModelNameDialog'}, ...        
      };
load_system(['algo_' name]);
for i=1:numel(list)
    parobj=strrep(list{i}{1},'template',name);
    oldvalue = get_param(parobj,list{i}{2});
    newvalue = strrep(oldvalue,'template',name);
    fprintf('changing %s,%s %s->%s\n', parobj,list{i}{2},oldvalue,newvalue);
    set_param(parobj,list{i}{2},newvalue);
end

fprintf('Changing referenced subsystem instance parameter ...\n');
list={{'algo_template/Model', 'InstanceParameters'}};
for i=1:numel(list)
    parobj=strrep(list{i}{1},'template',name);
    oldvalue = get_param(parobj,list{i}{2});
    newvalue = oldvalue;
    newvalue.Value = sprintf('algo_%s_tp.refmodel',name);
    fprintf('changing %s,%s %s->%s\n', parobj,list{i}{2},oldvalue.Value,newvalue.Value);
    set_param(parobj,list{i}{2},newvalue);    
end

save_system(['algo_' name]);

fprintf('Renaming ''template'' to ''%s'' inside .m files ...\n',name);
rncmd=sprintf('cd %s; sed -i ''s/template/%s/g'' *.m', algopath, name);
system(rncmd);

eval(sprintf('cd %s',startpath));

end