function varargout=dopBehChk(fileNameLoc,varargin)
% dopOSCCI Mark II: dopBehChk ~ 19-11-2010 (last edit)
%
% check behavioural information:
% - read file name
%
% outputs:
% - behNames=beh.file.varNames;
% - fileReport=beh.file.report;
% - n=beh.file.n;
% - nVar=beh.file.nVar;
% - data=beh.file.data;
% - levels=beh.file.levels;
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
fprintf('%s\n',['Running ',mfilename]);
try
    beh.out.okay=1;
    if iscell(fileNameLoc)
        fileNameLoc=fileNameLoc{1};
    end
    if exist(fileNameLoc,'file')==2
        %% setGetInputs
        inputs.varargin=varargin;
        inputs.defaults=struct('fileList',[],... % list of available files
            'dataDir',[]); % data directory to search in for available files
        beh=setGetInputsStruct(inputs);
        beh.translation='behavioural';
        beh.abbreviation='beh';
        beh.out.fileNameLoc=fileNameLoc;
        beh.out.okay=1;
        %% check inputted list
        % - check if exists
        beh.chkInList=[];
        if ~isempty(beh.fileList)
            for i=1:length(beh.fileList)
%                 disp(['In: ',beh.fileList{i}]);
                if exist(beh.fileList{i},'file')==2
                    beh.chkInList{end+1}=beh.fileList{i};
                elseif ~isempty(beh.dataDir) && ...
                        exist([beh.dataDir,beh.fileList{i}],'file')==2
                    % make sure it's got a location
                    beh.chkInList{end+1}=[beh.dataDir,beh.fileList{i}];
                end
            end
            beh.out.inListReport=sprintf('%s\n',...
                [num2str(length(beh.fileList)),' of ',...
                num2str(length(beh.chkInList)), ' input files exist.']);
            disp(beh.out.inListReport);
        end
        
        %% read behavioural file
        beh.file=dopBehFileRead(beh.out.fileNameLoc);

        if beh.file.okay
            
            %% check session list available
            beh.chkBehList=[];
            for i=1:length(beh.file.list)
                %                 disp(['Ses: ',beh.file.list{i}]);
                [beh.tmp.d beh.tmp.n beh.tmp.e] = fileparts(beh.file.list{i});
                if exist(beh.file.list{i},'file')==2 && ~isempty(beh.tmp.d) % needs to have a directory with the name
                    %                 if exist(beh.file.list{i},'file')==2
                    beh.chkBehList{end+1}=beh.file.list{i};
                elseif ~isempty(beh.dataDir) && ...
                        exist([beh.dataDir,beh.file.list{i}],'file')==2
                    % make sure it's got a location
                    beh.chkBehList{end+1}=[beh.dataDir,beh.file.list{i}];
                end
            end
            beh.out.behListReport=sprintf('%s\n',...
                [num2str(length(beh.file.list)),' of ',...
                num2str(length(beh.chkBehList)), ' behavioural files exist.']);
            disp(beh.out.behListReport);
            
            if ~isempty(beh.chkInList) && ~isempty(beh.chkBehList)
                %% cross check list
                beh.chkCrossList=[];
                beh.out.noMatch=0;
                % check available list with beh.file.list
                for i=1:length(beh.chkInList) % beh.chkBehList
                    beh.fileMatch(i,:)=[0 0 0];
                    beh.ifnl=beh.chkInList{i}; % in file name loc
                    [beh.ifd beh.ifn beh.ifx]=fileparts(beh.ifnl);
                    j=0;
                    while j<numel(beh.chkBehList) && sum(beh.fileMatch(i,:))~=3; j=j+1;
                        beh.bfnl=beh.chkBehList{j}; % beh file name loc
                    [beh.bfd beh.bfn beh.bfx]=fileparts(beh.bfnl);
                        % check directory
                        if strcmp(beh.bfd,beh.ifd)
                            beh.fileMatch(i,1)=1;
                        end
                        % check name
                        if strcmp(beh.bfn,beh.ifn)
                            beh.fileMatch(i,2)=1;
                        end
                        % check extension
                        if strcmp(beh.bfx,beh.ifx)
                            beh.fileMatch(i,3)=1;
                        end
                    end
                    if sum(beh.fileMatch(i,:))==3
                        % reorder list based upon session infor
                        beh.chkCrossList{end+1}=beh.chkInList{i};
                    else
                        beh.out.noMatch=beh.out.noMatch+1;
                    end
                end
                beh.out.xListReport=sprintf('%s\n',...
                [num2str(length(beh.chkCrossList)),' of ',...
                num2str(length(beh.chkBehList)), ' cross-check files exist.']);
            % found cross list number of beh list
            end
            %% which list to output?
            % it's important that the behavioural list remains the same -
            % can't adjust list based upon this otherwise behavioural
            % matching doesn't work.
            if isfield(beh,'chkBehList')
                 beh.out.fileList=beh.chkBehList;
            else
                beh.out.okay=0;
            end
%                 beh.out.fileList=beh.chkCrossList;
            % old > 
%             if isfield(beh,'chkCrossList')
%                 beh.out.fileList=beh.chkCrossList;
%             elseif or(~isempty(beh.chkInList) && ~isempty(beh.chkBehList) && ...
%                     length(beh.chkCrossList)==length(beh.chkBehList),...
%                     isempty(beh.chkInList) && ~isempty(beh.chkBehList))
%                 % lists are the same or no in list
%                 beh.out.fileList=beh.chkBehList;
%             elseif isempty(beh.chkBehList) && ~ isempty(beh.chkInList)
%                 beh.out.fileList=beh.chkInList;
%             else % we have some issues
%                 beh.out.okay=0;
%             end
            %% pass file info to out structure
                if beh.out.okay
                    beh.out.behNames=beh.file.varNames;
                    beh.out.report=beh.file.report;
                    beh.out.guiReport=beh.file.guiReport;
                    beh.out.n=beh.file.n;
                    beh.out.nVar=beh.file.nVar;
                    beh.out.data=beh.file.data;
                    beh.out.levels=beh.file.levels;
                end
        else
            beh.out.okay=0;
        end
    else
        fprintf('%s\n\t%s\n\n%s\n','Behavioural file was NOT found:',...
            fileNameLoc,'Behaviorual info can not be returned.');
        beh.out.okay=0;
    end
    varargout{1}=beh.out;
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,beh,mfilename);
end