function status = touch(pth);
% touch(pth): 
%
% simulates unix touch: do nothing if path exists, create path if it doesn't.
%
% 10/03 ras.
status = 2;

callingDir = pwd;

if exist(pth,'file') | exist(pth,'dir')
	return
else
	[a{1} b] = fileparts(pth);

	% lame: fileparts behaves differently for diff. versions of matlab,
	% on Mac crapOS9 it's not recursive, must manually remove filesep 
	% char
	if isequal(a{1}(end),filesep)
		a{1} = a{1}(1:end-1);
	end
	
	while ~exist(a{end},'dir')
		if isequal(a{end}(end),filesep)
			a{end+1} = fileparts(a{end}(1:end-1));
		else
			a{end+1} = fileparts(a{end});
		end
	end

	for i = 1:length(a)
		if isequal(a{i}(end),filesep)
			a{i} = a{i}(1:end-1);
		end
	end
		
	for i = length(a)-1:-1:1
		[x y] = fileparts(a{i});
		cd(x);
		mkdir(y);
	end

	% check if last part of path is a file or a directory
	if isequal(b(end),filesep)  % pth is a directory
		cd(a{1});
		status = mkdir(b);
	else	
		if isempty(b)
			% this should only happen in mac crapOS9, but it means that
			% the path was a directory, and it has already been created
			% in the last part -- so do nothing		
		else 	% pth is a file
			cd(a{1});
			[fid status] = fopen(b,'w');
			fclose(fid);
		end
	end
end
		
cd(callingDir);

return
