function [ LRAUVmatFiles ] = matFilePaths_LRAUV( serverpath,varargin )

% This function locates .mat files from server folders and logs their paths
% Last modified Dec. 22, 2014
% Ben Raanan, MBARI

% INPUTS:
% serverpath = '/Volumes/LRAUV';
outputfolder = ([fileparts(which('matFilePaths_LRAUV.m')) filesep 'mat' filesep]);
vh = { 'Tethys', 'Daphne', 'Makai'} ; % (e.g., 'Tethys','Daphne','Makai')
%--------------------------------------------------------------------------

% Check server connection
%------------------------------------------------------------------
if ~checkServerConnection( serverpath )
    error('matFilePaths_LRAUV: Check server connection!')
end

% Make output folder if needed
%------------------------------------------------------------------
if ~exist(outputfolder,'dir')
    mkdir(outputfolder)
end

% Load old scans
%------------------------------------------------------------------
try
    load([outputfolder 'LRAUVmatFiles.mat']);
    oldscan = LRAUVmatFiles;
catch
    oldscan = [];
    disp('Couldnt find LRAUVmatFiles.mat: Performing full scan (crawling the server may take a few minutes to run)...')
end


% Force fresh scan
if any(strcmpi(varargin,'fresh'))
    oldscan = [];
    disp('matFilePaths_LRAUV is crawling server! May take a few minutes to run...')
end

% define nonrelevent directories
ignore  = {' ';'.';'..';'latest';'Lab';'Tank';'Tow';'Battery';'tmp';'devel'};


for v=1:numel(vh)
    
    
    vhFolder = [serverpath filesep vh{v} filesep 'missionlogs' filesep];
    % find folder subdirectories
    %------------------------------------------------------------------
    d = dir(vhFolder);
    isub = [d(:).isdir]; %# returns logical vector
    yrFolds = {d(isub).name}';
    % eliminate other nonrelevent directories
    [~,ci] = ispresent(yrFolds,ignore);
    yrFolds = yrFolds(~ci);
    clear ci
    
    for q=1:numel(yrFolds)
        
        % Server path structure
        pathFolder = [vhFolder yrFolds{q} filesep];
        
        
        % find folder subdirectories
        %------------------------------------------------------------------
        d = dir(pathFolder);
        isub = [d(:).isdir]; %# returns logical vector
        nameFolds = {d(isub).name}';
        
        % eliminate other nonrelevent directories
        [~,ci] = ispresent(nameFolds,ignore);
        nameFolds = nameFolds(~ci);
        clear ci
        
        % create feilds for new year if needed
        if ~isempty(oldscan) && ~isfield(oldscan.(vh{v}).logName,(['y' yrFolds{q}]))
            oldscan.(vh{v}).logName.(['y' yrFolds{q}])      = [];
            oldscan.(vh{v}).filePath.(['y' yrFolds{q}])     = [];
            oldscan.(vh{v}).matFilePaths.(['y' yrFolds{q}]) = [];
            oldscan.(vh{v}).matFileNames.(['y' yrFolds{q}]) = [];
        end
        
        % GET .MAT FILES FROM SERVER
        %------------------------------------------------------------------
        fName = cell(1);
        logs  = cell(1,3);
        matpath = cell(1,2);
        matname = cell(1); matCount = 0;
        if any(~cellfun('isempty',nameFolds))
            
            % compare to older scans and eliminate nonrelevent directories
            %------------------------------------------------------------------
            if ~isempty(oldscan) && ~isempty(oldscan.(vh{v}).logName.(['y' yrFolds{q}]))                
                oldpaths = oldscan.(vh{v}).logName.(['y' yrFolds{q}])(:,1);
                oldpaths = unique(oldpaths);
                
                nameFolds = setdiff(nameFolds,oldpaths);
                clear oldpaths           
            end
            
            
            for j=1:numel(nameFolds)
                
                % folder path
                tmp = [pathFolder nameFolds{j} filesep];
                
                % readin .dlist
                mname = cell(1);
                fileID = fopen([pathFolder nameFolds{j} '.dlist']);
                if fileID~=-1
                    txt = textscan(fileID,'%s','Delimiter', '\n');
                    dlist = txt{1,1};
                    
                    if ~isempty(dlist)
                        ci = ~(cellfun('isempty', strfind(dlist,'Deployment Name:')));
                        mname = char(dlist(ci,1));
                        mname = mname(20:end);
                    end
                    fclose(fileID);
                end; clear fileID txt dlist ci
                
                
                % find folder subdirectories
                d = dir(tmp);
                isub = [d(:).isdir];       %# returns logical vector
                X = {d(isub).name}';
                
                % eliminate nonrelevent directories
                ci = ~cellfun('isempty', strfind(X,yrFolds{q}));
                X = X(ci);
                
                % log names
                in = size(fName,1)+1;
                ind = in:length(X)+in-1;
                logs(ind,2) = X;
                
                % concatanate to form .mat file paths
                for k=1:length(ind)
                    logs{ind(k),1} = nameFolds{j};
                    fName{ind(k),:} = strcat(tmp,X{k},filesep);
                    
                    % locate relevant .mat files within each log
                    w = what(fName{ind(k)});                    
                    m1 = ~cellfun('isempty',regexp(w.mat, ['^' yrFolds{q}]));
                    m2 = cellfun('length', w.mat)==29; % make sure its the full dataset
                    logs{ind(k),3} = w.mat(m1 & m2);   % and not science_...
                    
                    
                    % log relevant .mat files paths and names
                    for m = 1:numel(logs{ind(k),3})
                        matCount = matCount+1;
                        tmpmat=logs{ind(k),3};
                        matpath{matCount,1} = mname;
                        matpath{matCount,2} = strcat(fName{ind(k),:},tmpmat{m});
                        matname{matCount,:} = tmpmat{m};
                    end; clear m
                end; clear k;
            end; clear j in ind isub d ci X;
            fName(1) =[];
            logs(1,:)=[];
        end
        
        % Reorganize to output struct
        %------------------------------------------------------------------
        if any(~cellfun('isempty',fName))
            if ~isempty(oldscan) && ~isempty(oldscan.(vh{v}).filePath.(['y' yrFolds{q}]))
                LRAUVmatFiles.(vh{v}).filePath.(['y' yrFolds{q}]) = [LRAUVmatFiles.(vh{v}).filePath.(['y' yrFolds{q}]); fName];
            else
                LRAUVmatFiles.(vh{v}).filePath.(['y' yrFolds{q}]) = fName;
            end
        end
        if any(any(~cellfun('isempty',logs)))
            if ~isempty(oldscan) && ~isempty(oldscan.(vh{v}).logName.(['y' yrFolds{q}]))
                LRAUVmatFiles.(vh{v}).logName.(['y' yrFolds{q}]) = [LRAUVmatFiles.(vh{v}).logName.(['y' yrFolds{q}]); logs];
            else
                LRAUVmatFiles.(vh{v}).logName.(['y' yrFolds{q}]) = logs;
            end
        end
        if any(any(~cellfun('isempty',matpath)))
            if ~isempty(oldscan) && ~isempty(oldscan.(vh{v}).matFilePaths.(['y' yrFolds{q}]))
                LRAUVmatFiles.(vh{v}).matFilePaths.(['y' yrFolds{q}]) = [LRAUVmatFiles.(vh{v}).matFilePaths.(['y' yrFolds{q}]); matpath];
            else
                LRAUVmatFiles.(vh{v}).matFilePaths.(['y' yrFolds{q}]) = matpath;
            end
        end
        if any(~cellfun('isempty',matname))
            if ~isempty(oldscan) && ~isempty(oldscan.(vh{v}).matFileNames.(['y' yrFolds{q}]))
                LRAUVmatFiles.(vh{v}).matFileNames.(['y' yrFolds{q}]) = [LRAUVmatFiles.(vh{v}).matFileNames.(['y' yrFolds{q}]); matname];
            else
                LRAUVmatFiles.(vh{v}).matFileNames.(['y' yrFolds{q}]) = matname;
            end
        end
    end
end

% save
%
% uisave('LRAUVmatFiles','LRAUVmatFiles.mat')
save([outputfolder 'LRAUVmatFiles.mat'],...
    'LRAUVmatFiles');
%}
end
