function retstr = webmagic(instruct, outfile)
% RETSTR = WEBMAGIC(INSTRUCT, OUTFILE) generates a magic
%          square (one whose sums along the vertical,
%          horizontal, and diagonal are the same).  INSTRUCT
%          is an input structure containing Matweb control
%          variables and a user variable specifying the size
%          of the magic square.
%     WEBMAGIC(INSTRUCT) generates a magic square
%     WEBMAGIC(INSTRUCT, OUTFILE) additionally writes the HTML 
%     output to a file whose name is stored in OUTFILE.
% 
%   Author(s): M. Greenstein, 05-18-98
%   Copyright (c) 1998 by The MathWorks, Inc.
%   $Revision: 1.4 $   $Date: 1998/08/27 18:47:09 $

%% modified for ACS at MIT ajfox 19990914 
%%% modified by Dinh-Yen Tran 19990917

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1	Initialize the return string
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

retstr = char('');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2	Set working directory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set working directory. The variables INSTRUCT.MLDIR 
% and INSTRUCT.MLID are provided automatically to all
% MATLAB Web Server applications that use the matweb program.
% Use the directory in MLDIR for writing graphics files 
% in a location that the HTTPD can read.
% This step is not important for this application which does
% not generate graphics.
% cd(instruct.mldir);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 3	Get HTML form input variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (~length(instruct.msize))
     msize = 3; % default empty field
else
     msize = str2double(instruct.msize);
     if (msize > 20), msize = 20; end % Max size
     if (msize < 3), msize = 3; end % Min size
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 4	Perform MATLAB computations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 5	Place output variables in an output structure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put variables that you want to put into your HTML output document
% in an output structure.

% Save msize in output structure
outstruct.msize = msize;

% Create magic square in output structure 
outstruct.msquare = magic(msize);

% Get column, row, and diagonal sum. Put in ouput structure.
d = sum(outstruct.msquare,1);
outstruct.msum = d(1,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 6	Call HTMLREP with output struct and output HTML
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Specify the full AFS path to <OUTPUT.HTML>
% since we're in a different directory
templatefile = '/afs/athena/course/6/6.555/matweb/dev/webmagic2.html';

% If the argument OUTFILE is given, also write HTML output to OUTFILE
if (nargin == 1)
   retstr = htmlrep(outstruct, templatefile);
elseif (nargin == 2)
   retstr = htmlrep(outstruct, templatefile, outfile);
end
