#!/bin/sh
#
#  Name:
#     fmex - compilation program for MATLAB Fortran language MEX files
#     
#  Usage:  Please call mex directly.  This function may be unsupported
#     in the future.
#
#   Description:
#
#     fmex is a front-end to the main script, 'mex'.  The mex script
#     handles both C and Fortran source code, so separate cmex and
#     fmex scripts are no longer necessary.  They are provided only
#     for backward compatibility.
#
#  Algorithm:
#
#     1. Determine MATLAB root directory to locate "mex".
#     2. Wrap input arguments in proper level of quotation.
#     3. Invoke mex.
#
#  See Also:
#     mex(1L),cmex(1L)
#     MATLAB API Guide
#
# Copyright (c) 1984-1998 by The MathWorks, Inc.
# All Rights Reserved.
# $Revision: 1.14 $  $Date: 1998/08/25 12:19:55 $
#____________________________________________________________________________
#
    arg0_=$0
#
#   Determine the path of the MATLAB root directory - always one directory
#   up from the path to this command.
#
    filename=$arg0_
#
# Now it is either a file or a link to a file.
#
    cpath=`pwd`
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
    n=1
    maxlinks=8
    while [ $n -le $maxlinks ]
    do
#
# Get directory correctly!
#
	newdir=`echo "$filename" | awk '
                        { tail = $0
                          np = index (tail, "/")
                          while ( np != 0 ) {
                             tail = substr (tail, np + 1, length (tail) - np)
                             if (tail == "" ) break
                             np = index (tail, "/")
                          }
                          head = substr ($0, 1, length ($0) - length (tail))
                          if ( tail == "." || tail == "..")
                             print $0
                          else
                             print head
                        }'`
	if [ ! "$newdir" ]; then
	    newdir="."
	fi
	(cd $newdir) > /dev/null 2>&1
	if [ $? -ne 0 ]; then
    echo ''
    echo 'Internal error 1: We could not determine the path of the'
    echo '                  MATLAB root directory.'
    echo ''
    echo "                  original command path = $arg0_"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo ''
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
	    exit 1
	fi
	cd $newdir
#
# Need the function pwd - not the built in one
#
	newdir=`/bin/pwd`
#
	newbase=`expr //$filename : '.*/\(.*\)' \| $filename`
        lscmd=`ls -l $newbase 2>/dev/null`
	if [ ! "$lscmd" ]; then
    echo ''
    echo 'Internal error 2: Could not determine the path of the'
    echo '                  MATLAB root directory.'
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo ''
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
	    exit 1
	fi
#
# Check for link portably
#
	if [ `expr "$lscmd" : '.*->.*'` -ne 0 ]; then
	    filename=`echo "$lscmd" | awk '{ print $NF }'`
	else
#
# It's a file
#
	    dir="$newdir"
	    command="$newbase"
#
	    cd $dir/..
	    MATLAB=`/bin/pwd`; export MATLAB
	    break
	fi
        n=`expr $n + 1`
    done
    if [ $n -gt $maxlinks ]; then
    echo ''
    echo 'Internal error 3: More than $maxlinks links in path to'
    echo "                  this script. That's too many!"
    echo ''
    echo "                  original command path = $filename"
    echo "                  current  command path = $filename"
    echo ''
    echo '                  Please contact:'
    echo ''
    echo '                      MathWorks Technical Support'
    echo ''
    echo '                  for further assistance.'
    echo ''
	exit 1
    fi
#       
    cd $cpath
#
# Properly quote the arguments
#
    arglist=
    while [ $# -ne 0 ]
    do
      if [ `expr "//$1" : "//.*'.*" \| "//$1" : '//.*".*'` -gt 0 ];
then
          arglist="$arglist ""`echo "$1" | awk '
#----------------------------------------------------------------------------
      { squote = sprintf ("%c", 39)   # set single quote
        dquote = sprintf ("%c", 34)   # set double quote
        newarg=squote                 # initialize output string to
                                      # single quote
        lookquote=squote              # look for single quote
        oldarg = $0
          while ((i = index (oldarg, lookquote))) {
           newarg = newarg substr (oldarg, 1, i - 1) lookquote
           oldarg = substr (oldarg, i, length (oldarg) - i + 1)
           if (lookquote == squote)
              lookquote = dquote
           else
              lookquote = squote
           newarg = newarg lookquote
        }
        print newarg oldarg lookquote }'`"
#----------------------------------------------------------------------------
      else
          arglist="$arglist ""'$1'"
      fi
      shift
    done
#
    eval "exec $MATLAB/bin/mex $arglist"
