function stat=web(html_file,useSystemBrowser,waitForNetscape)
%WEB Open Web browser on site or files.
%   WEB URL displays the specified URL (Uniform Resource Locator) in
%   in the desktop Help Browser window.
%
%   WEB URL -BROWSER opens a Web browser and loads the file or Web site specified
%   in the URL (Uniform Resource Locator).  The URL can be of any form
%   that your browser can support.  Generally, it can specify a local 
%   file or a Web site on the Internet.  On UNIX, the Web browser used 
%   is specified in the DOCOPT M-file. On Windows, the Web browser is 
%   determined by the operating system.
%
%   STAT = WEB(...) returns the status of the WEB command in the variable
%   STAT. STAT = 0 indicates successful execution. STAT = 1 indicates that
%   the browser was not found. STAT = 2 indicates that the browser was
%   found, but could not be launched.
%
%
%   Examples:
%      web file:///disk/dir1/dir2/foo.html
%         opens the file foo.html in your browser.
%
%      web(['file:///' which('foo.html')]);
%         opens the file foo.html if it is on the MATLAB path.
%
%      web http://www.mathworks.com
%         loads The MathWorks Web page into your browser.
%
%      web mailto:email_address
%         uses your browser to send mail.
%
%   See also DOC, DOCOPT.

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.38 $


% Initialize error status.
stat = 0;


% If no argument is specified, complain and exit.
if nargin==0
   disp('No URL specified.');
   return;   
end


% If in Desktop environment, and only URL argument specified, use ICE Browser.
% Except for "mailto:", which is not supported in ICE.
if nargin==1 & (usejava('mwt') == 1) & ~strncmp(html_file,'mailto:',7)
   hBrowser = javaMethod ('getHelpBrowser','com.mathworks.ide.help.HelpBrowser');
   % If no protocol specified, or an absolute/UNC pathname is not given, 
   % include explicit 'http:'.  Otherwise ICE assumes 'file:'
   if isempty(findstr(html_file,':')) & ~strcmp(html_file(1:2),'\\') & ~strcmp(html_file(1),'/') 
      html_file = ['http://' html_file];
   end
   setCurrentLocation(hBrowser, html_file);
   return;
end


% Otherwise, use system web browser.  Check that correct argument was given.
if nargin>1 & ~strcmp(useSystemBrowser,'-browser')
   disp('Warning: second argument should be ''-browser'' to use web browser');
   if (useSystemBrowser == 1)
      waitForNetscape=1;  % previously, allowed waitForNetscape as second argument.
   end
end


% If no waitForNetscape argument is not specified, provide default of 0.
if nargin<=2,
  waitForNetscape=0;
end


% determine what computer we're on
if (~isunix & ~ispc)
    error(['WEB not available for this architecture: ', computer, '.'])
end

% get options
[doccmd,options] = docopt;

% open HTML file in browser
if isunix
   % The unix command has a bunch of problems when we try to handle errors.
   % Assume we submit to unix:
   %
   %    Mosiac file &
   %
   %    1. We don't know what shell is being used. It depends on the platform.
   %    2. The unix command does not save stderr
   %    3. You cannot save output since this blocks the return of the MATLAB
   %    prompt.
   %    4. You will always get the pid message and if there is a error message
   %    it will be cryptic one from the system.
   %    5. Status is always successful.
   %
   % Because of the above problems, there is no error handling. The user
   % will have to figure out things from the system error message.

   disp('To learn how to configure your Web browser type ''help docopt''');
   if isequal(lower(computer), 'hp700')
     comm = [doccmd ' ' options ' -remote "openURL(' html_file ')" >& /dev/null '];
   else
     comm = ['sh -c ''' doccmd ' ' options ' -remote "openURL(' html_file ')" > /dev/null 2>&1 ''' ];
   end
   
   % separate the path from the filename for netscape
   [dirx,fname,extx]=fileparts(doccmd);

   if strmatch(fname, strvcat('netscape', 'Netscape'))
      % We need to explicitly use /bin/ls because ls might be aliased to ls -F
      lockfile = [getenv('HOME') '/.netscape/lock' sprintf('\n')];
      [status,result]=unix(['/bin/ls ' lockfile]);
      if length(findstr(result,lockfile)) > 0
         % if the netscape lock file exists than try to open 
         % html_file in an existing netscape session
         status = unix(comm);
      else
         status=1;
      end
   else
      % Mosaic or Arena must be the browser can not look for netscape lock file
      % To the best of my knowledge Mosaic & Arena don't support -remote  bmb
      % so just start a new session
      status=1;
   end

   if status
      % browser not running, then start it up.
      if isequal(lower(computer), 'hp700')
	comm = [doccmd ' ' options ' ' html_file ' >& /dev/null &' ];
      else
	comm = ['sh -c ''' doccmd ' ' options ' ' html_file ' > /dev/null 2>&1 &'''   ];
      end
      status = unix(comm);
      if status
         stat = 1;
      end

      %
      % if waitForNetscape is nonzero, hang around in a loop until
      % netscape launches and connects to the X-server.  I do this by
      % exploiting the fact that the remote commands operate throuth the
      % netscape global translation table.  I chose 'undefined-key' as a
      % no-op to test for netscape being alive and running.
      %
      if waitForNetscape,
	if isequal(lower(computer), 'hp700')
	  comm = [doccmd ' ' options ' -remote "undefined-key()" >& /dev/null'];
        else
	  comm = ['sh -c ''' doccmd ' ' options ' -remote "undefined-key()" > /dev/null 2>&1 '''];
	end
	
         while ~status,
             status = unix(comm);
             pause(1);
         end
      end
    end

elseif ispc
    stat = ibrowse(html_file);
end
