function vbm_custom(varargin)
% vbm_custom.m: VBM preprocessing with custom template T1 or T2 images with SPM2.
% % vbm_custom.m: VBM preprocessing with custom template T1 or T2 images with SPM2.
% 
% Please note, with some setup work,
% this can be configured to run over Sun Grid Engine.  (SGE)
% By default SGE mode is disabled, but you can
% un-comment line 12 in vbm.m and line 19 in make_template.m 
% to allow SGE mode to be used. you will need to make some other changes in
% the code also.  Email me with any questions about SGE mode.
%
% SEE ALSO: vbm_ui.m
% Jan. 10, 2006
% Matthew L. Senjem
% Mayo Clinic 
% Senjem.Matthew1@mayo.edu
%%
% When using this code, please reference the following paper:
%
% Senjem ML, Gunter JL, Shiung MM, Petersen RC, Jack CR Jr., "Comparison of different methodological implementations of voxel-based
% morphometry in neurodegenerative disease." Neuroimage. 2005 Jun;26(2):600-8. 

sge=0;
if nargin==1 & strcmpi(varargin{1},'sge')
  sge=1;
elseif nargin & 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
spm_defaults;
global defaults
imgVols = spm_vol(spm_get(Inf,'*.IMAGE','Select Input Images'));
rotate = 0;%spm_input('Adjust pitch on oblique images (0.5 radians)?','+1','m','Yes|No',[1 0], 1);
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);
if rotate
    imgVols=adjust_pitch(imgVols,0.5);
end
if ~exist('sm','var')
    sm = spm_input('FWHM of smoothing kernel','+1','r',8);
end
if ~exist('results_dir','var')
    results_dir = spm_input('Directory to save results in?','+1','s',[pwd])
    a=spm_platform('filesys');% win, unx mac vms
    if strcmp(a,'unx')
        cd('/')
    elseif strcmp(a,'win')
        f=findstr(filesep,results_dir);
        cd(results_dir(1:f(1)));
        results_dir(1:f(1))=[];   
    end
    if ~exist(results_dir)
        mkdir(results_dir)
    end
    cd(results_dir)
    numsubjects = length(imgVols);
end
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
if ~exist('numsubjects','var')
  numsubjects = length(imgVols);
end
if sge
%     srccall =    'source /usr/local/sge/default/common/settings.sh';
%     system(srccall);
    fun=@sge_mls_fluid_prep;
else
    fun=@mls_fluid_prep;
end
feval(fun,'pet','vbmonly','numsubjects',numsubjects,'norm2custom', ...
      template_cell,'smooth',sm,'imgVols',imgVols,'results_dir',results_dir,'stime',stime,'optimal',optimal);


