function varargout=guide(varargin)
%GUIDE  Design GUI.
%   GUIDE initiates the GUI Design Environment tools that allow
%   FIG-files to be edited interactively.  Calling GUIDE by itself will
%   open a new untitled FIG-file. GUIDE(filename) opens the FIG-file
%   named 'filename' if it is on the MATLAB path. GUIDE(fullpath) opens
%   the FIG-file at 'fullpath' even if it is not on the MATLAB path.
%   GUIDE(HandleList) creates new FIG-files for each figure(s) containing
%   handles in the HandleList and copies the contents of the figure(s)
%   into the FIG-files.  
%     
%   See also INSPECT.

%   GUIDE(..., '-test') automatically puts guide in .fig file mode,
%   and stops the initial blocking dialog from coming up.  This
%   allows internal MathWorks automated tests to run. 

%   Copyright 1984-2000 The Mathworks, Inc.
%   $Revision: 1.51 $ $Date: 2000/07/31 14:54:25 $

  import com.mathworks.ide.layout.*;

  % error out if there is insufficient java support on this platform:
  error(javachk('MWT', 'GUIDE'));

  num_inputs = nargin;
  if num_inputs > 0 & isequal(varargin{end}, '-test')
    setappdata(0, 'MathWorks_GUIDE_testmode',1);
    num_inputs = num_inputs - 1;
  else
    if isappdata(0,'MathWorks_GUIDE_testmode')
      rmappdata(0,'MathWorks_GUIDE_testmode');
    end
  end
    
  error(nargchk(0,1,num_inputs));
  
  if num_inputs == 0

    % Open an untitled layout editor.
    result = LayoutEditor.newLayoutEditor;
    
  else
    ERRMSG = 'GUIDE can only be called with a .fig file name or a handle list.';

    % We want to pass a filename with a fully specified path to the
    % java code.  Be prepared to handle two other cases:
    % 1) a file name specified w/o a .fig extension (just append
    % it)
    % 2) a file name specified w/o a path (prepend the path by
    % using WHICH to find the file in the current directory or on
    % the path)
    % After prepending the path and/or apending the extension, use
    % EXIST to confirm that it is a legitimate file.
    if isstr(varargin{1})
      filename = varargin{1};
      [path,file,ext] = fileparts(filename);
      % APPEND THE .FIG EXTENSION if necessary:
      if isempty(ext)
	ext = '.fig';
      end
      if strcmp(ext, '.fig')
	% Try to open a fig file.
	filename = fullfile(path, [file ext]);
	% PREPEND THE PATH if necessary:
	if isempty(path)
	  filename = which(filename);
	end
	% MAKE SURE IT EXISTS before opening the layout editor:
	if exist(filename)
	  result = LayoutEditor.openLayoutEditor(filename);
	else
	  error(ERRMSG);
	end
      else
	error(ERRMSG);
      end
    else
      % Try to clone a figure.
      % shape input argument into a row vector:
      figs = varargin{1}; figs = figs(:)';
      % handle the special case of guide(0) which was legal in R11
      if isequal(figs, 0)
	result = guide;
      else
	% verify that they are all figure handles
	if any(~ishandle(figs)) |...
	      length(figs) ~= length(strmatch('figure',get(figs,'type'),'exact'))
	  error(ERRMSG);
	end
	result = {};
	for fig = figs
	  result{end+1} = LayoutEditor.openLayoutEditor(fig);
	end
	if length(figs) == 1
	  result = result{1};
	end
      end
    end
  end
  
  if nargout
    varargout{1} = result;
  end
