function load_data_files (prefixes, suffixes, varargin)
% load_data_files ([prefixes[, suffixes[, options...]]])
% 
% Load the data files in the current directory (and subdirectories if
% the -subdirs option is set to true).
%
% This selects those files which start with one of the prefixes and end
% with one of the suffixes. (Note that prefixes and suffixes can both be
% either a single string, or a cell-array of strings.) It then loads those
% data files, with a name formed by a prefix (default 'data_') and then the
% file name, by default stripping away the prefix and suffix (but see the
% options below). An optional suffix can be added to the variable name.
%
% The default prefix is 'RandMut_', and the default suffixes are '_raw.dat'
% and '_10-EQU_binned.dat'.
%
% Options:
%   -subdirs: whether to go into subdirectories or not. (default: false)
%   - hack: whether to apply the hack that replaces every occurrence of 'mM' by 'uM'. (default: true)
%   - data-prefix: the prefix added to the variable name. (default: 'data_')
%   - data-suffix: the suffix added to the variable name. (default: '')
%   - keep-prefix: whether to keep the file prefix. (default: false)
%   - keep-suffix: whether to keep the file suffix. (default: false)
%   - load-function: which load data function to use. (default: load_data)

if ~exist ('prefixes', 'var')
    prefixes = 'RandMut_';
end

if ~exist ('suffixes', 'var')
    suffixes = {'_raw.dat', '_10-EQU_binned.dat'};
end

if ~iscell (prefixes)
    prefixes = {prefixes};
end

if ~iscell (suffixes)
    suffixes = {suffixes};
end

err_fname = mfilename;

% defaults
subdirs = false;
have_mm_hack = true;
data_prefix = 'data_';
data_suffix = '';
keep_prefix = false;
keep_suffix = false;
load_function= 'load_data';

% parse the options
n_varargs = length (varargin);
expect_arg = false;
assign_arg_to = '';
for i = 1:n_varargs
    arg = varargin{i};
    if ~expect_arg && ischar (arg) && arg (1) == '-'
        switch arg
            case '-subdirs', 
                expect_arg = true;
                assign_arg_to = 'subdirs';
            case '-hack',
                expect_arg = true;
                assign_arg_to = 'have_mm_hack';
            case '-data-prefix',
                expect_arg = true;
                assign_arg_to = 'data_prefix';
            case '-data-suffix',
                expect_arg = true;
                assign_arg_to = 'data_suffix';
            case '-keep-prefix',
                expect_arg = true;
                assign_arg_to = 'keep_prefix';
            case '-keep-suffix',
                expect_arg = true;
                assign_arg_to = 'keep_suffix';
            case '-load-function',
                expect_arg = true;
                assign_arg_to = 'load_function';
            otherwise, error ([err_fname ':unknownarg'], 'Unknown argument %s.', arg);
        end
    else
        if ~expect_arg
            error ([err_fname ':unexparg'], 'Unexpected argument at position %d in varargin.', i);
        end
        eval ([assign_arg_to ' = varargin{i};']);
        expect_arg = false;
    end
end

if expect_arg
    error ([err_fname ':noarg'], ['Required argument for ' arg ' missing.']);
end

flist = dir();
for i=1:length(flist)
    fname = flist(i).name;
    if ~flist(i).isdir
        load_file(fname, load_function, prefixes, suffixes, data_prefix, data_suffix, keep_prefix, keep_suffix, have_mm_hack);
    elseif subdirs && fname(1) ~= '.'
        disp(['Entering directory ', fname]);
        cd(fname);
        load_data_files (prefixes, suffixes, varargin{:});
        cd ..;
    end
end

end

function load_file (fname, load_function, prefixes, suffixes, data_prefix, data_suffix, keep_prefix, keep_suffix, have_mm_hack)

n_prefixes = length (prefixes);
n_suffixes = length (suffixes);

found = false;

for i = 1:n_prefixes
    prefix = prefixes{i};
    l_prefix = length (prefix);
    if strncmp (fname, prefix, l_prefix)
        if ~keep_prefix
            stripped_prefix = fname (l_prefix+1:end);
        else
            stripped_prefix = fname;
        end
        for j = 1:n_suffixes
            suffix = suffixes{j};
            l_suffix = length (suffix);
            if length (stripped_prefix) >= l_suffix && strcmp (stripped_prefix (end-l_suffix+1:end), suffix)
                if ~keep_suffix
                    stripped = stripped_prefix(1:end-l_suffix);
                else
                    stripped = stripped_prefix;
                end
                found = true;
                break;
            end
        end
        if found
            break;
        end
    end
end

if found
    if have_mm_hack
        places = strfind (stripped, 'mM');
        stripped (places) = 'u';
    end
    var_name = [data_prefix stripped data_suffix];
    disp (['Loading ' fname ' into ' var_name '...']);
    var_name
    evalin ('base', [var_name ' = ' load_function '(''' fname ''');']);
end

end