function rm_cvs_dir(path_in, rm_file_dir)
% RM_CVS_DIR Remove recursive directories or files in the search directory
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: RM_CVS_DIR Remove recursive the the handover
%                         directories or files in the search directory
%
% CALL: rm_cvs_dir(toolbox,                 'CVS');
%       rm_cvs_dir(toolbox,                 '*.m~');
%       rm_cvs_dir(toolbox,                 '*.o');
%       rm_cvs_dir('/home/diepholz/matlab', 'CVS');
%
% REMARK: rm_cvs_dir(toolbox, 'CVS'); This command will remove the file 'CVS'
%                                     as well as the directory 'CVS'
%
%
% HISTORY: 24-05-2007 Diepholz
%             Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Replace the regular expression '*' with '\w*'
rm_file = strrep(rm_file_dir, '*', '\w*');
rm_file = strcat('^', rm_file);

files = dir(path_in);

%% Go through the files in the current directory
for i = 1:length(files)

  if files(i).isdir == 1

    %% Delete a directory if case sensitive matchs
    if (strcmp (files(i).name, rm_file_dir))
      rm_dir = fullfile(path_in, rm_file_dir);

      try
        rmdir (rm_dir, 's');
      catch
        error (sprintf('### rm_cvs_dir: Can not delete the directory %s',rm_dir ));
      end

    %% Go into the subdirectory
    elseif  ~(strcmp (files(i).name(1), '.'))
      new_dir = fullfile(path_in, files(i).name);
      rm_cvs_dir(new_dir, rm_file_dir);
    end

  else %% the object is a file

    match = regexp(files(i).name, rm_file, 'match');

    if ~isempty(match);
      remove_file = fullfile(path_in, files(i).name);
      try
        delete (remove_file);
      catch
        error (sprintf('### rm_cvs_dir: Can not delete the file %s', remove_File));
      end
    end

  end

end

