function varargout=dopGraphDataFileInfo(fileName)
% dopOSCCI Mark II: dopGraphDataFileInfo ~ 22-11-2010 (last edit)
%
% extract information about the graph data file from the name
%

fprintf('%s\n',['Running ',mfilename]);
try
    %% set inputs
    dgdf.fileName=fileName; clear fileName;
    dgdf.translation='dopOSCCI graph data file';
    dgdf.abbreviation='dgdf'; % hmm, cumbersome...
    dgdf.fileExt='.dat'; % this is what dopSaveGraphData saves to
    
    dgdf.search={'grp','ses','beh','Ep'}; % elements to search for
    dgdf.out.graphName=[];
    [dgdf.dir dgdf.fn dgdf.ext]=fileparts(dgdf.fileName);
    [dgdf.out.evName dgdf.rem]=strtok(dgdf.fn,'_');
%     i=0;
    while ~isempty(dgdf.rem); %i=i+1;
%         disp([num2str(i),': ',dgdf.rem])
        [dgdf.next dgdf.rem]=strtok(dgdf.rem,'_');
        
        dgdf.match=0;
        dgdf=checkTxt(dgdf);
        
%         [dgdf.next dgdf.rem]=strtok(dgdf.rem,'_');
        
    end
    % assume last bit to be Epoch selection info
%     dgdf=checkTxt(dgdf);
   fprintf('\t%s\n',dgdf.out.graphName);
    %% set out puts
    varargout{1}=dgdf.out;
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,dgdf,mfilename);
end
clear inputs dgdf
end

%% checkTxt
function dgdf=checkTxt(dgdf)
for i=1:numel(dgdf.search)
    if strfind(dgdf.next,dgdf.search{i})
        dgdf.match=1;
        switch dgdf.search{i}
            case {'grp','ses','beh'}
%                 disp(dgdf.search{i})
                j=length(dgdf.search{i})+1;
                dgdf.tnum=([dgdf.search{i},'Num']);
                dgdf.out.(dgdf.tnum)=dgdf.next(j);
                while isinteger(str2double([dgdf.out.(dgdf.tnum) dgdf.next(j+1)])); j=j+1;
                    dgdf.out.(dgdf.tnum)=[dgdf.out.(dgdf.tnum) dgdf.next(j)];
                end
                 dgdf.out.(dgdf.tnum)=str2double( dgdf.out.(dgdf.tnum));
                dgdf.out.([dgdf.search{i},'Name'])=dgdf.next(j+1:end);
                dgdf.out.graphName=[dgdf.out.graphName,' ',...
                    dgdf.out.([dgdf.search{i},'Name']),...
                    '(',dgdf.search{i},' ',num2str(dgdf.out.(dgdf.tnum)),') '];
            case 'Ep'
                dgdf.out.epSel=dgdf.next(1:strfind(dgdf.next,dgdf.search{i})-1);
                dgdf.out.graphName=[dgdf.out.graphName,' ',dgdf.out.epSel,...
                    ' epochs'];
        end
        break
    end
end
if ~dgdf.match
    fprintf('\t%s\n\t\t%s\n',...
        'Couldn''t recognise file info in:',dgdf.next);
end
end