function output = doc(topic)
%DOC Display HTML documentation in the Help browser.
%
%   DOC, by itself, displays the start page for the online doc.
%
%   DOC FUNCTION displays the HTML documentation for the MATLAB
%   function FUNCTION. If FUNCTION is overloaded, doc 
%   lists the overloaded functions in the MATLAB command
%   window.
%
%   DOC TOOLBOX/FUNCTION displays the HTML documentation for 
%   the specified toolbox function.
%
%   DOC TOOLBOX displays the documentation roadmap page for 
%   the specified product.
%
%   Examples:
%      doc eig
%      doc symbolic/eig
%      doc comm

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 5.56 $  $Date: 2000/08/11 21:05:29 $

% Get root directory of MATLAB Help
help_path = docroot;

% Show error if help docs not found 
if (isempty(help_path))
   if (isstudent)
        html_file = fullfile(matlabroot,'toolbox','local','sr_helperr.html');
   else
        html_file = fullfile(matlabroot,'toolbox','local','helperr.html');
   end
   if (exist(html_file) ~=2)
        error(['Could not locate help system home page.' sprintf('\n') ...
            'Please make sure the help system files are installed.']);
   end
   display_file('Error: No help found', html_file);
   return
end

% Case no topic specified.
if (nargin == 0)
   if (usejava('mwt') == 1)
       html_file = fullfile(help_path,'begin_here.html');
   else
       html_file = fullfile(help_path,'helpdesk.html');
   end
   if (exist(html_file) ~= 2)
      error(['Could not locate help desk page.' sprintf('\n') ...
        'Please make sure the help system files are installed.']);
   end
   display_file('', html_file);
   return
end

% Make note of trailing slash and strip it.
if (topic(end)=='/') | (topic(end)=='\')
  specified_as_dir = 1;
  topic = topic(1:end-1);
else
  specified_as_dir = 0;
end


k = find(topic == '/');
if isempty(k), k = 0; end
if length(k)>1, 
  error(['Invalid Documentation Topic: ' topic]);         
end
topic_dir = topic(1:k-1);
topic_name = topic(k+1:end);
topic_name = lower(topic_name);
topic_name = strrep(topic_name, ' ', '');
topic_name = strrep(topic_name, '-', '');
if length(topic_name>2) & strcmp(topic_name(end-1:end),'.m')
  topic_name = topic_name(1:end-2);
end
topic_file = [topic_name '.html'];

% Case topic path specified, e.g., symbolic/diag.
if (~isempty(topic_dir))
    if (strcmp(topic_dir,'simulink'))
        topic_dir = [topic_dir filesep 'ug'];
    end
    if (strcmp(topic_dir,'control'))
        topic_dir = [topic_dir filesep 'ref'];
    end
    html_file = fullfile(help_path, 'toolbox', topic_dir, topic_file);
    if (exist(html_file) ~=2)
        if (exist(topic_name))
            helpwin(topic_name);
       		if (nargout > 0)
            	output = which(topic_name);
            end
            return
        elseif (exist(fullfile(help_path,'nofunc.html'))==2)
            html_file = fullfile(help_path,'nofunc.html');
        else
            error(['Invalid Documentation Topic: ' topic]);         
        end
    end
    display_file('', html_file);
    if (nargout > 0)
       output = [topic ';'];
    end
    return
end


% Case toolbox path not specified.
% Check for product page first.  Otherwise,
% search for all instances of this topic in the help hierarchy.
% Display the first instance found and list the others
% in the MATLAB command window.

topic_files = {};

% Check if matching product page first
if (strcmp(topic,'matlab'))
   html_file = fullfile(help_path, 'techdoc', 'matlab_product_page.html');
else
   html_file = fullfile(help_path, 'toolbox', topic, [topic '_product_page.html']);
end
if (exist(html_file) == 2)
   % Show it.  However, in the case where name is both directory and function, 
   % check for trailing slash; if absent, fall through and treat as function.
   if ((exist(topic)~=2) & (exist(topic)~=5)) | (specified_as_dir == 1)
      if (usejava('mwt') == 1)
         hBrowser = javaMethod('getHelpBrowser','com.mathworks.ide.help.HelpBrowser');
         showProductPage(hBrowser, html_file);
      else
         display_file('', html_file);
      end
      if (nargout > 0)
      output = [topic ';'];
      end
      return
   end
end

% Check MATLAB html file directory.
html_file = fullfile(help_path, 'techdoc', 'ref', topic_file);
if (exist(html_file) == 2)
   topic_files = [topic_files {html_file}];
   % Also check for overloaded serial i/o version.
   html_file = strrep(html_file, '.html', 'serial.html');
   if (exist(html_file) == 2)
      topic_files = [topic_files {html_file}];
   end
end

% Check toolbox help directories
toolbox_path = fullfile(help_path, 'toolbox');
if (exist(toolbox_path) == 7)
   toolbox_dirs = dir(toolbox_path);
   for i = 1:length(toolbox_dirs)
      toolbox = toolbox_dirs(i);
      if ~isempty(toolbox.name) & toolbox.name(1)~='.' & toolbox.isdir
         if (strcmp(toolbox.name,'simulink'))
            toolbox.name = [toolbox.name filesep 'ug'];
         end
         if (strcmp(toolbox.name,'control'))
            toolbox.name = [toolbox.name filesep 'ref'];
         end
         html_file = fullfile(toolbox_path, toolbox.name, topic_file);
         if (exist(html_file) == 2)
            topic_files = [topic_files {html_file}];
        end      
      end
    end
end

% No help for this topic anywhere.
if (isempty(topic_files))
    if (exist(topic_name))
        helpwin(topic_name);
        if (nargout > 0)
	        output = which(topic_name);
        end
        return;
    elseif (exist(fullfile(help_path,'nofunc.html'))==2)
        html_file = fullfile(help_path,'nofunc.html');
    else
        error(['Invalid Documentation Topic: ' topic]);         
    end
    display_file('Error: No help available', html_file);    
    return
end

% Display first topic file found.
display_file('', topic_files{1});

% If additional topic files found, list them in the MATLAB command window
% and include their paths in output return value.
s = size(topic_files,2);
if (s > 0)
   if (nargout > 0)
	  output = '';
   end
   if (s>1)
      disp(' ');
      disp(' Overloaded methods');
   end
   for i = 1:s
      [path, name, ext, ver] = fileparts(topic_files{i});
      origPath = path;
      path = strrep(path, ['simulink' filesep 'ug'], 'simulink');
      path = strrep(path, ['control' filesep 'ref'], 'control');
      if path(end)==filesep
         path(end)=[];
      end
      k = find(path==filesep);
      if ~isempty(k)
        tbxname = path(k(end)+1:end);
      else
        tbxname = path;
      end
      if i>1
         if (strcmp(name, [topic_name 'serial']))
            disp(['   doc ' name]);
         else
            disp(['   doc ' tbxname '/' name]);
         end
      end

      helpfilename = [origPath filesep name];
      if nargout > 0
          output = [output helpfilename ';'];
      end
   end
   if s>1
      disp(' ');
  end
end

function display_file(display_text, html_file)

if (usejava('mwt') == 1)
    % call the help viewer
    hBrowser = javaMethod('getHelpBrowser','com.mathworks.ide.help.HelpBrowser');
    if (strcmp(display_text, '') == 0)
        setCurrentLocation(hBrowser, display_text, html_file);
    else
        setCurrentLocation(hBrowser, html_file);
    end
else
    % Construct URL
    if (strncmp(computer,'MAC',3))
        html_file = ['file:///' strrep(html_file,filesep,'/')];
    end

    % Load the correct HTML file into the browser.
    stat = web(html_file);
    if (stat==2)
        error(['Could not launch Web browser. Please make sure that'  sprintf('\n') ...
        'you have enough free memory to launch the browser.']);
    elseif (stat)
        error(['Could not load HTML file into Web browser. Please make sure that'  sprintf('\n') ...
        'you have a Web browser properly installed on your system.']);
    end

end
