function varargout=mkdir(varargin)
%MKDIR Make directory.
%   MKDIR(DIRNAME) will create the directory DIRNAME in the current
%   directory.
%
%   MKDIR(PARENTDIR,NEWDIR) will create the directory NEWDIR in the
%   already existing directory PARENTDIR.
%
%   STATUS = MKDIR(...) will return 1 if the new directory is created
%   successfully, 2 if it already exists, and 0 otherwise.
%
%   [STATUS,MSG] = MKDIR(...) will return a non-empty error
%   message string if an error occurred.
%
%   See also COPYFILE.

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.11 $ $Date: 1998/10/06 18:39:38 $

error(nargchk(1,2,nargin))

Status = 1;
if nargin==1,
  DirName = pwd;
  NewDirName = varargin{1};
  
elseif nargin == 2,
  DirName = varargin{1};
  NewDirName = varargin{2};

end % if nargin

NewDirectory = fullfile(DirName, NewDirName);

%% Check to see if the parent directory exists
if ~exist(DirName,'dir'), 
 % The directory does not exist
 Status = -1;
  
else,
  
  %% Check to see if the directory to be created exists as a
  %% directory or file
  Files = dir(DirName);
  if any(strcmp({Files.name},NewDirName)),
    if ~any(strcmp({Files([Files.isdir]).name},NewDirName)),
      Status=-2;
    else,
      Status = 2;
    end
  end
  
end % if ~exist

% if Status is 1 then everything is good up to this point.
if Status == 1,
  c=computer;
  if isunix | isvms,
    [Status, result] = unix(['mkdir ' fullfile(DirName,NewDirName)]);
    
  elseif strncmp(c,'PC',2),
% This is to check and see if the dos command is working.  In Win95
% if the current directory is a deeply nested directory or sometimes
% for TAS served file systems, the output pipe does not work.  The 
% solution is to make the current directory safe, %windir% and put it back
% when we are done.  The test is the cd command, which should always
% return something.
	[Status, result] = dos('cd');
	if isempty(result)
		OldDir = pwd;
		cd(getenv('windir'))
	else
		OldDir = [];
	end
    [Status, result] = dos(['mkdir "' fullfile(DirName,NewDirName) '"']);
	if ~isempty(OldDir)
	    cd(OldDir);
	end
    
  elseif strncmp(c,'MAC',3),
    Status = 1-mkfldr(DirName,NewDirName);
    
  end % if computer type
  
  % Need to flip Status of 0 and 1 because they mean the opposite
  % thing for the output of this function.
  Status=(Status==0);
  
end % if Status == 1


% Check to see if output arguments are to be returned.  If an arg.
% is not returned then cause errors if necessary.
ErrMsg='';
switch Status,
  case -2,
    ErrMsg = ['Cannot make directory ' NewDirName ' because a file ' ...
	      'in ' DirName ' already exists by that name.'];
  case -1,
    ErrMsg = ['Cannot make directory ' NewDirName ' because ' DirName ...
	      ' does not exist.'];
      
  case 0,
    ErrMsg = ['Cannot make directory ' NewDirName '.'];
    
  case 2,
    ErrMsg = ['Directory or file ' NewDirName ' already exists in ' ...
	       DirName];
    
end % if Status checking
  
if nargout == 0,
  error(ErrMsg)
  
else,
  if Status==-1|Status==-2,Status=0;end
  varargout{1} = Status;
  varargout{2} = ErrMsg;
  
end % if nargout
