#! /bin/sh
#
#  Name:
#     matlab    script file for invoking MATLAB
#
#  Usage:
#     matlab [-h|-help] | [-n] [-arch | -ext | -arch/ext]
#	     [-c licensefile] [-display Xdisplay | -nodisplay]
#	     [-nosplash] [-mwvisual visualid] [-debug]
#	     [-nodesktop | -nojvm] [-runtime] [-check_malloc]
#	     [-r MATLAB_command] [-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 and
#	    arguments passed to the MATLAB executable as well as
#	    other diagnostic information. MATLAB is not run.
#
#     -arch
#
#	    Run MATLAB assuming this architecture rather than the
#	    actual machine architecture.
#
#     -ext
#	    Execute the version of MATLAB with extension ext if it
#           exists for the actual machine architecture.
#
#     -arch/ext
#
#	    Execute the version of MATLAB with extension ext assuming
#	    this architecture rather than the actual machine
#	    architecture.
#
#     -c licensefile 
#
#           Set LM_LICENSE_FILE to licensefile. It can have the form
#           port@host. This supersedes either the value set in
#	    .matlab6rc.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 Xdisplay
#
#	    Send X commands to X Window Server display, Xdisplay. This
#	    supersedes the value of the DISPLAY environment variable.
#
#     -nodisplay
#
#	    Do not display any X commands. The DISPLAY environment
#	    variable is ignored. The MATLAB desktop will not be started.
#	    However, unless -nojvm is also provided the Java virtual
#	    machine will be started.
#
#     -nosplash  
#
#           Do not display the splash screen during startup.
#
#     -mwvisual <visualid> 
#
#           The default X visual to use for figure windows.
#           The visualid is a hex number which can be found using
#           xdpyinfo, or using glxinfo on an sgi.
#
#     -debug
#
#           Provides debugging information especially for X based
#	    problems. Should be used only in conjunction with a
#	    Technical Support Representative from The MathWorks, Inc.
#
#     -nodesktop	
#
#	    Do not start the MATLAB desktop. Use the current window
#	    for commands. The Java virtual machine will be started.
#
#     -nojvm
#
#	    Shut off all Java support by not starting the Java virtual
#	    machine. In particular the MATLAB desktop will not be
#	    started.
#
#     -runtime
#
#	    Run MATLAB in Runtime Server mode. Required only if MATLAB
#	    is run from the Runtime Development Kit.
#
#     -check_malloc
#
#           Turn on MATLAB memory integrity checking.
#
#     -r MATLAB_command
#
#	    Start MATLAB and execute the MATLAB command.
#
#     -Ddebugger [options]
#
#	    Start MATLAB with debugger (e.g. dbx, gdb, dde, xdb, cvd).
#	    A full path can be specified for debugger. The options
#	    cover ONLY those that go after the executable to be debugged
#	    in the syntax of the actual debug command and for most
#	    debuggers this is very limited. To customize your debugging
#	    session use a startup file. See your debugger documentation
#	    for details. Options above that would normally be passed to
#	    the MATLAB executable should be used as parameters of
#	    a command inside the debugger like 'run' and not used
#	    when running the matlab script. If any of the options are
#	    placed before the -Ddebugger argument they will be
#	    handled as if they were part of the options after the
#	    -Ddebugger argument and will be treated as illegal
#	    options by most debuggers. The MATLAB_DEBUG environment
#	    variable is set to the filename part of the debugger argument.
#
#	    NOTE: For certain debuggers like gdb, the SHELL environment
#		  variable is ALWAYS set to /bin/sh.
#
# Copyright 1984-2000 The MathWorks, Inc.
# $Revision: 1.77 $  $Date: 2000/09/02 22:11:36 $
#__________________________________________________________________________
#
    arg0_=$0
#
# Temporary file that hold MATLABPATH code from .matlab6rc.sh file.
#
    temp_file=/tmp/$$a
#
    trap "rm -f $temp_file; exit 1" 1 2 3 15
#
#========================= archlist.sh (start) ============================
#
# usage:        archlist.sh
#
# abstract:     This Bourne Shell script creates the variable ARCH_LIST.
#
# note(s):      1. This file is always imbedded in another script
#
# Copyright 1997-2000 The MathWorks, Inc.
# $Revision: 1.8 $  $Date: 2000/06/16 16:08:59 $
#----------------------------------------------------------------------------
#
    ARCH_LIST='sol2 hpux hp700 alpha ibm_rs sgi glnx86'
#=======================================================================
# Functions:
#   check_archlist ()
#=======================================================================
    check_archlist () { # Sets ARCH. If first argument contains a valid
			# arch then ARCH is set to that value else
		        # an empty string. If there is a second argument
			# do not output any warning message. The most
			# common forms of the first argument are:
			#
			#     ARCH=arch
			#     MATLAB_ARCH=arch
			#     argument=-arch
			#
                        # Always returns a 0 status.
                        #
                        # usage: check_archlist arch=[-]value [noprint]
                        #
	if [ $# -gt 0 ]; then
	    arch_in=`expr "$1" : '.*=\(.*\)'`
	    if [ "$arch_in" != "" ]; then
	        ARCH=`echo "$ARCH_LIST EOF $arch_in" | awk '
#-----------------------------------------------------------------------
	{ for (i = 1; i <= NF; i = i + 1)
	      if ($i == "EOF")
		  narch = i - 1
	  for (i = 1; i <= narch; i = i + 1)
		if ($i == $NF || "-" $i == $NF) {
		    print $i
		    exit
		}
	}'`
#-----------------------------------------------------------------------
	       if [ "$ARCH" = "" -a $# -eq 1 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo "    Warning: $1 does not specify a valid architecture - ignored . . ."
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	       fi
	    else
		ARCH=""
	    fi
	else
	    ARCH=""
	fi
#
	return 0
    }
#=======================================================================
#========================= archlist.sh (end) ==============================
#
#=======================================================================
# Functions:
#   program ()
#   actualpath ()
#   check_rc_file ()
#   check_license ()
#   build_cmd ()
#=======================================================================
    program () { # Returns the name of the program to execute.
		 # If no extension use matlab.
		 #
                 # Always returns a zero status.
                 #
                 # usage: program arch [ext]
                 #
	arch=$1
        if [ $# -eq 2 ]; then
	    ext=$2
        else
	    ext=""
        fi
#
        if [ "$ext" != "" -a -f $MATLAB/bin/$arch/matlab.$ext ]; then
	    echo matlab.$ext
        else
	    echo matlab
        fi
#
        return 0
    }
#=======================================================================
    actualpath () { # Determine the actual path of a file following
		    # all links. Returns the real path or null if it
		    # is not a file.
                    #
		    # Always returns a 0 status.
		    #
                    # usage: actualpath path
                    #

        File=$1
        Filepath=
#
        lsCmd=`ls -ld $File 2>/dev/null`
        if [ "$lsCmd" ]; then
#
# Check for link portably
#
	    if [ `expr "$lsCmd" : '.*->.*'` -eq 0 ]; then
    	        if [ ! -d $File ]; then
	            Filepath=$File
                fi
            else
#
# A directory link?
#
		if [ -d $File ]; then
	            :
                else
#
# Now it is either a file, link to a file, or still a bad path.
#
	            cpath=`/bin/pwd`
                    localFile=$File
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
	            n=1
    	            while [ $n -le 8 ]
    	            do
#
# Get directory correctly!
#
   	                newDir=`echo "$localFile" | 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
			if [ ! -d $newDir ]; then
		            break
	                fi
	                cd $newDir
	                newDir=`/bin/pwd`
	 newBase=`expr //$localFile : '.*/\(.*\)' \| $localFile`
                        lsCmd=`ls -l $newBase 2>/dev/null`
	                if [ ! "$lsCmd" ]; then
		            break
	                fi
#
# Check for link portably
#
	                if [ `expr "$lsCmd" : '.*->.*'` -ne 0 ]; then
	                    localFile=`echo "$lsCmd" | \
				awk '{ print $NF }'`
	                else
#
# It's a file
#
	                    Filepath=$newDir/$newBase
	                fi
		        n=`expr $n + 1`
    	            done
    	            cd $cpath
	        fi
	    fi
        fi
	echo $Filepath
	return 0
    }
#=======================================================================
    check_rc_file () { # Checks rc_file file for minimal features.
                       # Currently the only thing it checks for is:
                       # 
                       #    .matlab6rc.sh in the file
                       # 
                       # If it fails the check print a warning message.
                       # The rc_file is assumed to exist.
                       # 
                       # Always returns a zero status.
                       # 
                       # usage: check_rc_file rc_file
                       # 
        grep '\.matlab6rc.sh' $1 > /dev/null 2>&1
        if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "---------------------------------------------------------------------------"
echo "Warning: The .matlab6rc.sh file that was sourced is old . . ."
echo "         --> file = $1"
echo " "
echo '         Please use $MATLAB/bin/.matlab6rc.sh to update this file.'
echo "         --> MATLAB = $MATLAB"
echo "---------------------------------------------------------------------------"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        fi
        return 0
    }
#=======================================================================
    check_license () { # Check the license file.
		       # 1. If there is at least one port@host then
		       #    continue.
		       # 2. If no port@host and a single file then
		       #    continue.
		       # 3. Otherwise check all the files on the
		       #    ":" separated list for existence.
		       #
                       # Return a zero status unless a ":" separated
		       # list, no port@host, and ALL files don't exist.
                       #
                       # usage: check_license
                       #
#
# LM_LICENSE_fILE can be a ":" separated list. Check first for a port@host.
# If there is at least one then skip the rest of the checking. Otherwise, check
# the whole list and only print something if they are all bad.
#
	licensefilelist=`echo $LM_LICENSE_FILE | tr ':' ' '`
	nbadfiles=0
	nfiles=0
        for licensefile in $licensefilelist
        do
            if [ `expr "$licensefile" : '.*@.*'` -ne 0 ]; then
	        return 0
	    elif [ ! -f $licensefile ]; then
	        nbadfiles=`expr $nbadfiles + 1`
	    fi
	    nfiles=`expr $nfiles + 1`
        done
        if [ "$nfiles" = "$nbadfiles" -a "$nfiles" != "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    matlab: license files in LM_LICENSE_FILE path do not exist'
    echo ''
    echo '            $LM_LICENSE_FILE'" = $LM_LICENSE_FILE"
    echo ''       
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            for licensefile in $licensefilelist
            do
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "            file = $licensefile"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            done
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            return 1
        fi
	return 0
    }
#=======================================================================
    build_cmd () { # Takes the cmd input string and outputs the same
                   # string correctly quoted to be evaluated again.
                   #
                   # Always returns a 0
                   #
                   # usage: build_cmd
                   #
        echo "$cmd" | awk '
#----------------------------------------------------------------------------
        BEGIN { squote = sprintf ("%c", 39)   # set single quote
                dquote = sprintf ("%c", 34)   # set double quote
              }
NF != 0 { newarg=dquote                 # initialize output string to
                                        # double quote
          lookquote=dquote              # look for double 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 == dquote)
                lookquote = squote
             else
                lookquote = dquote
             newarg = newarg lookquote
          }
          printf " %s", newarg oldarg lookquote }'
#----------------------------------------------------------------------------
        return 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/..
	    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
#
#**************************************************************************
#
# Do not use ARCH if it exists in the environment
#
    ARCH=""
#
    RCSHEADER='$Revision: 1.77 $  $Date: 2000/09/02 22:11:36 $'
#
    USAGE1="${command} [-h|-help] | [-n] [-arch | -ext | -arch/ext]"
    USAGE2="       [-c licensefile] [-display Xdisplay | -nodisplay]"
    USAGE3="       [-nosplash] [-mwvisual visualid] [-debug]"
    USAGE4="       [-nodesktop | -nojvm] [-runtime] [-check_malloc]"
    USAGE5="       [-r MATLAB_command] [-Ddebugger [options]]" 
#
# Parse the arguments
#
    stat="OK"
    showenv=0
    EXT=""
    arglist=""
    arglist2=
    check_malloc=0
    jdbflag=0
    jvmflag=1
    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
 		;;
	    -nodisplay)
		arglist="$arglist $1"
		;;
	    -check_malloc)
	        check_malloc=1
		;;
	    -D*)
		debugger=`expr "$1" : '-D\(.*\)'`
		if [ "$debugger" = "" ]; then
		    stat=""
		else
		    MATLAB_DEBUG=`expr "//$debugger" : ".*/\(.*\)"`
		fi
		;;
	    -jdb)
		arglist="$arglist $1"
		jdbflag=1
		;;
	    -nojvm)
		arglist="$arglist $1"
		jvmflag=0
		;;
	    -r)
		if [ $# -eq 1 ]; then
		    stat=""
		else
		    arglist="$arglist $1"
		    shift
		    cmd="$1"
		    quoted_cmd=`build_cmd`
		    arglist="$arglist `echo $quoted_cmd`"
		fi
 		;;
	    -*/*)
		found=0
#
# Test options if no debugger.
#
                if [ "$debugger" = "" ]; then
                    value=`expr "$1" : '-\(.*\)/.*'`
                    ext=`expr "$1" : '-.*/\(.*\)'`
		    arch=$ARCH
		    check_archlist argument=-$value noprint
		    if [ "$ARCH" != "" -a -f $MATLABdefault/bin/$value/matlab.$ext ]; then
			EXT=$ext
			found=1
		    else
			ARCH=$arch
		    fi
		fi
		if [ "$found" = "0" ]; then
                    arglist="$arglist $1"
		fi
		;;
	    -*)
		found=0
#
# Test options if no debugger.
#
                if [ "$debugger" = "" ]; then
		    arch=$ARCH
		    check_archlist argument=$1 noprint
		    if [ "$ARCH" != "" ]; then
			if [ "$EXT" != "" ]; then
  			    if [ ! -f $MATLABdefault/bin/$ARCH/matlab.$EXT ]; then
				EXT=""
			    fi
			fi
			found=1
		    else
			ARCH=$arch
		    fi
#
# Check for extension
#
		    if [ "$found" = "0" ]; then
		        ext=`expr "$1" : '-\(.*\)'`
			if [ "$ARCH" = "" ]; then
  			    . $MATLABdefault/bin/util/arch.sh
			fi
  			if [ -f $MATLABdefault/bin/$ARCH/matlab.$ext ]; then
  			    EXT=$ext
  			    found=1
			fi
		    fi
                fi
		if [ "$found" = "0" ]; then
                    arglist="$arglist $1"
		fi
                ;;
	    *)
		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 "            $USAGE4"
    echo "            $USAGE5"
    echo ""
    echo "    -h|-help             - Display arguments." 
    echo "    -n                   - Display final environment variables,"
    echo "                           arguments, and other diagnostic information."
    echo "    -arch                - Start MATLAB assuming architecture arch."
    echo "    -ext                 - Start the version of MATLAB with"
    echo "                           extension ext if it exists."
    echo "    -arch/ext            - Start the version of MATLAB with"
    echo "                           extension ext if it exists assuming"
    echo "                           architecture arch."
    echo "    -c licensefile       - Set LM_LICENSE_FILE to licensefile."
    echo "                           It can have the form port@host."
    echo "    -display Xdisplay    - Send X commands to X server display, Xdisplay."
    echo "    -nodisplay           - Do not display any X commands. The MATLAB"
    echo "                           desktop will not be started. However, unless"
    echo "                           -nojvm is also provided the Java virtual machine"
    echo "                           will be started."
    echo "    -nosplash            - Do not display the splash screen during startup."
    echo "    -mwvisual visualid   - The default X visual to use for figure windows."
    echo "    -debug               - Provide debugging information especially for X"
    echo "                           based problems."
    echo "    -nodesktop           - Do not start the MATLAB desktop. Use the current"
    echo "                           terminal for commands. The Java virtual machine"
    echo "                           will be started."
    echo "    -nojvm               - Shut off all Java support by not starting the"
    echo "                           Java virtual machine. In particular the MATLAB"
    echo "                           desktop will not be started."
    echo "    -runtime		   - Start MATLAB in Runtime Server mode. Required only"
    echo "                           if MATLAB is run from the Runtime Development Kit."
    echo "    -check_malloc        - Turn on MATLAB memory integrity checking."
    echo "    -r MATLAB_command    - Start MATLAB and execute the MATLAB_command."
    echo "    -Ddebugger [options] - Start debugger to debug MATLAB."
    echo ""
    echo "    Revision #: $RCSREVISION"
    echo ""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Determine what is set in the environment
#
    AUTOMOUNT_MAPenv="$AUTOMOUNT_MAP"
    DISPLAYenv="$DISPLAY"
    LM_LICENSE_FILEenv="$LM_LICENSE_FILE"
    TOOLBOXenv="$TOOLBOX"
    MATLABPATHenv="$MATLABPATH"
    XAPPLRESDIRenv="$XAPPLRESDIR"
    XKEYSYMDBenv="$XKEYSYMDB"
    MATLAB_MEM_MGRenv="$MATLAB_MEM_MGR"
    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/X11/app-defaults'
    XKEYSYMDBdefault='$MATLAB/X11/app-defaults/XKeysymDB'
#
    MATLAB_MEM_MGRdefault=''
#
    SHELLdefault='$SHELL'
#
    MATLAB_UTIL_DIRdefault=$MATLABdefault/bin/util
#
# Feature state variables
#
#--------------------------------------------------------------------------
#
# Source file .matlab6rc.sh and get values for the following environment
# variables
#
#       ARCH                    (machine architecture)
#       AUTOMOUNT_MAP           (Path prefix map for automounting)
#       DISPLAY                 (DISPLAY variable for X Window System)
#       LDPATH_PREFIX           (path(s) that appear at the start of
#       			 LD_LIBRARY_PATH)
#       LDPATH_SUFFIX           (path(s) that appear at the end of
#       			 LD_LIBRARY_PATH)
#       LD_LIBRARY_PATH         (load library path - the name
#       			 LD_LIBRARY_PATH is platform dependent)
#       LM_LICENSE_FILE         (FLEXlm license file)
#       MATLAB                  (MATLAB root directory)
#       MATLABPATH              (MATLAB search path)
#       SHELL                   (which shell to use for ! and unix
#       			 command in MATLAB)
#       TOOLBOX                 (toolbox path)
#       XAPPLRESDIR             (X Application Resource Directory)
#       XKEYSYMDB               (X keysym Database file)
#
# The search order for .matlab6rc.sh is:
#
#       .               (current directory)
#       $HOME           (users home directory)
#       $MATLAB/bin     (MATLAB bin directory)
#
    if [ -f .matlab6rc.sh ]; then
        SOURCED_DIR='.'
        SOURCED_DIReval=`pwd`
        . $cpath/.matlab6rc.sh
    elif [ -f $HOME/.matlab6rc.sh ]; then
        SOURCED_DIR='$HOME'
        SOURCED_DIReval=$HOME
        . $HOME/.matlab6rc.sh
    elif [ -f $MATLABdefault/bin/.matlab6rc.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/.matlab6rc.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 ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            exit 1
        fi
        MATLAB="$MATLABsave"
#
	ARCHdefault=$ARCH
    fi
#
#--------------------------------------------------------------------------
#
# Determine the final values for the following variables
#
#       ARCH                    (machine architecture)
#       AUTOMOUNT_MAP           (Path prefix map for automounting)
#       DISPLAY                 (DISPLAY variable for X Window System)
#       LM_LICENSE_FILE         (FLEXlm license file)
#       MATLAB                  (MATLAB root directory)
#       MATLAB_MEM_MGR          (Type of memory manager)
#       MATLAB_DEBUG		(name of the debugger from -Ddebugger argument)
#       MATLABPATH              (MATLAB search path)
#       SHELL                   (which shell to use for ! and unix
#       			 command in MATLAB)
#       TOOLBOX                 (toolbox path)
#       XAPPLRESDIR             (X Application Resource Directory)
#       XKEYSYMDB               (X keysym Database file)
#
    rcfile='r '
    environ='e '
    script='s '
    argument='a '
    rcfilep='rs'
    environp='es'
#
# Sourced a .matlab6rc.sh file
#
    if [ "$SOURCED_DIR" != "" ]; then
	if [ "$AUTOMOUNT_MAP" != "" ]; then
	    if [ "$MATLAB" != "$MATLABdefault" -a "$AUTOMOUNT_MAPenv" != "" ]; then
                AUTOMOUNT_MAPmode="$environ"
	    else
                AUTOMOUNT_MAPmode="$rcfile"
	    fi
	else
            AUTOMOUNT_MAPmode="$script"
	    AUTOMOUNT_MAP="$AUTOMOUNT_MAPdefault"
	fi
#
        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 [ "$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 [ "$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
#
	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
#
# For MATLAB_MEM_MGR:
#
#	 1. check_malloc argument
#        2. rcfile (not currently set)
#	 3. environment
#	 4. default (empty)
#
	if [ "$check_malloc" = "1" ]; then
	    MATLAB_MEM_MGRmode="$argument"
	    MATLAB_MEM_MGR='debug'
	elif [ "$MATLAB_MEM_MGR" != "" ]; then
	    if [ "$MATLAB_MEM_MGRenv" = "$MATLAB_MEM_MGR" ]; then
	        MATLAB_MEM_MGRmode="$environ"
	    else
	        MATLAB_MEM_MGRmode="$rcfile"
	    fi
	else
	    MATLAB_MEM_MGRmode="$script"
	    MATLAB_MEM_MGR="$MATLAB_MEM_MGRdefault"
	fi
#
	if [ "$MATLAB_DEBUG" = "" ]; then
            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
	if [ "$AUTOMOUNT_MAPenv" != "" ]; then
    	    AUTOMOUNT_MAPmode="$environ"
	    AUTOMOUNT_MAP="$AUTOMOUNT_MAPenv"
	else
    	    AUTOMOUNT_MAPmode="$script"
	    AUTOMOUNT_MAP="$AUTOMOUNT_MAPdefault"
	fi
	MATLABmode="$script"
        if [ "$AUTOMOUNT_MAP" != "" ]; then
            MATLAB=`echo $MATLABdefault $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}'`
	else
	    MATLAB="$MATLABdefault"
        fi
	if [ "$display" != "" ]; then
            DISPLAYmode="$argument"
	    DISPLAY="$display"
	else
            DISPLAYmode="$environ"
	    DISPLAY="$DISPLAYenv"
	fi
        ARCHmode="$script"
	ARCH="$ARCHdefault"
	if [ "$licensefile" != "" ]; then
	    LM_LICENSE_FILE="$licensefile"
            LM_LICENSE_FILEmode="$argument"
	elif [ "$LM_LICENSE_FILEenv" != "" ]; then
	    LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILEenv:$LM_LICENSE_FILEdefault`"
            LM_LICENSE_FILEmode="$environp"
	else
	    LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILEdefault`"
            LM_LICENSE_FILEmode="$script"
	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 [ "$check_malloc" = "1" ]; then
	    MATLAB_MEM_MGRmode="$argument"
	    MATLAB_MEM_MGR='debug'
	elif [ "$MATLAB_MEM_MGR" != "" ]; then
	    MATLAB_MEM_MGRmode="$environ"
	else
	    MATLAB_MEM_MGRmode="$script"
	    MATLAB_MEM_MGR="$MATLAB_MEM_MGRdefault"
        fi
	if [ "$MATLAB_DEBUG" = "" ]; then
            MATLAB_DEBUGmode="$script"
	else
            MATLAB_DEBUGmode="$argument"
	fi
        SHELLmode="$environ"
	SHELL="$SHELLenv"
    fi
#
#--------------------------------------------------------------------------
#
# Check rc_file
#
    if [ "$SOURCED_DIR" = '.' ]; then
	check_rc_file $cpath/.matlab6rc.sh
    elif [ "$SOURCED_DIR" = '$HOME' ]; then
	check_rc_file $HOME/.matlab6rc.sh
    fi
#
# Determine which version of MATLAB to run.
#
    PROGRAM=`program $ARCH $EXT`

#
# Determine which version of BLAS to use if not already specified
#
    if [ "$BLAS_VERSION" = "" ]; then
	case "$ARCH" in
	    sol2)
		if [ "`uname -m`" != "sun4u" ]; then
		    BLAS_VERSION=atlas_SunSuper.so
		    export BLAS_VERSION
		fi
		;;
	esac
    fi

#
#
# Determine the final values for the following variables
#
#       LD_LIBRARY_PATH         (load library path - the name
#       			 LD_LIBRARY_PATH is platform dependent)
#	_JVM_THREADS_TYPE	(type of Java virtual machine threads)
#
#--------------------------------------------------------------------------
#
# Determine the java vm path for each platform.
#
    case "$ARCH" in
	sol2)
	    JARCH="sparc"
	    ;;
	hpux)
	    JARCH="PA_RISC2.0"
	    ;;
	alpha)
	    JARCH="alpha"
	    ;;
	ibm_rs)
	    JARCH="aix"
	    ;;
	sgi)
	    JARCH="sgi"
	    ;;
	glnx86)
	    JARCH="i386"
	    ;;
	*)
	    JARCH=$ARCH
	    ;;
    esac
#
    DEFAULT_JRE_LOC=$MATLAB/sys/java/jre/$ARCH/jre
    JRE_LOC=$DEFAULT_JRE_LOC${MATLAB_JAVA:-}
    if [ $jdbflag -eq 1 ]; then
	DEFAULT_JDK_LOC=$MATLAB/java/jdk/$ARCH/jdk
	JDK_LOC=$DEFAULT_JDK_LOC${MATLAB_JAVA:-}
	JRE_LOC=$JDK_LOC
	if [ -d "$JRE_LOC/jre" ]; then
	    JRE_LOC=$JRE_LOC/jre
	fi
    fi
    if [ ! -d "$JRE_LOC" ]; then
	JRE_LOC=$MATLAB_JAVA
    fi
    if [ -d "$JRE_LOC" -a "$ARCH" = "glnx86" ]; then
        hasOldVM=`ls -ld $JRE_LOC | awk '{print $NF}' | grep -c '1.1.7'`
        if [ $hasOldVM -eq 1 -a $jvmflag -eq 1 ]; then
#
# 1.1.7 VM requires us to fork at startup
#
            arglist="$arglist -fork"
#
# 1.1.7 VM + newer kernel requires 5-digit PIDs
#
            hasOldProc=`ls -l /proc/01 2>/dev/null | grep -c cmdline`
            if [ $hasOldProc -eq 0 ]; then
                pid=`sh -c 'echo $$'` ; needpid=10000
                if [ $pid -lt $needpid ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "---------------------------------------------------------------------------"
echo "Kernel 2.2.12-20 or newer causes problems with Blackdown 1.1.7v1a JRE";
echo "and PIDs below 10000.  Working around the problem, please wait . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    dots='........................................................................'
                    count=`echo 1+\($needpid-$pid\)/145 | bc`
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo -ne "`echo $dots | cut -c1-$count`\r"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    while [ $pid -lt $needpid ]; do
                        for i in 0 1 2 3 4 5 6 7 8 9 a b; do
                            for j in 0 1 2 3 4 5 6 7 8 9 a b ; do
                                /bin/echo -n
                            done
                        done
                        pid=`sh -c 'echo $$'`
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo -n o
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    done
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    if [ "$showenv" != "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "---------------------------------------------------------------------------"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    fi
                fi
            fi
        fi
    fi
#
# Threads
#
    case "$ARCH" in
        sgi)
            # SGI vm includes O32 and N32 binaries.  Need N32
            JLIB=lib32
            ;;
       *)
            JLIB=lib
            ;;
    esac
    JAVA_VM_PATH="$JRE_LOC/$JLIB/$JARCH/native_threads"
#
# JVM
#
    JVM_HOTSPOT="$JRE_LOC/$JLIB/$JARCH/hotspot"
    JVM_CLASSIC="$JRE_LOC/$JLIB/$JARCH/classic"
    if [ -d $JVM_HOTSPOT ]; then
	JAVA_VM_PATH="$JAVA_VM_PATH:$JVM_HOTSPOT"
    elif [ -d $JVM_CLASSIC ]; then
	JAVA_VM_PATH="$JAVA_VM_PATH:$JVM_CLASSIC"
	_JVM_THREADS_TYPE=native_threads; export _JVM_THREADS_TYPE
    fi
    JAVA_VM_PATH="$JAVA_VM_PATH:$JRE_LOC/$JLIB/$JARCH"
#
# Augment with AWT Motif default locale resource files
#
    XFILESEARCHPATH="$JRE_LOC/lib/locale/%L/%T/%N%S:$XFILESEARCHPATH"
    export XFILESEARCHPATH

#
# If the display has no GLX extension then use Mesa instead of any system libs.
#
     HAS_SYSTEM_GL=`(xdpyinfo -display "$DISPLAY") 2>&1 | grep GLX`;
     if [ "$HAS_SYSTEM_GL" = "" ]; then
         if [ "$LDPATH_PREFIX" != "" ]; then
             LDPATH_PREFIX=$LDPATH_PREFIX:'$MATLAB/sys/opengl/lib/$ARCH'
         else
             LDPATH_PREFIX='$MATLAB/sys/opengl/lib/$ARCH'
         fi
     fi

#
#--------------------------------------------------------------------------
#
# Determine <final_load_library_path> for each platform
#
    case "$ARCH" in
	sol2|alpha|glnx86)
	    LD_LIBRARY_PATH="`eval echo $LD_LIBRARY_PATH`"
	    LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH:$JAVA_VM_PATH
	    if [ "$LD_LIBRARY_PATH" != "" ]; then
		LD_LIBRARY_PATH=$LDPATH_MATLAB:$LD_LIBRARY_PATH
                LD_LIB_PATHmode="$rcfilep"
	    else
		LD_LIBRARY_PATH=$LDPATH_MATLAB
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export LD_LIBRARY_PATH
	    ;;
	hpux)
	    SHLIB_PATH="`eval echo $SHLIB_PATH`"
	    LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH:$JAVA_VM_PATH
	    if [ "$SHLIB_PATH" != "" ]; then
		SHLIB_PATH=$LDPATH_MATLAB:$SHLIB_PATH
                LD_LIB_PATHmode="$rcfilep"
	    else
		SHLIB_PATH=$LDPATH_MATLAB
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    SHLIB_PATH=$LDPATH_PREFIX:$SHLIB_PATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    SHLIB_PATH=$SHLIB_PATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export SHLIB_PATH
	    ;;
	hp700)
	    SHLIB_PATH="`eval echo $SHLIB_PATH`"
	    LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH
	    if [ "$SHLIB_PATH" != "" ]; then
		SHLIB_PATH=$LDPATH_MATLAB:$SHLIB_PATH
                LD_LIB_PATHmode="$rcfilep"
	    else
		SHLIB_PATH=$LDPATH_MATLAB
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    SHLIB_PATH=$LDPATH_PREFIX:$SHLIB_PATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    SHLIB_PATH=$SHLIB_PATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export SHLIB_PATH
	    ;;
	ibm_rs)
#
# IBM does their own thing when it comes to Java
#
	    JAVA_VM_PATH="$JRE_LOC/bin:$JRE_LOC/bin/classic"
#
	    LIBPATH="`eval echo $LIBPATH`"
            if [ "matlab.$EXT" = "$PROGRAM" -a -f $MATLAB/bin/$ARCH/$EXT/matlab ]; then
		LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH/$EXT:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH:$JAVA_VM_PATH
            else
                LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH:$JAVA_VM_PATH
            fi
	    LDPATH_SYSTEM=/usr/lib:/lib
	    if [ "$LIBPATH" != "" ]; then
		LIBPATH=$LDPATH_MATLAB:$LIBPATH:$LDPATH_SYSTEM
                LD_LIB_PATHmode="$rcfilep"
	    else
		LIBPATH=$LDPATH_MATLAB:$LDPATH_SYSTEM
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    LIBPATH=$LDPATH_PREFIX:$LIBPATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    LIBPATH=$LIBPATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export LIBPATH
	    ;;
	sgi)
	    LD_LIBRARY_PATH="`eval echo $LD_LIBRARY_PATH`"
	    LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH:$JAVA_VM_PATH
	    if [ "$LD_LIBRARY_PATH" != "" ]; then
		LD_LIBRARY_PATH=$LDPATH_MATLAB:$LD_LIBRARY_PATH
                LD_LIB_PATHmode="$rcfilep"
	    else
		LD_LIBRARY_PATH=$LDPATH_MATLAB
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export LD_LIBRARY_PATH
	    ;;
	*)
	    LD_LIBRARY_PATH="`eval echo $LD_LIBRARY_PATH`"
	    LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH
	    if [ "$LD_LIBRARY_PATH" != "" ]; then
		LD_LIBRARY_PATH=$LDPATH_MATLAB:$LD_LIBRARY_PATH
                LD_LIB_PATHmode="$rcfilep"
	    else
		LD_LIBRARY_PATH=$LDPATH_MATLAB
                LD_LIB_PATHmode="$script"
	    fi
	    if [ "$LDPATH_PREFIX" != "" ]; then
	        LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	        if [ "$LDPATH_PREFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    if [ "$LDPATH_SUFFIX" != "" ]; then
	        LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	        if [ "$LDPATH_SUFFIX" != "" ]; then
                    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LDPATH_SUFFIX
                    LD_LIB_PATHmode="$rcfilep"
		fi
	    fi
	    export LD_LIBRARY_PATH
	    ;;
    esac
#--------------------------------------------------------------------------
#
# On Alpha, set unaligned access control to produce a bus error.
#
    if [ "$ARCH" = "alpha" ]; then
# To fix unaligned accesses and attempt to continue, use the following line
# instead:
#       uac p noprint
        uac p nofix noprint sigbus
    fi
#
#--------------------------------------------------------------------------
#
# 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
#
#--------------------------------------------------------------------------
#
# Add on $HOME/matlab if available and $MATLAB/toolbox/local
#

    if [ -d $HOME/matlab ]; then
	MATLABPATH=$MATLABPATH:$HOME/matlab
    fi
    MATLABPATH=$MATLABPATH:$MATLAB/toolbox/local
#
# Remove any leading ":" character from the path. Can't use awk
# here because it fails on very long paths.
#
    MATLABPATH=`echo $MATLABPATH | sed 's/^://'`
#
#--------------------------------------------------------------------------
#
# Check OS version
#
    if [ -f $MATLAB_UTIL_DIR/oscheck.sh ]; then
	. $MATLAB_UTIL_DIR/oscheck.sh
	if [ "$oscheck_status" = "1" ]; then
	    exit 1
	fi
    fi
#
#--------------------------------------------------------------------------
#
    if [ "$showenv" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    (
    echo '------------------------------------------------------------------------'
	if [ "$SOURCED_DIR" != "" ]; then
    echo "->      (.matlab6rc.sh) sourced from directory (DIR = $SOURCED_DIR)"
    echo "->      DIR = $SOURCED_DIReval"
	else
    echo "->      (.matlab6rc.sh) not found."
	fi
    echo '------------------------------------------------------------------------'
    echo '        a = argument  e = environment  r = rcfile  s = script'
    echo '------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "->  $MATLABmode  MATLAB              = $MATLAB"
    echo "->  $AUTOMOUNT_MAPmode  AUTOMOUNT_MAP       = $AUTOMOUNT_MAP"
    echo "->  $DISPLAYmode  DISPLAY             = $DISPLAY"
    echo "->  $ARCHmode  ARCH                = $ARCH"
    echo "->  $LM_LICENSE_FILEmode  LM_LICENSE_FILE     = $LM_LICENSE_FILE"
    echo "->  $TOOLBOXmode  TOOLBOX             = $TOOLBOX"
	if [ "$XUSERFILESEARCHPATH" != "" ]; then
    echo "->  $XAPPLRESDIRmode  XUSERFILESEARCHPATH = $XUSERFILESEARCHPATH"
	else
    echo "->  $XAPPLRESDIRmode  XAPPLRESDIR         = $XAPPLRESDIR"
	fi
    echo "->  $XKEYSYMDBmode  XKEYSYMDB           = $XKEYSYMDB"
#
# For java
#
    echo "->  s   _JVM_THREADS_TYPE   = $_JVM_THREADS_TYPE"
    echo "->  e   MATLAB_JAVA         = $MATLAB_JAVA"
#
    echo "->  $MATLAB_MEM_MGRmode  MATLAB_MEM_MGR      = $MATLAB_MEM_MGR"
    echo "->  $MATLAB_DEBUGmode  MATLAB_DEBUG        = $MATLAB_DEBUG"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	case "$ARCH" in
	    sol2|alpha|glnx86)
    echo "->  $LD_LIB_PATHmode  LD_LIBRARY_PATH     = $LD_LIBRARY_PATH"
		;;
	    hpux)
    echo "->  $LD_LIB_PATHmode  SHLIB_PATH          = $SHLIB_PATH"
		;;
	    hp700)
    echo "->  $LD_LIB_PATHmode  SHLIB_PATH          = $SHLIB_PATH"
		;;
	    ibm_rs)
    echo "->  $LD_LIB_PATHmode  LIBPATH             = $LIBPATH"
		;;
	    sgi)
    echo "->  $LD_LIB_PATHmode  LD_LIBRARY_PATH     = $LD_LIBRARY_PATH"
		;;
	    *)
    echo "->  $LD_LIB_PATHmode  LD_LIBRARY_PATH     = $LD_LIBRARY_PATH"
		;;
	esac
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "->  $argument  arglist             = $arglist"
    echo "->  $SHELLmode  SHELL               = $SHELL"
    echo "->  e   PATH                = $PATH"					
    echo " "									) > $temp_file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fi
#
# Cannot locate JRE
#
    if [ ! -d "$JRE_LOC" -a "$jvmflag" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(
echo "---------------------------------------------------------------------------"
echo "Warning: Cannot locate Java Runtime Environment (JRE) . . ."
echo " "
echo "         1. Either a correct JRE was not available for redistribution when"
echo "            this release was shipped, in which case you should refer to the"
echo "            Release Notes for additional information about how to get it."
echo " "
echo "         2. Or you have tried to use the MATLAB_JAVA environment variable"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if [ "$showenv" != "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "            to specify an alternate JRE, but MATLAB cannot find it.  Please"
echo "            run 'matlab -n' to determine what value you are using for"
echo "            MATLAB_JAVA and fix accordingly."                                
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "            to specify an alternate JRE, but MATLAB cannot find it.  Check"
echo "            the value of MATLAB_JAVA above and fix accordingly."                                
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
echo "---------------------------------------------------------------------------" ) >> $temp_file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if [ "$showenv" != "1" ]; then
	    cat $temp_file
	    rm -f $temp_file
	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo " "									>> $temp_file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
    fi
#
    if [ "$showenv" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(
    echo "->  $MATLABPATHmode  MATLABPATH          = (initial version)"
	if [ "$MATLABPATH" != "" ]; then
	    for dir in `echo $MATLABPATH | tr ':' ' '`
	    do
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "	$dir"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    done
	fi
#
    echo " "
        if [ -f $MATLAB/bin/$ARCH/$PROGRAM -a -f $MATLAB/bin/ldd ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '->      $MATLAB/bin/'"$ARCH/$PROGRAM shared library information -"
    echo "|-----------------------------------------------------------------------"
            $MATLAB/bin/ldd -$ARCH $MATLAB/bin/$ARCH/$PROGRAM 2>/dev/null | awk '{ print "| " $0 }'
    echo "|-----------------------------------------------------------------------"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        fi
#
    echo " "
    echo '->      $MATLAB/toolbox/local/pathdef.m -'
    echo "|-----------------------------------------------------------------------"
	if [ -f $MATLAB/toolbox/local/pathdef.m ]; then
	    cat $MATLAB/toolbox/local/pathdef.m | awk '{ print "| " $0 }'
	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    Warning: $MATLAB/toolbox/local/pathdef.m not found . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
    echo "|-----------------------------------------------------------------------"
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '------------------------------------------------------------------------') >> $temp_file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	more $temp_file
        rm -f $temp_file
	exit 0
    fi
#
# Export the variables
#
    export MATLAB
    export AUTOMOUNT_MAP
    export DISPLAY
    export ARCH
    export LM_LICENSE_FILE
    export TOOLBOX
    export MATLABPATH
    export XAPPLRESDIR
    export XKEYSYMDB
    if [ "$MATLAB_MEM_MGR" != "" ]; then
        export MATLAB_MEM_MGR
    fi
    export MATLAB_DEBUG
    export SHELL
#
    check_license
    if [ $? -ne 0 ]; then
	exit 1
    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/$PROGRAM ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "	matlab: No MATLAB executable for this machine architecture."
    echo ''       
    echo "           $MATLAB/bin/$ARCH/$PROGRAM does not exist!"
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi
#
# Fully evaluate the MATLAB executable path to handle cases when the
# name of executable must be matlab. The only known problems are
# on ibm_rs.
#
    if [ "$ARCH" != "ibm_rs" ]; then
        exec_path=$MATLAB/bin/$ARCH/$PROGRAM
    else
        exec_path=`actualpath $MATLAB/bin/$ARCH/$PROGRAM`
    fi
#
    case "$ARCH" in
        sol2|hpux|hp700|alpha|ibm_rs|sgi)
	    if [ -d $MATLAB/rtw/bin/$ARCH ]; then
	        PATH=$MATLAB/rtw/bin/$ARCH:$PATH; export PATH
	    fi
            if [ "$debugger" != "" ]; then
                if [ "$MATLAB_DEBUG" = "gdb" -o "$MATLAB_DEBUG" = "xxgdb" ]; then
                    SHELL=/bin/sh; export SHELL
                fi
		eval exec $debugger $exec_path $arglist
            else
		eval exec $exec_path $arglist
            fi
	    ;;
        *)
            if [ "$debugger" != "" ]; then
                if [ "$MATLAB_DEBUG" = "gdb" -o "$MATLAB_DEBUG" = "xxgdb" ]; then
                    SHELL=/bin/sh; export SHELL
                fi
		eval exec $debugger $exec_path $arglist
            else
		eval exec $exec_path $arglist
            fi
	    ;;
    esac
