#!/usr/bin/env octave
% Takes an xml definition of an fmri preprocess pipeline and creates an xml 
% pipeline and seperate .mat jobs.
%
% SYNTAX:
% psom_generate_pipeline <pipe_definition.xml> <jobs_folder>
%
% ___________________________________________________________________________
% INPUTS
%
% pipe_definition.xml
%     A .xml pipeline definition. See file template.xml for format.
% 
% jobs_folder
%     An existing folder where to save the pipeline.xml and seperate jobs.mat.
% ___________________________________________________________________________
% OUTPUTS
%
% This script will create the following files in jobs_folder : 
%
% pipeline.xml
%     A .xml file containing the pipeline.
%       
% job_(name of job).mat 
%     Files for each job in the given pipeline.
% 
% _________________________________________________________________________
% COMMENTS:
%
% Copyright (c) Sebastien Lavoie-Courchesne, 
% Centre de recherche de l'institut de Gériatrie de Montréal
% Département d'informatique et de recherche opérationnelle
% Université de Montréal, 2011.
%
% Maintainer : pierre.bellec@criugm.qc.ca
% See licensing information in the code.
% Keywords : pipeline, xml, fmri, preprocess

% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.

if(nargin != 2)
    error("USAGE: niak_pipeline_fmri_preprocess_generator <pipe_definition.xml> <jobs_folder>\n");
end

args = argv();
pipe_definition = args{1};
if ~exist(pipe_definition,'file')
    error(strcat('No pipe_definition.xml found : ',pipe_definition));
end
jobs_folder = args{2};
if (jobs_folder(length(jobs_folder)) != filesep)
    jobs_folder = strcat(jobs_folder,filesep);
end
if ~exist(jobs_folder,'dir')
    error(strcat('No jobs_folder found : ',jobs_folder));
end
if (jobs_folder(end) != filesep)
    jobs_folder = strcat(jobs_folder,filesep);
end

pipeline = psom_read_xml(pipe_definition);
files_in = pipeline.niak_pipeline_fmri_preprocess.files_in;
opt = pipeline.niak_pipeline_fmri_preprocess.opt;
opt.flag_test = 1;
pipeline2 = niak_pipeline_fmri_preprocess(files_in,opt);
psom_write_pipeline2xml(pipeline2,jobs_folder);
printf('***Success***\n');