#! /bin/sh
#
#  Name:
#     matlab    script file for invoking MATLAB
#
#  Usage:
#     matlab [-h|-help] | [-n] [-version]
#	     [[-c licensefile] [-display Xserver] [-debug]]
#	     [-Ddebugger [options]]
#
#  Description:
#     This Bourne Shell script sets MATLAB environment variables,
#     determines the machine architecture, and starts the appropriate
#     executable.
#
#  Options:
#
#     -h,-help
#
#           Help. Show command usage
#
#     -n
#
#	    Print out the values of the environment variables used by 
#	    matlab. The image is NOT run.
#
#
#     -version
#
#	    Execute matlab.$version rather than matlab
#
#	    (sgi only: R3|R3000, R4|R4000, or R8|R8000)
#
#     -c licensefile (ignored on convex or cray)
#
#           Set LM_LICENSE_FILE to licensefile. This supersedes either
#	    the value set in .matlabrc.sh or the default set in this
#	    script. $MATLAB/etc/license.dat.DEMO.* files are NOT appended
#	    using the ':' notation to this value. Those license.dat.DEMO*
#	    files are appended in all other cases.
#
#     -display Xserver
#
#	    Send X commands to X Window Server given by Xserver. This
#	    supersedes the value of the DISPLAY environment variable.
#
#     -debug
#
#           Provides debugging information especially for X based
#	    problems. Should be used only in conjunction with a
#	    Technical Support Representative from The MathWorks, Inc.
#
#     -Ddebugger options
#
#	    Startup matlab with debugger (e.g. dbx, xdb, gdb, ups, etc.)
#	    The options if any depend on the debugger. If the debugger
#	    allows options to be passed to matlab always include them
#	    as part of options. Sets the environment variable:
#	    		MATLAB_DEBUG=debugger
#
#	    NOTE: For certain debuggers like gdb, the SHELL environment
#		  variable is ALWAYS set to /bin/sh.
#
#  Copyright (c) 1986-94 by The MathWorks, Inc.
#  $Revision: 1.49 $  $Date: 1995/03/16 17:36:23 $
#__________________________________________________________________________
#
#**************************************************************************
# Determine the path of the MATLAB root directory - always one directory
# up from the path to this command.
#**************************************************************************
#
    filename=$0
#
# 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 = $0"
    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/..
	    MATLABdefault=`/bin/pwd`;
	    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
#
#**************************************************************************
#
    RCSHEADER='$Revision: 1.49 $  $Date: 1995/03/16 17:36:23 $'
#
    USAGE1="${command} [-h|-help] | [-n] [-version]"
    USAGE2="       [[-c licensefile] [-display Xserver] [-debug]]"
    USAGE3="       [-Ddebugger [options]]" 
#
# Parse the arguments
#
    stat="OK"
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
	case "$1" in
	    -h|-help)
		stat=""
		;;
            -n)
                showenv=1
                ;;
	    -c)
		if [ $# -eq 1 ]; then
		   stat=""
		else
		   shift
		   licensefile=$1
		fi
		;;
	    -display)
		if [ $# -eq 1 ]; then
		    stat=""
		else
		    arglist="$arglist $1"
		    shift
		    isoption=`expr "/$1" : '^/\(-.*\)'`
		    if [ "$isoption" != "" ]; then
		        stat=""
		    else
		        arglist="$arglist $1"
			display="$1"
		    fi
		fi
 		;;
	    -D*)
		debugger=`expr "$1" : '-D\(.*\)'`
		if [ "$debugger" = "" ]; then
		    stat=""
		else
		    MATLAB_DEBUG=`expr "//$debugger" : ".*/\(.*\)""`
		fi
		;;
            *=*)
                lhs=`expr "$1" : '\([a-zA-Z0-9_]*\)=.*'`
                rhs=`expr "$1" : '[a-zA-Z0-9_]*=\(.*\)$'`
                arglist=`echo $arglist $lhs='"'$rhs'"'`
                ;;
	    -R3|-R3000)
	        matlabversion=R3000
		;;
	    -R4|-R4000)
	        matlabversion=R4000
		;;
	    -R8|-R8000)
	        matlabversion=R8000
		;;
	    *)
		arglist="$arglist $1"
		;;
	esac
	shift
    done
#
# Check for errors
#
    if [ "$stat" != "OK" -a "$showenv" != "1" ]; then	# An error occurred.
#
        RCSREVISION=`echo "$RCSHEADER" | awk '{ print $2 }'`
#
        if [ "$stat" != "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ""
    echo "    ${command}:  $stat"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ""
    echo "    Usage:  $USAGE1"
    echo "            $USAGE2"
    echo "            $USAGE3"
    echo ""
    echo "    -h | -help           - display arguments" 
    echo "    -n                   - print environment variables only" 
    echo "    -version             - execute matlab.$version rather"
    echo "                           than matlab"
    echo "			     (sgi only: R3 (R3000), R4 (R4000), or R8 (R8000))"
    echo "    -c licensefile       - set LM_LICENSE_FILE to licensefile"
    echo "                           (ignored on convex and cray)" 
    echo "    -display Xserver     - Send X commands to Xserver"
    echo "    -debug               - Turn on some MATLAB internal debugging"
    echo "    -Ddebugger [options] - Startup matlab with debugger"
    echo ""
    echo "    Revision #: $RCSREVISION"
    echo ""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	trap ""
        exit 1
    fi
#
# Determine what is set in the environment
#
    DISPLAYenv="$DISPLAY"
    LM_LICENSE_FILEenv="$LM_LICENSE_FILE"
    TOOLBOXenv="$TOOLBOX"
    MATLABPATHenv="$MATLABPATH"
    XAPPLRESDIRenv="$XAPPLRESDIR"
    XKEYSYMDBenv="$XKEYSYMDB"
    SHELLenv="$SHELL"
#
# Set the defaults - MATLABdefault is determined above.
#
    AUTOMOUNT_MAPdefault=''
    DISPLAYdefault=''
    ARCHdefault=''
    LM_LICENSE_FILEdefault='$MATLAB/etc/license.dat'
    TOOLBOXdefault='$MATLAB/toolbox'
    MATLABPATHdefault=''
    XAPPLRESDIRdefault='$MATLAB/app-defaults'
    XKEYSYMDBdefault='$MATLAB/app-defaults/XKeysymDB'
    SHELLdefault='$SHELL'
#
    MATLAB_UTIL_DIRdefault=$MATLABdefault/install
#
#--------------------------------------------------------------------------
#
# Source file .matlabrc.sh and get values for the following environment
# variables
#
#       MATLAB                  (MATLAB root directory)
#       AUTOMOUNT_MAP           (Path prefix map for automounting)
#       DISPLAY                 (DISPLAY variable for X Window System)
#       ARCH                    (machine architecture)
#       LM_LICENSE_FILE         (FLEXlm license file)
#       TOOLBOX                 (toolbox path)
#       MATLABPATH              (matlab search path)
#       XAPPLRESDIR             (X Application Resource Directory)
#       XKEYSYMDB               (X keysym Database file)
#       SHELL                   (which shell to use for ! in MATLAB)
#
# The search order for .matlabrc.sh is:
#
#       .               (current directory)
#       $HOME           (users home directory)
#       $MATLAB/bin     (MATLAB bin directory)
#
    if [ -f .matlabrc.sh ]; then
        SOURCED_DIR='.'
        SOURCED_DIReval=`pwd`
        . $cpath/.matlabrc.sh
    elif [ -f $HOME/.matlabrc.sh ]; then
        SOURCED_DIR='$HOME'
        SOURCED_DIReval=$HOME
        . $HOME/.matlabrc.sh
    elif [ -f $MATLABdefault/bin/.matlabrc.sh ]; then
#
# NOTE: At this point we will use the MATLAB determined earlier to
#       source the file. After that the value in that file if not
#       null will be used.
#
        SOURCED_DIR='$MATLAB/bin'
        SOURCED_DIReval=$MATLABdefault/bin
        . $MATLABdefault/bin/.matlabrc.sh
    else
        SOURCED_DIR=
        MATLAB_UTIL_DIR=$MATLAB_UTIL_DIRdefault
#
# arch.sh requires MATLAB - save temporarily
#
        MATLABsave="$MATLAB"
        MATLAB="$MATLABdefault"
#
        . $MATLAB_UTIL_DIR/arch.sh
        if [ "$Arch" = "unknown" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '^G    Sorry! We could not determine the machine architecture for your'
    echo '           host. Please contact:'
    echo ''
    echo '               MathWorks Technical Support'
    echo ''
    echo '           for further assistance.'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            trap ""
            exit 1
        fi
        MATLAB="$MATLABsave"
#
	ARCHdefault=$Arch
	license=1
        if [ "$Arch" = "c2mp" -o "$Arch" = "cray" ]; then
	    license=0
	fi
    fi
#
# Determine the final values for the following variables
#
#       MATLAB                  (MATLAB root directory)
#       AUTOMOUNT_MAP           (Path prefix map for automounting)
#       DISPLAY                 (DISPLAY variable for X Window System)
#       ARCH                    (machine architecture)
#       LM_LICENSE_FILE         (FLEXlm license file)
#				(ignored on convex and cray)
#       TOOLBOX                 (toolbox path)
#       MATLABPATH              (matlab search path)
#       XAPPLRESDIR             (X Application Resource Directory)
#       XKEYSYMDB               (X keysym Database file)
#       MATLAB_DEBUG		(name of the debugger from -Ddebugger argument)
#       SHELL                   (which shell to use for ! in MATLAB)
#
    rcfile='r '
    environ='e '
    script='s '
    argument='a '
    rcfilep='rs'
    environp='es'
#
# Sourced a .matlabrc.sh file
#
    if [ "$SOURCED_DIR" != "" ]; then
        if [ "$MATLAB" != "" ]; then
            MATLABmode="$rcfile"
        else
            MATLABmode="$script"
            MATLAB="$MATLABdefault"
        fi
        if [ "$AUTOMOUNT_MAP" != "" ]; then
            MATLAB=`echo $MATLAB $AUTOMOUNT_MAP | awk '
                {if (substr($1,1,length($2)) == $2)
                     if (NF == 4)                               # a -> b
                         print $NF substr($1,length($2) + 1)
                     else                                       # a ->
                         print substr($1,length($2) + 1)
                     else
                         print $1}'`
        fi
#
	if [ "$AUTOMOUNT_MAP" != "" ]; then
            AUTOMOUNT_MAPmode="$rcfile"
	else
            AUTOMOUNT_MAPmode="$script"
	    AUTOMOUNT_MAP="`eval echo $AUTOMOUNT_MAPdefault`"
	fi
#
	if [ "$display" != "" ]; then
            DISPLAYmode="$argument"
	    DISPLAY="$display"
	elif [ "$DISPLAY" != "" ]; then
	    if [ "$DISPLAYenv" = "$DISPLAY" ]; then
                DISPLAYmode="$environ"
	    else
                DISPLAYmode="$rcfile"
	    fi
	else
            DISPLAYmode="$script"
	    DISPLAY="`eval echo $DISPLAYdefault`"
	fi
#
	if [ "$ARCH" != "" ]; then
            ARCHmode="$rcfile"
	else
            ARCHmode="$script"
	    ARCH="$ARCHdefault"	
	fi
#
	if [ "$license" = "1" ]; then
	    if [ "$licensefile" != "" ]; then
	        LM_LICENSE_FILE="$licensefile"
                LM_LICENSE_FILEmode="$argument"
	    elif [ "$LM_LICENSE_FILE" != "" ]; then
	        LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILE`"
	        if [ "$LM_LICENSE_FILEenv" = "$LM_LICENSE_FILE" ]; then
                    LM_LICENSE_FILEmode="$environ"
	        else
                    LM_LICENSE_FILEmode="$rcfile"
	        fi
	    else
                LM_LICENSE_FILEmode="$script"
	        LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILEdefault`"
	    fi
	fi
#
	if [ "$TOOLBOX" != "" ]; then
	    if [ "$TOOLBOXenv" = "$TOOLBOX" ]; then
                TOOLBOXmode="$environ"
	    else
                TOOLBOXmode="$rcfile"
	    fi
	else
            TOOLBOXmode="$script"
	    TOOLBOX="`eval echo $TOOLBOXdefault`"
	fi
#
	if [ "$XAPPLRESDIR" != "" ]; then
	    XAPPLRESDIR="`eval echo $XAPPLRESDIR`"
	    if [ "$XAPPLRESDIRenv" = "$XAPPLRESDIR" ]; then
                XAPPLRESDIRmode="$environ"
	    else
                XAPPLRESDIRmode="$rcfile"
	    fi
	else
            XAPPLRESDIRmode="$script"
	    XAPPLRESDIR="`eval echo $XAPPLRESDIRdefault`"
	fi
#
	if [ "$XKEYSYMDB" != "" ]; then
	    XKEYSYMDB="`eval echo $XKEYSYMDB`"
	    if [ "$XKEYSYMDBenv" = "$XKEYSYMDB" ]; then
                XKEYSYMDBmode="$environ"
	    else
                XKEYSYMDBmode="$rcfile"
	    fi
	else
            XKEYSYMDBmode="$script"
	    XKEYSYMDB="`eval echo $XKEYSYMDBdefault`"
	fi
#
        if [ "$MATLABPATH" != "" ]; then
	    if [ "$MATLABPATHenv" = "$MATLABPATH" ]; then
                MATLABPATHmode="$environp"
	    else
                MATLABPATHmode="$rcfilep"
	    fi
        else
            MATLABPATHmode="$script"
	    MATLABPATH="`eval echo $MATLABPATHdefault`"
        fi
#
	if [ "$MATLAB_DEBUG" = "" ]; then
	    MATLAB_DEBUG=
            MATLAB_DEBUGmode="$script"
	else
            MATLAB_DEBUGmode="$argument"
	fi
#
	if [ "$SHELL" != "" ]; then
	    if [ "$SHELLenv" = "$SHELL" ]; then
                SHELLmode="$environ"
	    else
                SHELLmode="$rcfile"
	    fi
	else
            SHELLmode="$script"
	    SHELL="`eval echo $SHELLdefault`"
	fi
    else
	MATLABmode="$script"
	MATLAB="$MATLABdefault"
    	AUTOMOUNT_MAPmode="$script"
	AUTOMOUNT_MAP="`eval echo $AUTOMOUNT_MAPdefault`"
	if [ "$display" != "" ]; then
            DISPLAYmode="$argument"
	    DISPLAY="$display"
	else
            DISPLAYmode="$environ"
	    DISPLAY="$DISPLAYenv"
	fi
        ARCHmode="$script"
	ARCH="$ARCHdefault"
	if [ "$license" = "1" ]; then
	    if [ "$licensefile" != "" ]; then
	        LM_LICENSE_FILE="$licensefile"
                LM_LICENSE_FILEmode="$argument"
	    else
                LM_LICENSE_FILEmode="$script"
	        LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILEdefault`"
	    fi
	fi
	TOOLBOXmode="$script"
	TOOLBOX="`eval echo $TOOLBOXdefault`"
        XAPPLRESDIRmode="$script"
	XAPPLRESDIR="`eval echo $XAPPLRESDIRdefault`"
        XKEYSYMDBmode="$script"
	XKEYSYMDB="`eval echo $XKEYSYMDBdefault`"
        if [ "$MATLABPATHenv" != "" ]; then
            MATLABPATHmode="$environp"
	    MATLABPATH="$MATLABPATHenv"
	else
            MATLABPATHmode="$script"
	    MATLABPATH="`eval echo $MATLABPATHdefault`"
	fi
	if [ "$MATLAB_DEBUG" = "" ]; then
	    MATLAB_DEBUG=
            MATLAB_DEBUGmode="$script"
	else
            MATLAB_DEBUGmode="$argument"
	fi
        SHELLmode="$environ"
	SHELL="$SHELLenv"
    fi
#--------------------------------------------------------------------------
#
# Fix LD_LIBRARY_PATH for various cases
#
    LD_LIB_PATHfix=
    case "$Arch" in
	sol2|alpha)
	    if [ "$LD_LIBRARY_PATH" != "" ]; then
		LD_LIBRARY_PATH=$MATLAB/extern/lib/$Arch:$LD_LIBRARY_PATH
                LD_LIB_PATHmode="$environp"
	    else
		LD_LIBRARY_PATH=$MATLAB/extern/lib/$Arch
                LD_LIB_PATHmode="$script"
	    fi
    	    LD_LIB_PATHfix=1
	    export LD_LIBRARY_PATH
	    ;;
	sgi)
	    if [ -d $TOOLBOX/images ]; then
		if [ "$LD_LIBRARY_PATH" != "" ]; then
		    if [ "`/bin/uname`" = "IRIX64" ]; then
		        LD_LIBRARY_PATH=$TOOLBOX/images/lib/sgi64:$LD_LIBRARY_PATH
		    else
		        LD_LIBRARY_PATH=$TOOLBOX/images:$LD_LIBRARY_PATH
		    fi
                    LD_LIB_PATHmode="$environp"
		else
		    if [ "`/bin/uname`" = "IRIX64" ]; then
		        LD_LIBRARY_PATH=$TOOLBOX/images/lib/sgi64
		    else
		        LD_LIBRARY_PATH=$TOOLBOX/images
		    fi
                    LD_LIB_PATHmode="$script"
		fi
    	        LD_LIB_PATHfix=1
		export LD_LIBRARY_PATH
	    fi
	    if [ "$LD_LIBRARY_PATH" != "" ]; then
		LD_LIBRARY_PATH=$MATLAB/extern/lib/$Arch:$LD_LIBRARY_PATH
		if [ "$LD_LIB_PATHmode" = "" ]; then
                    LD_LIB_PATHmode="$environp"
		fi
	    else
		LD_LIBRARY_PATH=$MATLAB/extern/lib/$Arch
		if [ "$LD_LIB_PATHmode" = "" ]; then
                    LD_LIB_PATHmode="$script"
		fi
	    fi
    	    LD_LIB_PATHfix=1
	    export LD_LIBRARY_PATH
	    ;;
	*)
	    ;;
    esac
#--------------------------------------------------------------------------
#
# SHELL must currently be defined. (problem showed up on sol2)
#
    if [ "$SHELL" = "" ]; then 
        SHELLmode="$script"
	SHELL="/bin/sh"
    fi
#
#--------------------------------------------------------------------------
#
# Add any 'DEMO' licenses in $MATLAB/etc unless -c option is used
#
    if [ "$license" = "1" ]; then
        if [ "$LM_LICENSE_FILEmode" != "$argument" ]; then
	    demolist=`ls $MATLAB/etc/license.dat.DEMO* 2>/dev/null`
            if [ "$demolist" != "" ]; then
	        for demofile in $demolist
	        do
		    LM_LICENSE_FILE="$LM_LICENSE_FILE:$demofile"
	        done
            fi
        fi
    fi
#--------------------------------------------------------------------------
#
# Be sure that X sees the Matlab Resource Database (X11/R5).
#
# The critical thing that can block access to the $XAPPLRESDIR/Matlab
# database is the existence of the XUSERFILESEARCHPATH variable. If
# it exists then it does NOT automatically use $XAPPLRESDIR/Matlab even if
# it is not part of the XUSERFILESEARCHPATH path variable.
#
    if [ "$XUSERFILESEARCHPATH" != "" ]; then
        XAPPLRESDIRmode="$environp"
	XUSERFILESEARCHPATH=$XUSERFILESEARCHPATH:$XAPPLRESDIR/%N
	export XUSERFILESEARCHPATH
    fi
#
    BASEMATLABPATH=$MATLABPATH; export BASEMATLABPATH
#
#--------------------------------------------------------------------------
#
# Determine the rest of MATLABPATH
#
#============================================================================
#
# Add other standard Toolbox options to MATLABPATH 
#
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/general
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/ops
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/lang
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/elmat
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/specmat
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/elfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/specfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/matfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/datafun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/polyfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/funfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/sparfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/plotxy
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/plotxyz
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/graphics
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/color
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/sounds
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/strfun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/iofun
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/matlab/demos
#
# Add licensed Toolbox options to MATLABPATH
#
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/compiler
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/control
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/ident
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/images
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/local
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/optim
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/pde
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/signal
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/stats
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/symbolic
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/uitools
#
# Append SIMULINK toolboxes to MATLABPATH
#
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/simulink/simulink
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/simulink/blocks
MATLABPATH=$MATLABPATH:$MATLAB/toolbox/simulink/simdemos
#
    if [ -d $HOME/matlab ]; then
	MATLABPATH=$MATLABPATH:$HOME/matlab
    fi
#
# Remove any leading ":" character from the path. Can't use awk
# here because it fails on very long paths.
#
    MATLABPATH=`echo $MATLABPATH | sed 's/^://'`
#
#============================================================================
#
    if [ "$showenv" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    (echo '------------------------------------------------------------------------'
	if [ "$SOURCED_DIR" != "" ]; then
    echo "->      (.matlabrc.sh) sourced from directory (DIR = $SOURCED_DIR)"
    echo "->      DIR = $SOURCED_DIReval"
	else
    echo "->      (.matlabrc.sh) not found."
	fi
    echo '------------------------------------------------------------------------'
    echo '        r = rcfile  e = environment  s = script  a = argument'
    echo '------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "->  $MATLABmode  MATLAB              = $MATLAB"
    echo "->  $AUTOMOUNT_MAPmode  AUTOMOUNT_MAP       = $AUTOMOUNT_MAP"
    echo "->  $DISPLAYmode  DISPLAY             = $DISPLAY"
    echo "->  $ARCHmode  ARCH                = $ARCH"
    if [ "$license" = "1" ]; then
        echo "->  $LM_LICENSE_FILEmode  LM_LICENSE_FILE     = $LM_LICENSE_FILE"
    fi
    echo "->  $TOOLBOXmode  TOOLBOX             = $TOOLBOX"
	if [ "$XUSERFILESEARCHPATH" != "" ]; then
    echo "->  $XAPPLRESDIRmode  XUSERFILESEARCHPATH = $XUSERFILESEARCHPATH"
	else
    echo "->  $XAPPLRESDIRmode  XAPPLRESDIR         = $XAPPLRESDIR"
	fi
    echo "->  $XKEYSYMDBmode  XKEYSYMDB           = $XKEYSYMDB"
    echo "->  $MATLAB_DEBUGmode  MATLAB_DEBUG        = $MATLAB_DEBUG"
	if [ "$LD_LIB_PATHfix" != "" ]; then
    echo "->  $LD_LIB_PATHmode  LD_LIBRARY_PATH     = $LD_LIBRARY_PATH"
	fi
    echo "->  $argument  arglist             = $arglist"
    echo "->  $SHELLmode  SHELL               = $SHELL"
    echo "->  $MATLABPATHmode  MATLABPATH          ="
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if [ "$MATLABPATH" != "" ]; then
	    for dir in `echo $MATLABPATH | tr ':' ' '`
	    do
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "	$dir"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    done
	fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------------') | more
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	exit 0
    fi
#
# Export the variables
#
    export MATLAB
    export AUTOMOUNT_MAP
    export DISPLAY
    export ARCH
    if [ "$license" = "1" ]; then
        export LM_LICENSE_FILE
    fi
    export TOOLBOX
    export MATLABPATH
    export XAPPLRESDIR
    export XKEYSYMDB
    export MATLAB_DEBUG
    export SHELL
    export LD_PRELOAD
#
# This can be a ":" separated list. Go down the list and issue a message
# for each nonexistent file. Fail if none exist or if the first one does not
# exist.
#
    if [ "$license" = "1" ]; then
        licensefilelist=`echo $LM_LICENSE_FILE | tr ':' ' '`
        cont=1
        nfiles=1
        for licensefile in $licensefilelist
        do
	    if [ `expr "$licensefile" : '.*@.*'` -ne 0 ]; then
	        :
	    elif [ ! -f $licensefile ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    matlab: license file in LM_LICENSE_FILE path does not exist'
    echo ''       
    echo "            file = $licensefile"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if [ $nfiles -eq 1 ]; then
		    cont=0
		fi
	    fi
	    nfiles=`expr $nfiles + 1`
	done
        if [ "$cont" != "1" ]; then 
	    exit 1
        fi
    fi
#
    if [ ! -d $MATLAB/bin/$ARCH ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    matlab: No MATLAB bin directory for this machine architecture.'
    echo ''       
    echo "           ARCH = $ARCH" 
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
    if [ ! -f $MATLAB/bin/$ARCH/matlab ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '	matlab: No MATLAB executable for this machine architecture.'
    echo ''       
    echo "           $MATLAB/bin/$ARCH/matlab does not exist!"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
    case "$ARCH" in
        apollo )
#
#  Apollo specific code
#
            if [ ! "${MATLAB_PRINT_OPT:+1}" ]; then
	        MATLAB_PRINT_OPT='prf -plot -bw /sys/node_data/paste_buffers/default.gmf';export MATLAB_PRINT_OPT
	    fi
	    if [ ! "${SHELL:+1}" ]; then
	        SHELL=/etc/sys_sh;export SHELL
	    fi
#
	    PATH=$OLDPATH; export PATH
            if [ "$debugger" != "" ]; then
	        if [ `expr "$MATLAB_DEBUG" : '.*gdb.*'` -ne 0 ]; then
                    SHELL=/bin/sh; export SHELL
                fi
                exec $debugger $MATLAB/bin/$Arch/matlab $arglist
            else
                exec $MATLAB/bin/$Arch/matlab $arglist
            fi
	    ;;
        hp700|ibm_rs|dec_risc|alpha|hp300)
	    if [ -d $MATLAB/codegen/bin/$ARCH ]; then
	        PATH=$MATLAB/codegen/bin/$ARCH:$PATH; export PATH
	    fi
            if [ "$debugger" != "" ]; then
	        if [ `expr "$MATLAB_DEBUG" : '.*gdb.*'` -ne 0 ]; then
                    SHELL=/bin/sh; export SHELL
                fi
                exec $debugger $MATLAB/bin/$Arch/matlab $arglist
            else
                exec $MATLAB/bin/$Arch/matlab $arglist
            fi
	    ;;
        sgi)
	    if [ -d $MATLAB/codegen/bin/$ARCH ]; then
	        PATH=$MATLAB/codegen/bin/$ARCH:$PATH; export PATH
	    fi
#
# Determine which image: matlab.R3000 (MIPS R3000), matlab (MIPS R4000), or
#			 matlab.R8000 (MIPS R8000)
#
	    if [ "$matlabversion" != "" ]; then
		if [ "$matlabversion" = "R4000" ]; then
		    image_type=""
	        else
		    image_type=".$matlabversion"
		fi
	    else
	        if [ "`/bin/uname`" = "IRIX64" ]; then
	            image_type=".R8000"
	        else
	            image_type=`/bin/hinv | grep 'CPU' | awk '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    { if (index($3,"R4") != 0 || index($3,"R8") != 0)
	  print ""
      else
          print ".R3000" }'`
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        fi
	    fi
            if [ "$debugger" != "" ]; then
	        if [ `expr "$MATLAB_DEBUG" : '.*gdb.*'` -ne 0 ]; then
                    SHELL=/bin/sh; export SHELL
                fi
                exec $debugger $MATLAB/bin/$Arch/matlab$image_type $arglist
            else
                exec $MATLAB/bin/$Arch/matlab$image_type $arglist
            fi
	    ;;
        *)
            if [ "$debugger" != "" ]; then
	        if [ `expr "$MATLAB_DEBUG" : '.*gdb.*'` -ne 0 ]; then
                    SHELL=/bin/sh; export SHELL
                fi
                exec $debugger $MATLAB/bin/$Arch/matlab $arglist
            else
                exec $MATLAB/bin/$Arch/matlab $arglist
            fi
	    ;;
    esac
