function doc(topic)
%DOC Display HTML documentation in Web browser.
%
%   DOC, by itself, launches the Help Desk.
%
%   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.
%
%   Examples:
%      doc eig
%      doc symbolic/eig

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.34 $  $Date: 1998/07/27 16:13:45 $

% Get root directory of MATLAB Help
help_path = docroot;

% Show error if help docs not found 
if (isempty(help_path))
   html_file = fullfile(matlabroot,'toolbox','local','helperr.html');
   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(html_file);
   return
end

% Case no topic specified.
if (nargin == 0)
    html_file = fullfile(help_path,'helpdesk.html');
    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
 
 
% Case subscribe topic requested.
if (strcmp(topic,'subscribe'))
    html_file = fullfile(help_path,'subscribe.html');
    if (exist(html_file) ~= 2)
        error(['Could not locate subscribe page.' sprintf('\n') ...
        'Please make sure the help system files are installed.']);
    end
    display_file(html_file);
    return
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_file = [topic_name '.html'];

% Case topic path specified, e.g., symbolic/cos.
if (~isempty(topic_dir))
    html_file = fullfile(help_path, 'toolbox', topic_dir, topic_file);
    if (exist(html_file) ~=2)
        if (exist(topic_name) & exist(fullfile(help_path,'nohelp.html'))==2)
            html_file = fullfile(help_path,'nohelp.html');
        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);
   return
end

% Case toolbox path not specified.
% 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 MATLAB html file directory.
html_file = fullfile(help_path, 'techdoc', 'ref', topic_file);
if (exist(html_file) == 2)
   topic_files = [topic_files {html_file}];
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
         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) & exist(fullfile(help_path,'nohelp.html'))==2)
        html_file = fullfile(help_path,'nohelp.html');
    elseif (exist(fullfile(help_path,'nofunc.html'))==2)
        html_file = fullfile(help_path,'nofunc.html');
    else
        error(['Invalid Documentation Topic: ' topic]);         
    end
    display_file(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.
s = size(topic_files,2);
if (s > 1)
   disp(' ');
   disp(' Overloaded methods');
   for i = 2:s
      [path, name, ext, ver] = fileparts(topic_files{i});
      if path(end)==filesep | (isvms & path(end)==']')
         path(end)=[];
      end
      k = find(path==filesep);
      if ~isempty(k)
        name = path(k(end)+1:end);
      else
        name = path;
      end
      disp(['   doc ' name '/' topic_name]);
   end
   disp(' ')
end
         
function display_file(html_file)
    % 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



