function addpathx(basepath, recursive, position)
%ADDPATHX Recursive addpath

linux_suffix = '-linux-x64';
mac_suffix = '-mac-x64';

if(nargin < 2)
    recursive = true;
end

if(nargin < 3)
    position = '-begin';
end

if(iscell(basepath))
    basepath = cell2mat(basepath);
    if(~ischar(basepath))
        error('Path does not seem to be a string (or a string in a cell).');
    end
end
if(isempty(basepath))
    warning('Trying to add an empty path.');
    return;
end
if(unix(sprintf('test -d "%s"', basepath)))
    error('Directory "%s" does not exist.', basepath);
end



evalc('addpath(basepath, position)');

if(recursive)
    
    flisting = dir(basepath);
    dirnames = { flisting([flisting.isdir]).name };
    for i = 1 : length(dirnames)
        dirname = dirnames{i};
        if(strcmp('.', dirname) || strcmp('..', dirname) || length(dirname) >= 1 && (dirname(1) == '@' || dirname(1) == '-'))
            continue;
        end
        if(ismac && length(dirname) > length(linux_suffix) && strcmp(linux_suffix, dirname(end - length(linux_suffix) + 1 : end)) || ...
           ~ismac && isunix && length(dirname) > length(mac_suffix) && strcmp(mac_suffix, dirname(end - length(mac_suffix) + 1 : end)))
            continue;
        end
        dirpath = fullfile(basepath, dirname);
        addpathx(dirpath, recursive, position);
    end
end

end