function icatb_ttest2_maps(c, files1, files2, outputDir, group1Name, group2Name, explicit_mask)
% Function to do two sample t-test on the selected files using SPM5
% functions.
%
% Inputs:
% 1. c - Data structure containing defaults for factorial design
% 2. files1 - Input files for group1
% 3. files2 - Input files for group2
% 4. outputDir - Output directory
% 5. group1Name - Group 1 name
% 6. group2Name - Group 2 name
% 7. explicit_mask - Explicit mask 

spm_defaults;

global defaults;

if ~exist('outputDir', 'var')
    outputDir = pwd;
end

if ~exist('group1Name', 'var')
    group1Name = 'group 1';
end

if ~exist('group2Name', 'var')
    group2Name = 'group 2';
end

cd(outputDir);

spmMATFile = fullfile(outputDir, 'SPM.mat');

try
    if exist(spmMATFile, 'file')
        delete(spmMATFile);
    end
catch
end

defaults.modality = 'FMRI';

% Two sample t-test selection
c.val{1}.val{1}.val{1}.val{1} = c.val{1}.val{1}.val{1}.values{2};

% Group1 files
c.val{1}.val{1}.val{1}.val{1}.val{1}.val{1} = cellstr(files1);

% Group2 files
c.val{1}.val{1}.val{1}.val{1}.val{2}.val{1} = cellstr(files2);

% Explicit mask
c.val{1}.val{1}.val{3}.val{end}.val{1} = cellstr(explicit_mask);

% Output directory
c.val{1}.val{1}.val{end}.val{1} = {outputDir};

% Get jobs for running batch analysis
[c1, jobs] = spm_jobman('harvest', c);

% Run job
spm_jobman('run', jobs);

% Load SPM.mat
spmInfo = load(spmMATFile);

% Estimate parameters
spmInfo.SPM = spm_spm(spmInfo.SPM);

% T-map contrast
spmInfo.SPM.xCon = spm_FcUtil('Set', [group1Name, ' - ', group2Name], 'T', 'c', [1;-1], spmInfo.SPM.xX.xKXs);

% SPM Contrasts
spm_contrasts(spmInfo.SPM);
