function [adi_dir,iif]=adi_dir_find(dir_name,ii_stop,ii_cur);
%
% Call [adi_dir,iif]=adi_dir_find(dir_name,ii_stop,ii_cur);
%
% ii_cur	= 0 to avoid looking in current directory (kluge for UNIX,
%			  where Waveforms directory found where it's not really there);
%
% Looks for directory name dir_name in current dir and in dirs
% parallel to current dir and returns path name to dir in adi_dir.
% If unsuccesful, adi_dir is returned empty, and if ii_stop=1, 
% program execution is stopped.
% FIX: does not search in current dir because if you are in the
% directory it comes back the same as if there is that directory in
% the current one.
%
% Example call:
%
% adi_dir=adi_dir_find('ADI',1);
% fopen([adi_dir,adi_root,'_adi']);
%
if nargin<2, ii_stop=1; end
if nargin<3, ii_cur=1; end
%
adi_dir=[];
if isunix, dx='/'; elseif isppc, dx=':'; else, dx='\'; end
%
iif=0;
% New: make adi_dir the whole path, including the final separator:
cur_dir=pwd;
if cur_dir(end)~=dx, cur_dir=[cur_dir,dx]; end
lcur=length(cur_dir);

ii=findstr(cur_dir,dir_name);
if ~isempty(ii),
	if ii(end)==lcur-3,
		iif=1; adi_dir=cur_dir;
	end
end
%
if exist([cur_dir,dir_name],'dir'),
%%	disp('Found ADI in pwd: '), cur_dir
	adi_dir=[cur_dir,dir_name,dx];
	iif=1;
else
	cc=findstr(cur_dir,dx);
	lwd=length(cur_dir); lcc=length(cc)-1;
	up_dir=[cur_dir(1:cc(max(1,lcc))),dir_name,dx];
	if exist(up_dir,'dir'), adi_dir=up_dir; iif=1; end
end
%
if ii_stop & ~iif,
	error(['CANNOT FIND ',dir_name,' IN LOCAL, OR ''../'' DIRECTORIES.']);
end
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
