function vbm_custom2(varargin)
%VBM_custom for fluid resuts...
% this routine performs VBM preprocessing steps on a number of T1 images.
% The user is prompted for the images, as well as custom template and
% priors.  The normalization is performed (for now) either GM -> GM, or
% WM -> WM.  i.e., each subjects WM or GM is normalised to the WM or GM
% apriori image.
% Additionally, the resulting DCT deformation is applied to related images
% for each subject.  These images could be, e.g., Log(Jacobian) maps from a serial fluid
% warping for each subject.  These masks are normalized into [1 1 1] voxel
% size, with NN interpolation.... those settings could be changed if you
% look for them in the code.  
% SEE ALSO: vbm_custom, vbm_custom3
% USAGE: 
%   vbm_custom2  prompts user for all inputs.
%   vbm_custom2('sge')  runs over grid engine.
% Matt Senjem 

sge=0;
if nargin==1 & strcmpi(varargin{1},'sge')
  sge=1;
elseif nargin<=2
  templatedir=varargin{1}
  template_cell{1} = [templatedir,'/s8template.img'];
  template_cell{2} = [templatedir,'/s8gray.img'];
  template_cell{3} = [templatedir,'/s8white.img']; 
  template_cell{4} = [templatedir,'/s8csf.img'];
  if nargin==2
    numsubjects=varargin{2};
  end
elseif nargin==6 & sum(isstr(varargin))==4
  for i=1:4
    template_cell{i}=varargin{i};
  end
end  
if ~exist('stime','var')
    stime=[date,'_',num2str(now)];%stime = date;
end

Anatnames = spm_get(Inf,'*.IMAGE',{'Select Anat Images'});
Anatdir  = get_rootdir(Anatnames);
slash = findstr(filesep,Anatdir);
rootdir = Anatdir(1:slash(end)-1);
logJdir  = spm_input('LogJ rootdir?','+1','s',[rootdir,filesep,'LogJLinks']);
logJnames = find_AnatJacpairs(Anatnames,logJdir);


if ~exist('results_dir','var')
    results_dir = spm_input('Directory to save results in?','+1','s',[pwd,filesep,stime])
    cd('/')
    if ~exist(results_dir)
        mkdir(results_dir)
    end
    
end
disp(['pwd = ',pwd])
cd(results_dir)
numsubjects = length(Anatnames);
optimal = spm_input('Normalize to Whole Head/GM/WM ?',1,'m','Normalize to Whole Head|Normalize to GM prior|Normalize to WM prior',[0 1 2], 1);
sm = spm_input('FWHM(mm) smoothing [0=none]?','+1','w','12');
if ~exist('template_cell','var')
  template_cell{1} = spm_get(1,'s8template*.IMAGE','Select template for normalization'); 
  template_cell{2} = spm_get(1,'s8gray*.IMAGE','Select gray matter prior'); 
  template_cell{3} = spm_get(1,'s8white*.IMAGE','Select white matter prior'); 
  template_cell{4} = spm_get(1,'s8csf*.IMAGE','Select CSF prior'); 
end


%error('stopping here for now')

if sge
    sge_mls_fluid_prep2('norm2custom',template_cell,'smooth',sm,...
        'Anatnames',Anatnames,'logJnames',logJnames,...
        'results_dir',results_dir,'stime',stime,'optimal',optimal);
end
   
return

function logJnames = find_AnatJacpairs(Anatnames,logJdir)
subdirs = dir(logJdir);
logJnames = {};
for k=1:length(Anatnames)
    [pth,nam,ext] = fileparts(Anatnames{k});
    slash = findstr(filesep,pth);
    diagnosis = pth(slash(end)+1:end);
    for i=1:length(subdirs)
        if subdirs(i).isdir & strcmpi(diagnosis,subdirs(i).name)
            disp(['matching ',Anatnames{k}])
            disp(['scanning ',fullfile(logJdir,subdirs(i).name),' ... '])
            files = dir(fullfile(logJdir,subdirs(i).name,['*',nam,'*',ext]));
            if length(files)==1
                disp(['found: ',files(1).name])
                logJnames{k} = fullfile(logJdir,subdirs(i).name,files(1).name);
            else
                disp('no match found')
            end
        end
    end
end
return
    
    
        
