#!/bin/bash
###############################################################################
#
# cutest2matlab: build the bridge between CUTEst and Matlab
#
# Nick Gould for GALAHAD Productions, January 2013
#
###############################################################################

if [[ -z "$MYMATLABARCH" ]]; then
  if [[ -z "$MYARCH" ]]; then
    echo ' neither environment variable MYMATLABARCH nor MYARCH is set.'
    echo ' Set MYMATLABARCH as a gfortran-installed version from the list '
    /bin/ls -1 $CUTEST/versions 2>/dev/null
    echo ' and re-run.'
    exit 1
  else
    ARCH=${MYARCH}
  fi
else
  ARCH=${MYMATLABARCH}
fi
echo $ARCH

#  check that the architecture provided in MYMATLABARCH or MYARCH exists

if [[ ! -e $CUTEST/versions/$ARCH ]] ; then
    echo ' The architecture privided by the environment variables MYMATLABARCH'
    echo '  and MYARCH has not been installed. Install a gfortran version of'
    echo ' CUTEst unsing install_cutest and re-run.'
    exit 2
fi

#  check that a gfortran version is used (as this is what Matlab supports!)

if [[ ${ARCH##*.} != 'gfo' && ${ARCH##*.} != 'gfo47' ]] ; then
    echo ' The architecture privided by the environment variables MYMATLABARCH'
    echo ' (or otherwise MYARCH) must use the gfortran compiler. Install a '
    echo ' gfortran version of CUTEst unsing install_cutest and re-run.'
    exit 3
fi

if [[ $# > 0 ]] ; then
  ${CUTEST}/bin/runcutest -A $ARCH -p matlab -D $1
else
  ${CUTEST}/bin/runcutest -A $ARCH -p matlab
fi

exit 0
