function preppath(this_dir, dirs, subdirs)
% preppath(this_dir, dirs, subdirs)
% essentially does "addpath" to [this_dir '/' dirs] for each of the dirs
% if subdirs exists, assumes they are subdirectories of the first dirs{} entry
%
% "dirs" and "subdirs" are cellarrays
%
% eg: to prepend [path]/codas3 and [path]/codas3/edit
%  this_dir = fileparts(which(codas3));
%  dirs = {'codas3', 'misc'}
%  subdirs = {'edit'}
%  preppath(this_dir, dirs, subdirs);


if (~exist(this_dir, 'dir') | (nargin <2))
   help preppath
   error('incorrect arguments to preppath')
end

%%%%
for diri = 1:length(dirs)
   dirname = fullfile( this_dir, dirs{diri});
   if (exist(dirname, 'dir'))
      addpath( dirname)
      fprintf('adding %s to path\n', dirname)
   else
      fprintf('not adding %s to path\n', dirname)
   end
end


%%%%
if (nargin == 3)
   this_dir = fullfile(this_dir, dirs{1});
   for subdiri = 1:length(subdirs)
      subdirname = fullfile(this_dir, subdirs{subdiri});
      if (exist(subdirname, 'dir'))
         addpath(subdirname)
         fprintf('adding %s to path\n', subdirname)
      else
         fprintf('not adding %s to path\n', subdirname)
      end
   end
end


clear dirname  subdiri  subdirs  thisdir  diri dirs subdirname  this_dir    
clear this_dir dirs
