% generate and print the steady-state values to be replaced in the mod file
% use the following to build the initval entries
% need to run dynare first
%ss_read_3co.m;
% if ~exist('xie','var')
%     disp('xie does not exist. Setting it to 1')
%     xie=1;
% end
% if ~exist('xie_s','var')
%     disp('xie_s does not exist. Setting it to 1')
%     xie_s=1;
% end    


namemod=input(upper('Give name of .mod file to update with extension  ==> '),'s');
disp('________________________________________________________________________________');
%logeval=input(upper('Is it in logs? (1/0)'));
dottt=regexp(namemod,'\.');
if ~isempty(dottt)
eval(['ys_=',namemod(1:dottt-1),'_steadystate;']);
end
lgy_=M_.endo_names;
kkk=[];
for jj=1:size(ys_),
%     if logeval==1
%         if ys_(jj)>1e-10
%             loggy='log(';
%             loggyend=')';
%         else
            loggy=[];
             loggyend=[];
%         end
%     end
    kkk=strvcat(kkk,[lgy_(jj,:),'=',loggy,num2str((ys_(jj)),16),loggyend,';']);
end,
kkk=strvcat(kkk,'end;');


[ddd,jjj]=regexp(namemod,'\.mod','match');

if isempty(jjj)

    namemod=[namemod,'.mod'];
end
model=(textread(namemod,'%s','bufsize',1000000,'delimiter','\n'));
%model=char(strread(model','%s','delimiter',';&\n'));
% modstrt= strmatch('initval',model);
modstrt=find_incell(model,'^initval|(?<!(\S\s*))(initval)');
% modjunk=strmatch('end',model);
modend=modstrt+find_incell(model(modstrt:end,:),'^end|(?<!(\S\s*))(end)');%modjunk(min(find(modjunk>modstrt)));

surerep=input('Are you sure? (1/0)(default 0)');
if surerep==1
if exist('old_stuff','dir')==0
    [cck,jjk]=mkdir(['old_stuff']);
    if cck~=1
      disp(strvcat('__________________________________________________________',...
     upper('Failed to generate backup folder old_stuff... aborting updating')));
    end
end
if isunix
   copycom='cp ';
   dircom='/';
   else
   copycom='copy ';
   dircom='\';
end 
[ck,jj]=system([copycom,namemod,'    old_stuff',dircom,  regexprep(namemod,'\.mod',''),'_',deblank(datestr(now,'yyyymmddTHHMMSS')),'.mod']);
 
%     replacy=input(upper('Proceeding to replace initial values in the original file? (1/0)  '));
if ck==0
    disp(strvcat('__________________________________________________________',...
     upper('Saved copy of model with current time in sub-directory old_stuff')));
 [xx,yy]=find_incell(model,'% New version');
 if isempty(xx)
yy=1;
 end
 model=model(yy(end):end,:);
 modstrt=modstrt-yy(end)+1;
 modend=modend-yy(end)+1;
 model=char(model);
tmp=strvcat(model(1:modstrt,:),kkk,model(modend:end,:));
fid=fopen(namemod,'w+');
fprintf(fid,'%s\n',['//% New version updated: ',datestr(now),';']);
fclose('all');
fid=fopen(namemod,'a+');
for ddd=1:size(tmp,1);

fprintf(fid,'%s\n',[deblank(tmp(ddd,:))]);

end
fclose('all');
else
       disp(strvcat('__________________________________________________________',...
     upper('Failed to save copy of model... aborting updating')));
end
end %sure
% if exist('Tax','var')
% kkk=strvcat(kkk,['Tax=',num2str(Tax,16),'; Tax_s=',num2str(Tax_s,16),'; %copy into otherparm.m;'],...
%     ['xie=',num2str(xie,16),';'],['xie_s=',num2str(xie_s,16),';'],...
%     ['e_uf_ss=',num2str(e_uf_ss,16),';'],['e_f_ss=',num2str(e_f_ss,16),';'],['eI_f_ss=',num2str(eI_f_ss,16),';']);
% disp('initval;')
% disp(kkk)
% end