% find_syslog.m
% Last modified Sep. 15, 2014
% Ben Raanan

% This script locates syslog files from server folders and logs thier paths

% clear

global yr

for q=1:length(yr)
    
    year = num2str(yr(q));
    
    h = waitbar(0,['Retreving ' year ' syslogs...']);
    clear filePath mission logName nameFolds logs fName 
    
    pathFolder = ['/Volumes/900739.LRAUV.data/Testing/' year '/'];
    
    % find folder subdirectories
    d = dir(pathFolder);
    isub = [d(:).isdir]; % returns logical vector
    nameFolds = {d(isub).name}';
    
    % eliminate nonrelevent directories
    ignore  = {' ','.','..','latest','Lab','Tank','Tow','Battery'};
    for c=1:length(ignore)
        ci = (cellfun('isempty', strfind(nameFolds,ignore{c})));
        nameFolds = nameFolds(ci);
    end; clear c ci
    
    % log mission names for each vehicle
    b = not(cellfun('isempty', regexpi(nameFolds,'daphne')));
    c = not(cellfun('isempty', regexpi(nameFolds,'makai'))); 
    a = ~(b | c);   % not(cellfun('isempty', regexpi(nameFolds,'tethys'))); 
    
    
    % add 'M' to begining of mission name (struct can't start w/ num)
    for k=1:length(nameFolds)
        nf{k,:} = ['M' nameFolds{k}];
    end; clear k
    
    mission.all     = nameFolds;
    mission.Tethys  = nameFolds(a,:);
    mission.Daphne  = nameFolds(b,:);
    mission.Makai   = nameFolds(c,:);
    
    
    fName = cell(1);
    logs  = cell(1,2);
    missLog = cell(1);
    for j=1:length(nameFolds)
        
        % folder path
        tmp = [pathFolder nameFolds{j} '/Logs/'];
        
        % find folder subdirectories
        d = dir(tmp);
        isub = [d(:).isdir];       % returns logical vector
        X = {d(isub).name}';
        
        % eliminate nonrelevent directories
        ci = not(cellfun('isempty', strfind(X,year)));
        X = X(ci);
        
        % log names
        in = size(fName,1)+1;
        ind = in:length(X)+in-1;
        logs(ind,1) = X;
        
        % concatanate to form syslog file paths
        for k=1:length(ind)
            logs{ind(k),2} = nameFolds{j};
            fName{ind(k),:} = strcat(tmp,X{k},'/syslog');
        end; clear k;
        
        waitbar( j./length(nameFolds),h,['Retreving ' year ' syslogs ['...
                num2str(100*(j./length(nameFolds)),2) '%]'] );
            
    end; clear j in ind isub d ci X;
    fName(1)=[];
    logs(1,:) =[];
    
    % log syslog file paths and log names for each vehicle
    b = not(cellfun('isempty', regexpi(fName,'daphne')));
    c = not(cellfun('isempty', regexpi(fName,'makai'))); 
    a = ~(b | c);   % not(cellfun('isempty', regexpi(fName,'tethys'))); 
    
    filePath.all    = fName;
    filePath.Tethys = fName(a,:);
    filePath.Daphne = fName(b,:);
    filePath.Makai  = fName(c,:);
    
    logName.all     = logs;
    logName.Tethys  = logs(a,:);
    logName.Daphne  = logs(b,:);
    logName.Makai   = logs(c,:);
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' year '.mat'],...
        'filePath','mission','logName')
    %}
    close(h)
    display([datestr(clock) ': compleated syslog scan for ' year ])  
end
