function [ matpath, matname, DeploymentName ]  = findmat_LRAUV( vehicle, varargin )

% This function returns .mat file paths found on smb://atlas.shore.mbari.org/LRAUV/
%
%
% INPUTS:
% vehicle: 'Tethys', 'Daphne', 'Makai'
% search term: string containing mission, log or file name
%              (e.g., '20130920_20131007')
%
% EXAMPLE:
% vehicle  = 'Tethys';
% searchterm  = '20130920_20131007';  % <- this is a deployment folder name
% searchterm  = '20130912T181327';    % <- this is a log folder name
% searchterm  = '201309121813_201309140344'; % <- this is a log file name
% searchterm  = []; % <- no search term (or leave blank)
%
% [ matpath ]  = findmat_LRAUV( vehicle, searchterm )
%
% Last modified Jan 02, 2015
% Ben Raanan, MBARI
%--------------------------------------------------------------------------

% check inputs
%--------------------------------------------------------------------------
assert(ischar(vehicle),'Input vehicle must be a string.')

if nargin>1
    insearch = varargin{1};
    if ~isempty(insearch)
        assert(ischar(insearch),'Search term must be a string.')
    end
    
    if nargin>2
        year = varargin{2};
        assert(isnumeric(year),'Input year must be numeric.')
        yr = ['y' num2str(year)];
    else
        year = str2double(insearch(1:4));
        yr = ['y' num2str(year)];
    end
end





% check if LRAUVmatFiles.mat exists
%---------------------------------------------------------------------
try
    load LRAUVmatFiles.mat  % generated by: matFilePaths_LRAUV.m
catch
    str='';
    prompt = 'Could not find file LRAUVmatFiles.mat. Crawl server for mat files? YES/NO [YES]:';
    while ~strcmpi(str,'NO') && ~strcmpi(str,'YES')
        str = input(prompt,'s');
        if isempty(str)
            str = 'YES';
        end
    end
    if strcmpi(str,'YES')
        
        display('matFilePaths_LRAUV is crawling server! May take a few minutes to run...')
        try
            matFilePaths_LRAUV( '/Volumes/LRAUV' );
        catch
            serverpath = uigetdir(pwd,'Select server folder: smb://atlas.shore.mbari.org/LRAUV/' );
            matFilePaths_LRAUV( serverpath );
        end
        load LRAUVmatFiles.mat
    elseif strcmpi(str,'NO')
        error('Breaking out of findmat_LRAUV');
    end
end



% check if LRAUVmatFiles.mat is outdated
%---------------------------------------------------------------------
d=dir(which('LRAUVmatFiles.mat'));
if (datenum(clock)-d.datenum)>7
    str='';
    prompt = ['Mat file record is ' num2str((datenum(clock)-d.datenum),2)...
        ' days old. Crawl server for mat files? YES/NO [YES]:'];
    while ~strcmpi(str,'NO') && ~strcmpi(str,'YES')
        str = input(prompt,'s');
        if isempty(str)
            str = 'YES';
        end
    end
    if strcmpi(str,'YES')
        display('matFilePaths_LRAUV is crawling server! May take a few minutes to run...')
        serverpath = uigetdir(pwd,'Select server folder: atlas/LRAUV' );
        matFilePaths_LRAUV( serverpath );
        load LRAUVmatFiles.mat
    elseif strcmpi(str,'NO')
        load LRAUVmatFiles.mat
    end
end




list = LRAUVmatFiles.(vehicle).matFilePaths.(yr);
listf= LRAUVmatFiles.(vehicle).matFileNames.(yr);

% find .mat files and log paths
%--------------------------------------------------------------------------
if ~isempty(insearch)
    f=~cellfun('isempty',(strfind(list,insearch)));
    [f,~] = find(f);
    if any(f)
        DeploymentName = unique(list(f,1));
        matpath = list(f,2);
        matname = listf(f);
    else
        error(['Your search - ' insearch ' - did not match any .mat files or paths'])
    end
else
    matpath = list(:,2);
    matname = listf;
end
end
