% get_syslog_CRITICAL_LRAUV.m
% Last modified Dec 31, 2014
% Ben Raanan

% This script retreives and logs CRITICAL messeges from syslog files

clear

% workd = '~/Documents/MATLAB/MBARI/LoadAndFix/ServerSyslogs/mat/';
% yr=2010:2014;
% vh={'Tethys','Daphne','Makai'};

global yr vh workd

for v=1:length(vh)
    
    vhc = vh{v};
    
    h = waitbar(0,['Retreving ' vhc ' syslogs...']);
    
    load([workd 'syslog_' vhc '.mat'])
    syslog.nosyslog =cell(1);
    for q=1:length(yr)
        
        % check connection with server
        d = dir('/Volumes/LRAUV/');
        if isempty(d)
            close(h)
            error('find_syslog_LRAUV: Could not establish connection with server smb://atlas.shore.mbari.org/LRAUV/')
        end; clear d
        
        year = num2str(yr(q));
        yfield = ['y' year];
        
        waitbar(0.01,h,['Scaning ' year ' syslogs for ' (vh{v}) '...']);
        formatSpec = '%s';
        
        for j=1:numel(filePath.(yfield))
            
            ltmp = ['D' logName.(yfield){j,1}];
            mtmp = ['M' logName.(yfield){j,2}];
            ftmp = filePath.(yfield){j,1};
            
            syslog.(yfield).(mtmp).(ltmp).path = ftmp;
            
            % readin syslog to struct
            fileID = fopen([ftmp 'syslog']);
            
            if fileID~=-1
                txt = textscan(fileID,formatSpec,'Delimiter', '\n');
                txt = txt{1,1};
                if ~isempty(txt)
                    
                    
                    % index CRITICAL messages and extract them to struct
                    ci = (not(cellfun('isempty', strfind(txt,'CRITICAL'))));
                    syslog.(yfield).(mtmp).(ltmp).CRITICAL = txt(ci,1);
                    
                    % get first and last lines in syslogs for runtime calc
                    rtInd = find(not(cellfun(@isempty,txt)));
                    
                    % verify time stamp is complete
                    rtin=1; rtend=length(rtInd);
                    while cellfun('isempty', strfind(txt(rtInd(rtin)),'Z'))
                        rtin =rtin+1;
                    end
                    
                    while cellfun('isempty', strfind(txt(rtInd(rtend)),'Z'))
                        rtend =rtend-1;
                    end
                    
                    syslog.(yfield).(mtmp).(ltmp).runTime = txt([rtInd(rtin),rtInd(rtend)],1);
                    
                    
                    fclose(fileID);
                end
            else
                warning([ftmp ': Invalid file identifier'])
                syslog.nosyslog{length(syslog.nosyslog)+1,:}=ftmp;
                syslog.(yfield).(mtmp)=rmfield(syslog.(yfield).(mtmp),(ltmp));
                
                
            end
            clear txt ci fileID
            
            %     copyfile(['/Volumes' fileName{j,1}],[outf mission{j,1} '_' fields{j,1} '.syslog']);
            waitbar(j./length(filePath.(yfield)),h,['Scaning ' year ' syslogs for ' vh{v} ' ['...
                num2str(100*(j./length(filePath.(yfield))),2) '%]'] );
            
        end; clear j tmp;
        display([datestr(clock) ': retreived CRITICAL messeges from ' vhc ' ' year ' syslog records'])
        
        % save
        %{
        save([workd 'syslog_' vhc '.mat'],...
            'filePath','logName','syslog')
        %}
    end
    
    
    
    % save
    %
    save([workd 'syslog_' vhc '.mat'],...
        'filePath','logName','syslog')
    %}
    close(h)
    clearvars -except workd yr vh q v
end