% ***BEGIN CONFIG***

% This must be changed to suit your machine (including / vs \)!
current_dir = 'C:\Users\ben-user\Documents\MATLAB\web_final\';
cd(current_dir);

% build-system slash
slash='\'; % Windows (non-escaped)

% web-server-system slash (escaped where relevant)
sslash='/'; % unix - works on Apache under windows though

% Directory names to process (IB)
ib_array = {'a_stability', ...
    'further_interpolation',...
    'gaussian_quadrature',...
    'gradient_field',...
    'interp',...
    'lu_fac',...
    'ode',...
    'orthogonal_polynomials',...
    'predictor_corrector',...
    'qr_click',...
    'qr_fac',...
    'rk_methods',...
    'stability_of_interpolation',...
    'symplectic_integrators'};

% Directory names to process
ii_array = {'advection',...
    'chebyshev',...
    'eigenstuff',...
    'fft_gui',...
    'multigrid',...
    'pde_stability',...
    'pdes_2d',...
    'poisson_equation',...
    'precond',...
    'qr_hex',...
    'stencil'}; 



% ***END CONFIG***

% All 'nav' references must be escaped because they are used with fprintf
str_nav='"includes/navigation.php"'; % navigation string in xsl file
parent = ['..' sslash];
nav = ['includes' sslash 'navigation.php'];
nav_course =[parent nav];
nav_page = [parent parent nav];


fprintf('Publishing index.m\n');
publish('index.m', struct('outputDir', current_dir));
copyfile('index.html', 'index.php');
fid = fopen('index.html', 'r');
fod = fopen('index.php', 'w');
while ( ~feof(fid) )
    s = fgetl(fid);
    s = strrep(s, str_nav, ['"' nav '"']);
    fprintf(fod, '%s\n', s);
end
fclose(fid);
delete('index.html');
fprintf('Success!\n');

fprintf('Publishing further.m\n');
publish('further.m', struct('outputDir', current_dir));
copyfile('further.html', 'further.php');
fid = fopen('further.html', 'r');
fod = fopen('further.php', 'w');
while ( ~feof(fid) )
    s = fgetl(fid);
    s = strrep(s, str_nav, ['"' nav '"']);
    fprintf(fod, '%s\n', s);
end
fclose(fid);
delete('further.html');
fprintf('Success!\n');

cd 'ib';
filename = 'partib';
fprintf(['Publishing ' filename '.m\n']);
publish( [filename '.m'], struct('outputDir', [current_dir 'ib' slash] ));
copyfile([filename '.html'], [filename '.php']);
fid = fopen([filename '.html'], 'r');
fod = fopen([filename '.php'], 'w');
while ( ~feof(fid) )
    s = fgetl(fid);
    s = strrep(s, str_nav, ['"' nav_course '"']);
    fprintf(fod, '%s\n', s);
end
fclose(fid);
fclose(fod);
delete([filename '.html']);
fprintf('Success!\n');

cd(ib_array{1}); % go to the first element of the array

for idx = 1:numel(ib_array)
    filename = ib_array{idx};
    cd ([parent filename]);
    fprintf(['Publishing ' filename '.m\n']);
    publish( [filename '.m'], struct('outputDir', [current_dir 'ib' slash filename slash],'figureSnapMethod','getframe'));
    copyfile([filename '.html'], [filename '.php']);
    fid = fopen([filename '.html'], 'r');
    fod = fopen([filename '.php'], 'w');
    while ( ~feof(fid) )
        s = fgetl(fid);
        s = strrep(s, str_nav, ['"' nav_page '"']);
        fprintf(fod, '%s\n', s);
    end
    fclose(fid);
    fclose(fod);
    delete([filename '.html']);
    close all force;
    fprintf('Success!\n');
end

cd([parent parent 'ii']);
filename = 'partii';
fprintf(['Publishing ' filename '.m\n']);
publish( [filename '.m'], struct('outputDir', [current_dir 'ii' slash] ));
copyfile([filename '.html'], [filename '.php']);
fid = fopen([filename '.html'], 'r');
fod = fopen([filename '.php'], 'w');
while ( ~feof(fid) )
    s = fgetl(fid);
    s = strrep(s, str_nav,['"' nav_course '"']);
    fprintf(fod, '%s\n', s);
end
fclose(fid);
fclose(fod);
delete([filename '.html']);
fprintf('Success!\n');


cd(ii_array{1}); % Go to the first element of the array
% ii_array = {'poisson_equation' };
for idx = 1:numel(ii_array)
    filename = ii_array{idx};
    cd ([parent filename]);
    fprintf(['Publishing ' filename '.m\n']);
    publish( [filename '.m'], struct('outputDir', [current_dir 'ii' slash filename slash],'figureSnapMethod','getframe'));
    copyfile([filename '.html'], [filename '.php']);
    fid = fopen([filename '.html'], 'r');
    fod = fopen([filename '.php'], 'w');
    while ( ~feof(fid) )
        s = fgetl(fid);
        s = strrep(s, str_nav, ['"' nav_page '"']);
        fprintf(fod, '%s\n', s);
    end
    fclose(fid);
    fclose(fod);
    delete([filename '.html']);
    close all force;
    fprintf('Success!\n');
end

fprintf('All Finished!\n');
cd(current_dir);