function [list2,ind2] = recurseFolders(folder, list, ind, homog)
% This function recurses through a directory structure, looking for IR and
% Visible light images. It creates a list of their file locations (list2) 
% and an index (ind2) which it returns. Currently it only takes the first
% image pair within a folder but this can be changed by adjusting the limit
% variable

if nargin<4
    homog = maketform('projective',eye(3));
end

if isunix
    delim = '/';
else
    delim = '\';
end

% Load any homography in this folder and save it for each image in the
% folder
list_h = dir([folder delim 'homog*.mat']);
if ~isempty(list_h)
    load([folder delim list_h(1).name]);
end

list_curr = dir([folder delim '*']);
for j=1:length(list_curr)
    if (list_curr(j).isdir == 1 && ~strcmp(list_curr(j).name,'.') && ~strcmp(list_curr(j).name,'..'))
        [list ind] = recurseFolders([folder delim list_curr(j).name], list, ind, homog);
    end
end
list_currIR = dir([folder delim '*IR.bmp']);
%list_currIRA = dir([folder delim '*IRA.bmp']);
list_currVis = dir([folder delim '*Vis.bmp']);
limit = 1;
for i=1:min(limit,length(list_currIR))
    list(ind).imgIR = [folder delim list_currIR(i).name];%imread([folder delim list_currIR(i).name]);
%    list(ind).imgIRA = [folder delim list_currIRA(i).name];%imread([folder delim list_currIR(i).name]);
    list(ind).imgVis = [folder delim list_currVis(i).name];%imread([folder delim list_currVis(i).name]);
    list(ind).homog = homog;
    ind = ind + 1;
end
ind2 = ind;
list2 = list;
clear list;