#!/bin/sh

# $Id: getMATLABDirs,v 1.20 2014/02/27 11:17:20 svein Exp $

# Determine the MATLAB directory parameters

matlab_install="n"
makefile=$XITE_HOME/etc/Makefile.$XITE_HOSTTYPE

if [ -f $makefile ]
then
  matlab_home_def=`egrep '^ *MATLAB_HOME *= *' $makefile | awk '{print $3}'`
  matlab_inc_def=`egrep '^ *MATLAB_INC *= *' $makefile | awk '{print $3}'`
  matlab_lib_def=`egrep '^ *MATLAB_LIB *= *' $makefile | awk '{print $3}'`
  matlab_mex_dest_def=`egrep '^ *MATLAB_MEX_DEST *= *' $makefile | awk '{print $3}'`
  if [ -n "$matlab_home_def" -a "$matlab_home_def" != "-" -a -n "$matlab_inc_def" -a "$matlab_inc_def" != "-" -a -n "$matlab_lib_def" -a "$matlab_lib_def" != "-" -a -n "$matlab_mex_dest_def" -a "matlab_mex_dest_def" != "-" ]; then
    matlab_install="y"
  fi
fi

check=1
[ "$XITE_HOSTTYPE" = "Win32" ] && check=0

if test "$matlab_install" = "n" -a \
   \( -z "$MATLAB_HOME" -o -z "$MATLAB_INC" -o -z "$MATLAB_LIB" -o -z "$MATLAB_MEX_DEST" \)
then

cat <<EOF 1>&2

This software provides converters between the XITE image file
format and the binary file format used by the MATLAB software.

If the MATLAB software is installed on your platform, you may want
these converters installed also.

EOF

  echo "If you want the MATLAB converters, you will need to specify where" 1>&2
  echo "the MATLAB library and MATLAB include files are located." 1>&2
  $echoNl "Would you like to install the converters between MATLAB and " 1>&2
  $echoNl "XITE? ([y]/n) " 1>&2

  read matlab_install

  if test -z "$matlab_install" -o "$matlab_install" = "yes" \
	  -o "$matlab_install" = "y"
  then
    matlab_install="y"
  else
    matlab_install="n"
  fi

  if test "$matlab_install" = "y"
  then
    if test -n "$MATLAB_HOME"
    then
      matlab_home_def=$MATLAB_HOME
    else
      matlab_home_def=/usr/local/matlab
    fi
    MATLAB_HOME=`$getDir "$MATLAB_HOME" "$matlab_home_def" 'MATLAB home directory' "$TMP_INSTALL" "stderr" $check`

    MATLAB_HOSTTYPE=`$matlab_hosttype "$XITE_HOSTTYPE"`
    matlab_inc_def=$MATLAB_HOME/extern/include
    matlab_lib_def=$MATLAB_HOME/bin/$MATLAB_HOSTTYPE
    matlab_mex_dest_def=$MATLAB_HOME/toolbox/xite
  fi
elif test "$MATLAB_HOME" = "-" -o "$MATLAB_INC" = "-" -o "$MATLAB_LIB" = "-" -o \
    "$MATLAB_MEX_DEST" = "-"
then
  matlab_install="n"
  matlab_home_def=""
  matlab_inc_def=""
  matlab_lib_def=""
  matlab_mex_dest_def=""
else
  matlab_install="y"
fi

if test "$matlab_install" = "y"
then
  if test -z "$MATLAB_HOME"; then
    MATLAB_HOME=`$getDir "$MATLAB_HOME" "$matlab_home_def" 'MATLAB home directory' "$TMP_INSTALL" "stderr" $check`
  fi
  MATLAB_HOSTTYPE=`$matlab_hosttype "$XITE_HOSTTYPE"`
  MATLAB_INC=`$getDir "$MATLAB_INC" "$matlab_inc_def" 'MATLAB include directory' "$TMP_INSTALL" "stderr" $check`

  MATLAB_LIB=`$getDir "$MATLAB_LIB" "$matlab_lib_def" 'MATLAB library directory' "$TMP_INSTALL" "stderr" $check`

  MATLAB_MEX_DEST=`$getDir "$MATLAB_MEX_DEST" "$matlab_mex_dest_def" 'MATLAB mex-file destination directory' "$TMP_INSTALL" "stderr" 2 0755`

  if test "$XITE_HOSTTYPE" = "Win32"; then
    echo "Additional configuration is needed in the mexopts.bat file in" 1>&2
    echo "the XITE src/matlab directory." 1>&2
  fi
else
  MATLAB_HOME="-"
  MATLAB_INC="-"
  MATLAB_LIB="-"
  MATLAB_MEX_DEST="-"
  exceptionDirs="$exceptionDirs matlab"
fi

echo "MATLAB_HOME=$MATLAB_HOME; MATLAB_HOSTTYPE=$MATLAB_HOSTTYPE"
echo "MATLAB_INC=$MATLAB_INC; MATLAB_LIB=$MATLAB_LIB"
echo "MATLAB_MEX_DEST=$MATLAB_MEX_DEST; matlab_install=$matlab_install"
echo "exceptionDirs='$exceptionDirs'"
echo "export MATLAB_HOME MATLAB_HOSTTYPE MATLAB_INC MATLAB_LIB MATLAB_MEX_DEST"
echo "export matlab_install exceptionDirs"
