%% Create the guess_NASH_FILE_steadystate.m file.

fin = fopen([infilename,'_steadystate.m'],'r');
fout = fopen(['guess_',outfilename,'_steadystate.m'],'w');

% Replacing all instances of infilename with outfilename.
while ~feof(fin)
    s = fgets(fin);
    s = strrep(s,infilename,outfilename);
    fprintf(fout,'%s',s);
end;

fclose(fin);
fclose(fout);

%% Create the new _paramfile.m file
cmd = [' copyfile ', infilename,'_paramfile.m ', outfilename,'_paramfile.m'];
eval(cmd);

%% Create the new _ss_defs.f file.
% Copying over the old file into the new filename.
cmd = [' copyfile ',infilename,'_ss_defs.m ', outfilename,'_ss_defs.m'];
eval(cmd);
% Note (MB 10/17/13): replaced ! copy by copyfile

fid=fopen([outfilename,'_ss_defs.m'],'a');
fprintf(fid,'\n');

f3in = fopen('template_for_initialization_lmss.m','r');
while ~feof(f3in)
    s = fgets(f3in);
    fprintf(fid,'%s',s);
end;
fclose(f3in);
fprintf(fid,'\n');
% Note (MB 10/17/13): added above code


% Writing the line that will run the program to find the steady-state
% values for the Lagrangian multipliers.
fprintf(fid,'%s \n',[outfilename,'_lmss;']);


fprintf(fid,'\n');

% Assigning the guesses to the Lagrangian multipliers.
for iindx =1:nbehaveqs

    out_s  = ['c1lmult',num2str(iindx),'=lmss_vals(',num2str(iindx),');'];
    fprintf(fid,'%s \n',out_s);
end
for iindx =nbehaveqs+1:2*nbehaveqs

    out_s  = ['c2lmult',num2str(iindx-nbehaveqs),'=lmss_vals(',num2str(iindx),');'];
    fprintf(fid,'%s \n',out_s);
end


fprintf(fid,'\n');


if welfare_correction_flag
    fprintf(fid,'%s \n',c1correction_ss);
    fprintf(fid,'%s \n',c2correction_ss);
    
end


fclose(fid);


%% Create the new _overwrite_params.m file, if it exists.
if exist([infilename,'_overwrite_params'],'file') > 0
    cmd = ['copyfile ', infilename,'_overwrite_params.m ', outfilename,'_overwrite_params.m'];
    eval(cmd);
end;

%% Create the new _steadystate.m file

f2in = fopen('template_for_nash_steadystate.m','r');
f2out = fopen([outfilename,'_steadystate.m'],'w');

while ~feof(f2in)
    s = fgets(fin);
    s = strrep(s,'NASH_FILE',outfilename);
    fprintf(f2out,'%s',s);
end;

fclose(f2in);
fclose(f2out);
