#!/usr/bin/octave -fqH
#
# Command Line Interface for SPM
# SPM: http://www.fil.ion.ucl.ac.uk/spm/
#
# Copyright (C) 2017 Wellcome Trust Centre for Neuroimaging
#
# Guillaume Flandin
# $Id: spm12-octave 7284 2018-03-29 09:42:26Z guillaume $ 


spm_dir = getenv ("SPM_HOME");
if (isempty (spm_dir))
  spm_dir = mfilename ("fullpath");
  sts = false;
  while ! (sts)
    [out, sts] = readlink (spm_dir);
    if ! (sts)
      if ! (is_absolute_filename (out))
        spm_dir = fullfile (fileparts (spm_dir), out);
      else
        spm_dir = out;
      endif
    endif
  endwhile
  spm_dir = fullfile (fileparts (spm_dir), "..");
endif
addpath (canonicalize_file_name (spm_dir));

try
  spm ("Ver");
catch
  printf (["error: Cannot find the SPM directory. " ...
    "Set SPM_HOME environment variable.\n"]);
  exit (1);
end

spm_standalone (argv (){:});

while (! isempty (get (0, "CurrentFigure")))
  waitfor (get (0, "CurrentFigure"));
endwhile
exit (0);
