function scanpath(varargin)
%  1.  NAME
%
%  scanpath - Update Matlab path with a new search directory
%
%  2.  SYNOPSIS
%
%  scanpath(dir, flag )
%
%  3.  DESCRIPTION
%
%  Update the Matlab path variable to access the m-files in a new
%  subdirectory. Because some of the additionnal commands may have the
%  same name as those for editing profiles, the path to the CODAS edit m-
%  files is removed.
%
%  4.  PARAMETERS
%
%     dir optional
%        String containing the directory to add or remove from the Matlab
%        search path. Default is the current directory. The value of
%        CODASHOME directory is automatically prefixed to the given
%        string.
%
%     flag optional
%        Flag defining which action to perform: 0 will remove the
%        specified directory,1 will insert it. Default is 1.
%
%  5.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

% ------------------------------------------------------------------
% Directory separation: 
% 
%    Change if necessary.

dirsep = '/';

% ------------------------------------------------------------------

%--- Default values:
scan_dir = '.';
set_scan = 1;

%--- Read arguments:
if nargin > 0,
  if ischar(varargin{1}),
	 scan_dir = varargin{1};
  else
	 set_scan = varargin{1};
  end
end
if nargin > 1,
  set_scan = varargin{2};
end
  
%--- Build path to edit m-files:
codas_path = which('harmcrv');
if length(codas_path) == 0,
  error(...
		'Cannot find path to CODAS matlab scripts. Check your PATH variable.');
end
l = length(codas_path);
codas_path((l-8):l) = [];
edit_path = [codas_path dirsep 'edit'];

%--- Build path to scan m-files:
gfi_path = which('codasgfi');
if length(gfi_path) == 0,
  error(...
		'Cannot find path to GFI matlab scripts. Check your PATH variable.');
end
l = length(gfi_path);
gfi_path((l-10):l) = [];
scan_path = [gfi_path dirsep scan_dir];

%--- Actions according to input arguments:

if set_scan == 1,
  if inpath(edit_path),
	 rmpath(edit_path);
  end
  addpath(scan_path, '-begin');
else
  switch set_scan
	 case 0,
		if inpath(scan_path),
		  rmpath(scan_path);
		end
  end
end

return;
