% 2nd level GLM
% 2nd level GLM
% RFX analysis

% load subject etc details
global defaults;
spm_defaults;

disp('Congratuations! Ready to perform group analysis!');
%2nd level-analisis dir
org_dir=pwd;
% group_dir = PAR.groupdir;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usegment
group_dir = fullfile(PAR.root,PAR.STATSDIR);
if ~exist(group_dir)
    %mkdir(group_dir);
    mdir=['!mkdir ' group_dir];
    eval(mdir);
end
cd(group_dir);
spm_unlink(fullfile('.', 'SPM.mat'));
% get the spatially warped jd images
ST=1;   % right now we only consider the first scan
imgs=[];
fields=[];
jdfiles=[];
for sb=1:PAR.nsubs
    for s=1%:PAR.subs(sb).nsess   % avg image is saved in the folder of the first session
        uf=spm_select('FPList', PAR.subs(sb).ses(s).structdir{ST}, ['^swjdc1avg\.nii']);
        if isempty(uf)
            fprintf('No spatially normalized jd image found for %s \n', PAR.subs(sb).ses(s).structdir{ST});
            fprintf('\t We will skip this subject.\n');
            continue;
        end
    end
end

nscan=size(P,1);
i1=1:nscan;
i1=i1';
I=[i1 ones(nscan,1) ones(nscan,1) ones(nscan,1)];
defaults.modality='FMRI';
spm_glm_2nd(P,I);
load SPM.mat
% Estimate parameters
spm_unlink(fullfile('.', 'mask.img')); % avoid overwrite dialog

SPM = spm_spm(SPM);

load('SPM.mat');
% now put T contrast per row into SPM structure
if (isempty(SPM.xCon))                 % setting for spm5
    xcon_1 = 1;
    SPM.xCon=spm_FcUtil('Set',...
        'rfx',...
        'T',...
        'c',...
        1, ...
        SPM.xX.xKXs);
else
    xcon_1 = length(SPM.xCon)+1;
    SPM.xCon(end + 1)= spm_FcUtil('Set',...
        'rfx',...
        'T',...
        'c',...
        1, ...
        SPM.xX.xKXs);

end
spm_contrasts(SPM, xcon_1:xcon_1);
