function createFunA(pname,sdesc,fdesc,auth,cdate,mode)
% creatFun Create a functions with header

% revision history:
% 01/04/1994 Mark D. Shattuck <mds> createFun.m  
% 07/22/1996 mds add auth,sdesc,fdesc
% 04/03/1997 mds add default args
% 07/07/2001 mds add vars, change order 
% 11/30/2002 mds add defs 
% 12/24/2005 mds add cdate
% 01/21/2006 mds add %%
% 04/22/2014 mds add mode for update[u] create[c]
% 04/19/2016 mds convert to pname='[o1,o2,...]=name(i1[def1],i2[def2],...)'


%% Parse Inputs
if(~exist('auth','var') || isempty(auth))
  auth='Mark D. Shattuck';
end

if(~exist('sdesc','var') || isempty(sdesc))
  sdesc='Description here.';
end

if(~exist('fdesc','var') || isempty(fdesc))
  fdesc='';
end

if(~exist('cdate','var') || isempty(cdate))
  cdate=datestr(now, 'mm/dd/yy');
end

if(~exist('mode','var') || isempty(mode))
  mode='c';
end

%%  Main 
base=camelcase(name,true,false);  % compress camelcase first word lower
fn=[base '.m'];
acode=lower(auth(auth>='A' & auth<='Z'));

Nv=length(vars);

fom='w';
if(exist(fn,'file'))
  switch mode
    case 'c'
      fprintf(1,'%s exists.\n',fn);
      fprintf(1,'Rerun with mode ''o'' to over-write.\n');
      fprintf(1,'Rerun with mode ''u'' to update.\n');
      return;
    case 'o'
      fom='w';
      delete(fn);
    case 'u'
      fom='a+';
  end
end

fid=fopen(fn,fom);
fseek(fid, 0, 'bof'); % begining

fprintf(fid,'function %s(',base);
fprintf(fid,'%s,',vars{1:end-1});
fprintf(fid,'%s',vars{end});
fprintf(fid,')\n');

fprintf(fid,'%% %s <%s>\n',base,sdesc);
fprintf(fid,'%% %s\n',fdesc);
fprintf(fid,'\n');
fprintf(fid,'%% revision history:\n');
fprintf(fid,'%% %s %s <%s> %s\n',cdate,auth,acode,fn);
fprintf(fid,'\n');

if(~isempty(vars{1}))
  fprintf(fid,'%%%% Parse Input\n');
  for nv=1:Nv
    fprintf(fid,'if(~exist(''%s'',''var'') || isempty(%s))\n',vars{nv},vars{nv});
    fprintf(fid,'\t%s=%s;\n',vars{nv},defs{nv});
    fprintf(fid,'end\n\n');
  end
end

fprintf(fid,'%%%% Main\n\n\n\n\n\n');
fclose(fid);
edit(fn);

