function contrast_copier(varargin);
%-----------------------------------------------------------------
% Short script to copy contrast files from many dispersed subject directories
% into a centralized group directory and label them appropriately, for ease
% of use in creating group analyses.
% use: contrast_copier(names, startdir, resdirname, confilename, destdir)
%       names = mandatory argument, a cell vector of strings that contains
%       the names of the subject directories relative to start_dir.
%       startdir(optional) = the pathname of the experiment directory, in 
%       which the subject dirs 'names' can be found.  If not supplied,
%       the user will be queried.
%       resdir(optional) = a string containing the name of the results directory
%       in each subject directory in which the appropriate confile is to be found.
%       The script assumes that all subject dirs will have the same results
%       directory names.  If one is not supplied, the user will be queried for
%       one.
%       confilename(optional) = a string containing the name of the confile
%       which is to be copied from each resdir (i.e., 'con_0002.img').  If one
%       isn't supplied, the user will be queried for one.
%       destdir(optional) = a destination directory for the copied and relabeled
%       confiles.  If one is not supplied, the user will be queried for one.
%
%       4/21/04 Dara - added option to choose subject dir names via GUI


if nargin < 1
    names_char = spm_get(-Inf,'', 'Choose subject directories.');
    % convert full path names (char) to single dir names (cell)
    for n=1:size(names_char,1),
       [path,names{n}]=fileparts(names_char(n,:));
    end
else
    names = varargin{1};
end

if nargin < 2
    startdir = spm_get(-1,'', 'Choose experiment home directory.');
else
    startdir = varargin{1};
end

if nargin < 3
    resdirname = spm_input_ui('Enter results directory name:', 1, 's');
else
    resdirname = varargin{2};
end

if nargin < 4
    confilename = spm_input_ui('Enter contrast file name (with .img):', 1, 's');
else
    confilename = varargin{3};
end

if nargin < 5
    destdir = spm_get(-1,'', 'Choose destination directory.');
else
    destdir = varargin{4};
end
       

for i = 1:length(names) 
    curr_sname = names{i}
    curr_sdir = fullfile(startdir, names{i});
    curr_rdir = fullfile(curr_sdir, resdirname);
    [dummy, curr_conname] = fileparts(confilename);
    curr_imgname = fullfile(curr_rdir, confilename);
    curr_hdrname = fullfile(curr_rdir, [curr_conname '.hdr']);
    dest_imgname = fullfile(destdir, [curr_conname '_' curr_sname '.img']);
    dest_hdrname = fullfile(destdir, [curr_conname '_' curr_sname '.hdr']);    

    if (~(exist(curr_imgname, 'file'))) | (~(exist(curr_hdrname, 'file')))
        error(['Missing .img or .hdr from: ' curr_imgname]);
    else % i.e., both .img and .hdr exist
        system(['cp ' curr_imgname ' ' dest_imgname]);
        system(['cp ' curr_hdrname ' ' dest_hdrname]);
    end

end

