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

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