function varargout=dopSesChk(fileNameLoc,varargin)
% dopOSCCI Mark II: dopSesChk ~ 16-11-2010 (last edit)
%
% check session information:
% - read file name
%
fprintf('%s\n',['Running ',mfilename]);
try
    ses.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
        ses=setGetInputsStruct(inputs);
        ses.translation='session';
        ses.abbreviation='ses';
        ses.out.fileNameLoc=fileNameLoc;
        ses.out.okay=1;
        %% check inputted list
        % - check if exists
        ses.chkInList=[];
        if ~isempty(ses.fileList)
            for i=1:length(ses.fileList)
%                 disp(['In: ',ses.fileList{i}]);
                if exist(ses.fileList{i},'file')==2
                    ses.chkInList{end+1}=ses.fileList{i};
                elseif ~isempty(ses.dataDir) && ...
                        exist([ses.dataDir,ses.fileList{i}],'file')==2
                    % make sure it's got a location
                    ses.chkInList{end+1}=[ses.dataDir,ses.fileList{i}];
                end
            end
            ses.out.inListReport=sprintf('%s\n',...
                [num2str(length(ses.fileList)),' of ',...
                num2str(length(ses.chkInList)), ' input files exist.']);
            disp(ses.out.inListReport);
        end
        %% read session file
        ses.file=dopSessionFileRead(ses.out.fileNameLoc);

        if ses.file.okay
            
            %% check session list available
            ses.chkSesList=[];
            for i=1:length(ses.file.list)
%                 disp(['Ses: ',ses.file.list{i}]);
                if exist(ses.file.list{i},'file')==2
                    ses.chkSesList{end+1}=ses.file.list{i};
                elseif ~isempty(ses.dataDir) && ...
                        exist([ses.dataDir,ses.file.list{i}],'file')==2
                    % make sure it's got a location
                    ses.chkSesList{end+1}=[ses.dataDir,ses.file.list{i}];
                end
            end
            ses.out.sesListReport=sprintf('%s\n',...
                [num2str(length(ses.file.list)),' of ',...
                num2str(length(ses.chkSesList)), ' session files exist.']);
            disp(ses.out.sesListReport);
            
            if ~isempty(ses.chkInList) && ~isempty(ses.chkSesList)
                %% cross check list
                ses.chkCrossList=[];
                ses.out.noMatch=0;
                % check available list with ses.file.list
                for i=1:length(ses.chkSesList)
                    ses.fileMatch(i,:)=[0 0 0];
                    ses.sfnl=ses.chkSesList{i}; % session file name loc
                    [ses.sfd ses.sfn ses.sfx]=fileparts(ses.sfnl);
                    j=0;
                    while j<length(ses.chkSesList) && sum(ses.fileMatch(i,:))~=3; j=j+1;
                        ses.ifnl=ses.chkInList{j}; % in file name loc
                        [ses.ifd ses.ifn ses.ifx]=fileparts(ses.ifnl);
                        % check directory
                        if strcmp(ses.sfd,ses.ifd)
                            ses.fileMatch(i,1)=1;
                        end
                        % check name
                        if strcmp(ses.sfn,ses.ifn)
                            ses.fileMatch(i,2)=1;
                        end
                        % check extension
                        if strcmp(ses.sfx,ses.ifx)
                            ses.fileMatch(i,3)=1;
                        end
                    end
                    if sum(ses.fileMatch(i,:))==3
                        % reorder list based upon session infor
                        ses.chkCrossList{end+1}=ses.fileList{j};
                    else
                        ses.out.noMatch=ses.out.noMatch+1;
                    end
                end
                ses.out.xListReport=sprintf('%s\n',...
                [num2str(length(ses.chkCrossList)),' of ',...
                num2str(length(ses.chkSesList)), ' cross-check files exist.']);
            end
            %% which list to output?
            if or(~isempty(ses.chkInList) && ~isempty(ses.chkSesList) && ...
                    length(ses.chkCrossList)==length(ses.chkSesList),...
                    isempty(ses.chkInList) && ~isempty(ses.chkSesList))
                % lists are the same or no in list
                ses.out.fileList=ses.chkSesList;
            elseif isempty(ses.chkSesList) && ~ isempty(ses.chkInList)
                ses.out.fileList=ses.chkInList;
            else % we have some issues
                ses.out.okay=0;
            end
            %% pass file info to out structure
                if ses.out.okay
                    ses.out.sesNames=ses.file.sesNames;
                    ses.out.indFiles=ses.file.ind;
                    ses.out.report=ses.file.report;
                    ses.out.guiReport=ses.file.guiReport;
                    ses.out.n=ses.file.n;
                    ses.out.nSes=ses.file.nSes;
                end
        else
            ses.out.okay=0;
        end
    else
        fprintf('%s\n\t%s\n\n%s\n','Session file was NOT found:',...
            fileNameLoc,'Session summary will not be run.');
        ses.out.okay=0;
    end
    varargout{1}=ses.out;
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,ses,mfilename);
end