#!/bin/sh
#
# 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-mcr 7014 2017-02-13 12:31:33Z guillaume $ 

if [ "${MCR_HOME}" = "" ]; then
  echo "Set MCR_HOME environment variable." >&2 
  exit 1
fi
if [ ! -d "${MCR_HOME}" ]; then
  echo "MCR directory not found." >&2 
  exit 1
fi

if [ "${SPM_HOME}" = "" ]; then
  if [ "${PLATFORM}" = "Darwin" ]; then
    RL_FLAG="" # alternative needed
  else
    RL_FLAG="-f"
  fi
  SPM_HOME=$(readlink ${RL_FLAG} "$0")
  while [ ! -x "${SPM_HOME}/run_spm12.sh" ]; do
    if [ "${SPM_HOME}" = "$(dirname "${SPM_HOME}")" ]; then
      echo "Set SPM_HOME environment variable." >&2 
      exit 1
    fi
    SPM_HOME=$(dirname "${SPM_HOME}")
  done
fi
if [ ! -d "${SPM_HOME}" ]; then
  echo "SPM MCR directory not found." >&2 
  exit 1
fi
if [ ! -x "${SPM_HOME}/run_spm12.sh" ]; then
  echo "SPM MCR executable not found." >&2 
  exit 1
fi

${SPM_HOME}/run_spm12.sh ${MCR_HOME} $@
