close all
clear all

path(path,'../../ShipBoardSoftware/V8');

dpath = '/mbari/ProjectLibrary/707976 EPBuoyPrototypeAndTesting/Deployments/Fall2014/134.89.12.189/data/';
results_path = '/mbari/ProjectLibrary/707976 EPBuoyPrototypeAndTesting/Deployments/Fall2014/DecodedResults/'
ftp_path = '/mbari/FTP/pub/hamilton/';


while(1)
    
  dataDirs = dir([dpath 'R*']);
  for k = length(dataDirs):-1:1
    if(dataDirs(k).isdir == 1)
       path = [dpath dataDirs(k).name '/'];
       files = dir([path 'D*']);
       files
 
 
       for i = length(files):-1:max(1,length(files)-100)  %To keep from re-checking the same files forever.  Needs to be improved to re-read the D* files every time through.
         filename = files(i).name
         decoded_files = dir([results_path dataDirs(k).name '/' filename '.mat'])
    
         if((length(decoded_files) == 0)  || (decoded_files.datenum < files(i).datenum+(5/(24*60))))  %Decode again if within 5 minutes of raw file date
           if(~exist(['DecodedResults/' dataDirs(k).name]))
             cmd = ['!mkdir DecodedResults/' dataDirs(k).name];
             eval(cmd);
           end
          % if(~exist([ftp_path 'DecodedResults/' dataDirs(k).name]))
          %   cmd = ['!mkdir ' ftp_path 'DecodedResults/' dataDirs(k).name];
          %   eval(cmd);
         %  end
           SurfaceLogFileDecoderRing(path,filename,[results_path dataDirs(k).name '/'])
        %   cmd = ['!cp "' results_path dataDirs(k).name '/' filename '.mat" ' ftp_path 'DecodedResults/' dataDirs(k).name '/.' ]
         %  eval(cmd);
           MakePlots
         end
       end

    end
  end
  pause(60)

end
