#!/bin/sh
#
#  Name:
#
#      mcc    script file for invoking MCC
#
#  Usage:
#
#      mcc [arguments] -help | [arguments] -? | -cache |
#          -rmcache | [-arch] [-cache | -nocache] [-v]
#          [-Y license_file] arguments ...
#
#  Description:
#
#      This Bourne Shell script invokes the MATLAB compiler.
#      It uses a cache file to speed up execution.  The cache file
#      contains precomputed values of critical 'mcc' environment
#      variables.  It is automatically built whenever the backend
#      is called provided that the cache file does not exist and
#      and -nocache is not used.  Later executions of 'mcc' will
#      use it unless overridden by the '-nocache' argument.
#
#      The cache file is located in:
#
#          $HOME/.matlab/<rel_version>/mcc.cache/<revision>:<hostname:<cksum>
#
#          <rel_version> - Base release version like R14 . . .
#	   <revision>    - RCS revision of this script
#	   <hostname>    - name of host
#	   <cksum>       - echo dirpath($0) | cksum
#
#  Options:
#
#      -help             - Help. It prints this help then the mcc
#		           backend help. The optional arguments
#			   control the backend help.
#
#      -?                - Prints the mcc backend help only. The
#			   optional arguments control the help."
#
#      -cache		 - Print the cache file if the only argument. 
#			   With other arguments rebuild the cache.
#
#      -arch             - Assume local host has architecture arch.  
#			   Rebuilds the cache file unless -nocache
#			   is used.
#
#      -rmcache          - Remove the cache file.
# 
#      -nocache          - Ignore the cache file.
#
#      -v		 - Verbose mode. The mcc backend also supports
#			   this argument and it is passed through to the
#			   mcc backend.
#
#      -Y license_file   - Override default license file with
#			   license_file. license_file can be a colon
#			   separated list of files or port@host entries.
#			   The mcc backend also supports this argument.
#
#      arguments ...     - These are arguments to the mcc backend.
#                          Use -help or -? for the syntax. 
#
#  Copyright 1998-2003 The MathWorks, Inc.
#  $Revision: 1.24.4.7 $  $Date: 2004/12/22 19:52:41 $
#__________________________________________________________________________
#
    arg0_=$0
    tmpbase=/tmp/mcc.$LOGNAME.$$
#
    trap "rm -f $tmpbase.[a-d]; rm_trash_shfile; exit 1" 1 2 3 15
#
    cache_body=$tmpbase.b
#
# For oscheck.sh
#
    temp_file=$tmpbase.c
#
# Do not use ARCH if it exists in the environment
#
    ARCH=""
#
#========================= 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-2004 The MathWorks, Inc.
# $Revision: 1.9.4.4 $  $Date: 2004/07/08 01:06:10 $
#----------------------------------------------------------------------------
#
    ARCH_LIST='sol2 hpux glnx86 glnxi64 glnxa64 mac'
#=======================================================================
# 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) ==============================
#
#========================== version.sh (start) ============================
#
# usage:        version [-b]
#
# abstract:     This POSIX Shell script function returns the MATLAB
#               release version. Any bad options are ignored without
#		warning.
#
#		The version is assumed to be of the form:
#
#		    R<integer>[<noninteger>]
#
#               options:
#
#		b       - return just R<integer> or the base version.
#
# note(s):      1. This file is always imbedded in another script
#
# Copyright 1992-2003 The MathWorks, Inc.
# $Revision: 1.1.6.2 $  $Date: 2004/07/08 01:06:11 $
#----------------------------------------------------------------------------
#
version () {
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ver='R14'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    if [ $# -eq 1 ]; then
        if [ "$1" = '-b' ]; then
            expr "$ver" : '\(R[0-9]*\).*$'
	    return $?
        fi
    fi
    echo "$ver"
    return 0
}
#========================== version.sh (end) ==============================
    REL_VERSION=`version -b`
#
#=======================================================================
# Functions:
#   scriptpath ()
#   source_cache ()
#   get_cache_file ()
#   gethostname ()
#   dirpath_arg0 ()
#   remove_cache ()
#   get_arch ()
#   set_mcc_env ()
#   print_cache ()
#   check_env ()
#   check_matlab_bin ()
#   check_mcc
#   set_os_env ()
#   build_cache ()
#   cache_body_template ()
#   build_arglist ()
#   newawk ()
#   check_Xdisplay ()
#
#   rm_trash_shfile ()
#=======================================================================
    scriptpath () { # Returns path of this script as a directory,
                    # ROOTDIR, and command name, CMDNAME.
		    #
		    # Returns a 0 status unless an error.
		    #
                    # usage: scriptpath
                    #
#
	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 '                  this command.'
    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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 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 '                  this command.'
    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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        return 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"
	        CMDNAME="$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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
        cd $cpath
        return 0
    }
#=======================================================================
    source_cache () { # Sources the cache file if it exists and nocache
		      # is not set.
		      #
		      # The cache file is a Bourne Shell script that is
		      # source. Variables are assigned and exported.
		      #
		      # The DISPLAY variable is not used from the cache
		      # unless it is not set.
                      #
                      # Returns a 0 status unless it doesn't exist.
                      #
                      # usage: source_cache
                      #
	if [ "$nocache" = "0" ]; then
	    cache_file=`get_cache_file`
	    if [ -f $cache_file ]; then
		DISPLAYenv="$DISPLAY"
	        . $cache_file
		if [ "$verbose" = "1" ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "Loaded cache file: $cache_file"
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
		fi
		if [ "$DISPLAYenv" != "" ]; then
		    if [ "$DISPLAY" != "$DISPLAYenv" ]; then
		        DISPLAY="$DISPLAYenv"; export DISPLAY
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "-> DISPLAY=$DISPLAY  used from the environment, not the cache . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
		    fi
		elif [ "$DISPLAY" != "" ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "-> DISPLAY=$DISPLAY  used from the cache. No DISPLAY in the environment . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
		fi
		check_Xdisplay
	        return 0
	    else
		return 1
	    fi
	fi
	return 1
    }
#=======================================================================
    get_cache_file () { # Output the name of the cache file.
		        #
                        # Always returns a 0 status.
                        #
                        # usage: get_cache_file
                        #
	host=`gethostname`
        dirpath=`dirpath_arg0 $arg0_`
        sum=`echo $dirpath | cksum`
        sum=`expr "$sum" : '\([^ 	]*\)'`
	echo "$HOME/.matlab/$REL_VERSION/mcc.cache/$REVISION:$host:$sum"
#
	return 0
    }
#=======================================================================
    gethostname ()  { # Returns the hostname as follows:
		      #
		      # 1. HOSTNAME
		      # 2. hostname  (on path)
		      # 3. uname -a  (2nd field)
                      #
                      # Always returns a 0 status.
                      #
                      # usage: getusername
                      #
	if [ "$HOSTNAME" != "" ]; then
	    echo $HOSTNAME
	    return 0
        fi
	name=`hostname 2>/dev/null`
	if [ "$name" = "" ]; then
	    name=`uname -a`
#
# Take 2nd field
#
	    name=`expr "$uname" : '[^ 	]*[ 	]*\([^ 	]*\)'`
	fi
	echo $name
#
	return 0
    }
#=======================================================================
    dirpath_arg0 () { # Outputs the full directory name of $0 (arg0_)
		      # 
		      # Always returns a 0 status.
		      #
		      # usage: dirpath_arg0 arg0
		      #
        if [ `expr "$1" : '/*'` -ne 0 ]; then
            dirname $1
        else
	    cd `dirname $1`
	    /bin/pwd
	    cd $cpath
        fi
        return 0
    }
#=======================================================================
    remove_cache () { # Remove the cache file if it exists and it can be
		      # done.
		      #
                      # Returns a 0 status unless it failed to do it.
                      #
                      # usage: remove_cache
                      #
	cache_file=`get_cache_file`
	if [ -f $cache_file ]; then
	    rm -f $cache_file 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "Warning: Cache file could not be deleted . . ."
    echo "         file = $cache_file"
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    else
#-----------------------------------------------------------------------
    echo " "
    echo "Removed cache file: $cache_file"
    echo "-----------------------------------------------------------------"
    echo " "
#-----------------------------------------------------------------------
	    fi
	else
#-----------------------------------------------------------------------
    echo " "
    echo "No cache file to remove . . ."
    echo "-----------------------------------------------------------------"
    echo " "
#-----------------------------------------------------------------------
	fi
	return 0
    }
#=======================================================================
    get_arch () { # Determine the value of ARCH. MATLAB is assumed
		  # to be defined.
		  #
                  # Returns a 0 status unless it could not determine it.
                  #
                  # usage: get_arch
                  #
	if [ "$ARCH" = "" ]; then
	    . $MATLAB/bin/util/arch.sh
	    export ARCH
	fi
        if [ "$ARCH" = "unknown" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo 'Error: Invalid machine architecture . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
	fi
	return 0
    }
#=======================================================================
    set_mcc_env ()  { # Execute the MATLAB script with the -e option
		      # which returns all the environment variables and
		      # their values without executing MATLAB. Use
		      # these values to fill out the cache body template
		      # to make the cache body and source it to set the
		      # values in the environment.
		      #
                      # Returns a 0 status unless it failed for some
		      # reason.
                      #
                      # usage: set_mcc_env
                      #
#
# Determine ARCH
#
	get_arch
	if [ $? -ne 0 ]; then
	    return 1
	fi
	export ARCH
#
	nawk=`newawk`
#
        check_Xdisplay
#
# Call the MATLAB script to get the environment variables and their
# values. 
#
	$MATLAB/bin/matlab -e > $tmpbase.a
	status=$?
	if [ $status -ne 0 ]; then
	
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo 'Error: The MATLAB script returned a bad status while determining'
    echo   '       environment variables. There appears to be a problem'
    echo   '       with your MATLAB installation. You must fix the problem'
    echo   '       before mcc can work. Any messages from the script'
    echo   '       follow . . . '
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            cat $tmpbase.a | $nawk '
#------------------------------------------------------------------------------
    {if (index($0,"=") != 0) next; print }'
#------------------------------------------------------------------------------
	    return $status
	fi
#
# Fill out the cache file template with the values
#
	echo "EOF" >> $tmpbase.a
	cache_body_template >> $tmpbase.a
#
        cat $tmpbase.a | $nawk '
#------------------------------------------------------------------------------
    BEGIN {state = 0
	   squote = sprintf ("%c", 39)   # set single quote
	  }
    $1 == "EOF" {state = 1; next}
    state == 0 {i = index($0,"=")
                name = substr($0,1,i-1)
                value = substr($0,i+1)
                new[name] = value; next }
    state == 1 {i = match($0,"=\\$[A-Za-z_][A-Za-z0-9_]*")
		if (i == 0) { print; next } 
		name = substr($0,i+2,RLENGTH-2);
		print substr($0,1,i) squote new[name] squote substr($0,i+RLENGTH)
		}' > $cache_body
#------------------------------------------------------------------------------
	. $cache_body > /dev/null 2>&1
#
# Check the environment
#
	check_env
#
	rm -f $tmpbase.a
	return $?
    }
#=======================================================================
    print_cache () { # Output the contents of the cache file which is
		     # passed as an argument.
		     #
                     # Returns a 0 status unless no cache file exists
		     #
                     # usage: print_cache file
                     #
	if [ -f $1 ]; then
	    cat $1 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "Cache file: $1"
    echo "-----------------------------------------------------------------"
    echo "Warning: Cache file exists put could not print it . . ."
#-----------------------------------------------------------------------
		return 1
	    fi
            return 0
	else
#-----------------------------------------------------------------------
    echo "No cache file to print . . ."
    echo "-----------------------------------------------------------------"
    echo "Note: Use 'mcc -cache -build' to create the cache file to improve"
    echo "      startup performance."
#-----------------------------------------------------------------------
	fi
	return 1
    }
#=======================================================================
    check_env () { # Do a check on the environment. Check:
		   # 
		   # 1. OS check
		   # 2. MATLAB bin directory check
		   # 3. mcc binary check
		   #
		   # Returns a 0 status unless a problem occurred.
		   #
		   # usage: check_env
		   #
#
# Check OS version
#
        if [ -f $MATLAB/bin/util/oscheck.sh ]; then
	    . $MATLAB/bin/util/oscheck.sh
	    if [ "$oscheck_status" = "1" ]; then
	        return 1
	    fi
        fi
#
	check_matlab_bin
	if [ $? -ne 0 ]; then
	    return 1
	fi
#
	check_mcc
	if [ $? -ne 0 ]; then
	    return 1
	fi
#
	return 0
    }
#=======================================================================
    check_matlab_bin () { # Check that MATLAB bin directory exists for
			  # current architecture.
			  #
		          # Returns a 0 status unless it does not exist.
		          #
		          # usage: check_matlab_bin
		          # 
        if [ ! -d $MATLAB/bin/$ARCH ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "-----------------------------------------------------------------"
    echo "Error: No MATLAB bin directory for this machine architecture."
    echo "       ARCH = $ARCH" 
    echo "-----------------------------------------------------------------"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            return 1
        fi
	return 0
    }
#=======================================================================
    check_mcc () { # Check that mcc binary exists.
		   #
		   # Returns a 0 status unless it does not exist.
		   #
		   # usage: check_mcc
		   # 
        if [ ! -f $MATLAB/bin/$ARCH/mcc ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "-----------------------------------------------------------------"
    echo "Error: No MCC executable for this machine architecture."
    echo "       $MATLAB/bin/$ARCH/mcc does not exist!"
    echo "-----------------------------------------------------------------"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
	return 0
    }
#=======================================================================
    set_os_env () { # Do any setting of OS environment parameters.
		    # 
		    # Always returns a 0 status.
		    #
		    # usage: set_os_env
	return 0
    }
#=======================================================================
    build_cache () { # Build the cache. Output any status message.
		     #
                     # Returns a 0 status unless no cache could be
		     # built.
		     #
                     # usage: build_cache
                     #
        host=`gethostname`
        dirpath=`dirpath_arg0 $arg0_`
        sum=`echo $dirpath | cksum`
        sum=`expr "$sum" : '\([^ 	]*\)'`
#
	if [ ! -d "$HOME/.matlab/$REL_VERSION" ]; then
	    mkdir -p "$HOME/.matlab/$REL_VERSION" 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo 'Warning: Could not create $HOME/.matlab/'"$REL_VERSION"' directory . . .'
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    fi
	fi
	if [ ! -d "$HOME/.matlab/$REL_VERSION/mcc.cache" ]; then
	    mkdir "$HOME/.matlab/$REL_VERSION/mcc.cache" 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo 'Warning: Could not create $HOME/.matlab/'"$REL_VERSION"'/mcc.cache directory . . .'
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    fi
	fi
#
	dir="$HOME/.matlab/$REL_VERSION/mcc.cache"
        file=$REVISION:$host:$sum
	if [ -f "$dir/$file" ]; then
	    rm -f "$dir/$file" 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "Warning: Could not remove existing cache file . . ."
    echo '         file = $HOME/.matlab/'"$REL_VERSION"'/mcc.cache/'$file
    echo "         Updated cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    fi
	fi
#
	cat /dev/null > $dir/$file 2>/dev/null
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "Warning: Could not write cache file . . ."
    echo '         file = $HOME/.matlab/'"$REL_VERSION"'/mcc.cache/'$file
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	    return 1
	fi
#
	(echo "#________________________________________________________________"
	 echo "#"
	 echo "#"
	 echo "# NAME: $dir/$file"
	 echo "#________________________________________________________________"
	) > $dir/$file
        cat $cache_body >> $dir/$file
	if [ "$verbose" = "1" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "Built cache file: $dir/$file"
    echo "-----------------------------------------------------------------"
    echo " "
#-----------------------------------------------------------------------
	fi
	rm -f $cache_body
	return 0
    }
#=======================================================================
    cache_body_template () { # Output the cache body template to standard
			     # output.
			     #
			     # Always returns a 0 status
		             #
                             # usage: cache_body_template
                             #
#-----------------------------------------------------------------------
        echo "#"
	echo 'DISPLAY=$DISPLAY; export DISPLAY'
	echo "#"
        echo 'ARCH=$ARCH; export ARCH'
	echo "#"
        echo 'MATLAB=$MATLAB; export MATLAB'
	echo "#"
        echo 'AUTOMOUNT_MAP=$AUTOMOUNT_MAP; export AUTOMOUNT_MAP'
	echo "#"
#
        case "$ARCH" in
            sol2|glnx86|glnxi64)
                echo 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export LD_LIBRARY_PATH'
                ;;
            hpux)
                echo 'LD_PRELOAD=$LD_PRELOAD; export LD_PRELOAD'
                echo 'SHLIB_PATH=$SHLIB_PATH; export SHLIB_PATH'
                ;;
            mac)
                echo 'DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH; export DYLD_FALLBACK_LIBRARY_PATH'
                ;;
            *)
                echo 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export LD_LIBRARY_PATH'
                ;;
        esac
	echo "#"
	echo 'MATLABPATH=$MATLABPATH; export MATLABPATH'
	echo "#"
	echo 'BASEMATLABPATH=$BASEMATLABPATH; export BASEMATLABPATH'
	echo "#"
	echo 'TOOLBOX=$TOOLBOX; export TOOLBOX'
	echo "#"
        echo 'XAPPLRESDIR=$XAPPLRESDIR; export XAPPLRESDIR'
        echo "#"
	echo 'XFILESEARCHPATH=$XFILESEARCHPATH; export XFILESEARCHPATH'
        echo "#"
        echo 'XKEYSYMDB=$XKEYSYMDB; export XKEYSYMDB'
	echo "#________________________________________________________________"
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    build_arglist () { # The variable arglist is the input which has
		       # arguments separated by newlines. It outputs
		       # a single string correctly quoted to be evaluated.
		       # 
                       # Always returns a 0 status
                       #
                       # usage: build_arglist
                       #
	echo "$arglist" | awk '
#----------------------------------------------------------------------------
	BEGIN { squote = sprintf ("%c", 39)   # set single quote
                dquote = sprintf ("%c", 34)   # set double quote
	      }
NF != 0 { 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
          }
          printf " %s", newarg oldarg lookquote }'
#----------------------------------------------------------------------------
	return 0
    }
#=======================================================================
    newawk () { # Returns the basename of the 'new' awk program on the
		# current host. nawk is only available on Solaris.
	        # Some distributions of Linux distribute mawk. However,
                # since this is not standard in all distributions, use awk.
		#
		# Always returns a 0 status
		#
		# usage: newawk
		#
        case "$ARCH" in
            sol2)
                echo "nawk"
                ;;
             *)
                echo "awk"
	esac
	return 0
    }
#=======================================================================
    check_Xdisplay () { # This is a simply minded check for now. See
			# the function with the same name in the webstart
			# shell script in the Web Server.
			#
			# Currently it prints a message and runs xhost.
			#
			# Always returns a 0 status
			#
			# usage: check_Xdisplay
			#
        if [ "$DISPLAY" != "" ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "-> Checking access to X display $DISPLAY . . . "
    echo "   If no response hit ^C and fix host or access control to host."
    echo "   Otherwise, checkout any error messages that follow and fix . . ."
	    xhost > /dev/null 2>&1
    echo "-> Successful . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	fi
	return 0
    }
#=======================================================================
   rm_trash_shfile () { # On many platforms there is a shell bug which
		        # leaves around a /tmp/sh$$* file for a here
			# document. We will attempt to remove the file
			# on any platform without checking for the ARCH.
			#
			# We have observed the following:
			#
			# hpux:
			#     /tmp/sh$$.1
			#
                        # Always returns a 0 status.
                        #
                        # usage: rm_trash_shfile
                        #
        rm -f /tmp/sh$$0 /tmp/sh$$.* > /dev/null 2>&1
	return 0
    }
#=======================================================================
#
#**************************************************************************
#
    RCSHEADER='$Revision: 1.24.4.7 $  $Date: 2004/12/22 19:52:41 $'
    REVISION=`expr "$RCSHEADER" : '[^ 	]* \([^ 	]*\)'`
#
# Parse the arguments
#
    stat="OK"
    help=0
    backend_help=0
    cache=0
    rmcache=0
    print_cache_only=0
    build_cache_only=0
    nocache=0
    verbose=0
    bin_echo=/bin/echo
    if [ "`echo '\n' | awk 'NF == 1 { print 1 }'`" = "1" ]; then
        use_bin_echo=1
    fi
    if [ "$use_bin_echo" = "1" ]; then
#
# -e is a required option on Redhat Linux only. Otherwise no option.
#
	echo_option=`$bin_echo -e | awk 'NF == 0 { print "-e"}'`
        if [ "`$bin_echo $echo_option '\n' | awk 'NF == 1 { print 1 }'`" = "1" ]; then
            bin_echo=printf
        fi

        arglist=`$bin_echo $echo_option '\n'`
    else
        arglist='\n'
    fi
    narg=$#
#
# No arguments is equivalent to help
#
    if [ "$narg" = "0" ]; then
	help=1
	stat=""
    fi
    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
	case "$1" in
	    -help)
		help=1
		stat=""
		;;
	    -\?)
		backend_help=1
		stat=""
		;;
            -cache)
                cache=1
                if [ "$narg" = "1" ]; then
                    print_cache_only=1
		fi
                ;;
	    -rmcache)
	        rmcache=1
		if [ "$narg" != "1" ]; then
		   msg="-rmcache must be used alone"
		   stat=""
	        fi
		;;
	    -nocache)
		nocache=1
		;;
            #-Y) 
            #   This is now handled by the * case.
            #   ;;
#========================= ARCH_LIST (start) ==============================
            -sol2)
                ARCH=sol2
                ;;
            -hpux)
                ARCH=hpux
                ;;
            -glnx86)
                ARCH=glnx86
                ;;
            -glnxi64)
                ARCH=glnxi64
                ;;
            -glnxa64)
                ARCH=glnxa64
                ;;
            -mac)
                ARCH=mac
                ;;
#========================= ARCH_LIST (end) ================================
	    -*v*)
		verbose=1
                if [ "$use_bin_echo" = "1" ]; then
                    arglist=`$bin_echo $echo_option "${arglist}\n$1"`
                else
                    arglist="${arglist}\n$1"
                fi
		;;
            -D*)
                debugger=`expr "$1" : '-D\(.*\)'`
                if [ "$debugger" = "" ]; then
                    if [ "$use_bin_echo" = "1" ]; then
                        arglist=`$bin_echo $echo_option "${arglist}\n$1"`
                    else
                        arglist="${arglist}\n$1"
		    fi
                else
                    MATLAB_DEBUG=`expr "//$debugger" : ".*/\(.*\)"`
                fi
		;;
	    *)
                if [ "$use_bin_echo" = "1" ]; then
                    arglist=`$bin_echo $echo_option "${arglist}\n$1"`
                else
                    arglist="${arglist}\n$1"
                fi
		;;
	esac
	shift
    done
#
    if [ "$stat" = "OK" -a  "$cache" = "1" -a "$nocache" = "1" ]; then
	msg="-cache and -nocache cannot be specified together"
	stat=""
    fi
#
# Output the contents of the cache file if that is all to do.
#
    if [ "$print_cache_only" = "1" ]; then
#-----------------------------------------------------------------------
    echo " "
#-----------------------------------------------------------------------
	print_cache `get_cache_file`
	if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo " "
#-----------------------------------------------------------------------
	fi
	exit 0
    fi
#
# Remove the cache file if that is all to do.
#
    if [ "$rmcache" = "1" ]; then
	remove_cache
	exit $?
    fi
#
    load_cache=0
    if [ "$stat" = "" -o \( "$cache" = "0" -a "$nocache" = "0" \) ]; then
	if [ "$ARCH" != "" ]; then
            scriptpath
            if [ $? -ne 0 ]; then
                exit 1
            fi
	else
            source_cache
            if [ $? -ne 0 ]; then
                scriptpath
                if [ $? -ne 0 ]; then
                    exit 1
                fi
            else
	        load_cache=1
	    fi
        fi
    fi
#
    if [ "$stat" = "" ]; then
	cat /dev/null > $tmpbase.d
        if [ "$msg" != "" ]; then
(
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> $tmpbase.d
        fi
	if [ "$backend_help" = "0" ]; then
(
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "help for mcc front end . . ."
    echo "-----------------------------------------------------------------"
    echo " "
    echo "    Usage: mcc [arguments] -help | [arguments] -? | -cache |"
    echo "                        -rmcache | [-arch] [-cache | -nocache]"
    echo "                        [-v] [-Y license_file] arguments ..."
    echo " "
    echo "    -help             - Help. It prints this help then the"
    echo "                        mcc backend help. The optional"
    echo "                        arguments control the backend help."
    echo " "
    echo "    -?                - Print the mcc backend help only. The"
    echo "                        optional arguments control the help."
    echo " "
    echo "    -cache            - Print the cache if the only argument."
    echo "                        With other arguments rebuild the"
    echo "                        cache."
    echo " "
    echo "    -arch             - Assume local host has architecture arch."
    echo "                        Rebuilds the cache file unless -nocache"
    echo "                        is used."
    echo " "
    echo "    -rmcache          - Remove the cache file."
    echo " "
    echo "    -nocache          - Ignore the cache file."
    echo " "
    echo "    -v                - Verbose mode. The mcc backend also"
    echo "                        supports this argument and it is passed"
    echo "                        through to the mcc backend."
    echo " "
    echo "    -Y license_file   - Override default license file with"
    echo "                        license_file. license_file can be a"
    echo "                        colon separated list of files or"
    echo "                        port@host entries. The mcc backend"
    echo "                        also supports this argument."
    echo " "          
    echo "    arguments ...     - These are arguments to the mcc"
    echo "                        backend."
    echo " "
    echo "    The Bourne Shell front end to the MATLAB Compiler. It"
    echo "    uses a cache file to speed up execution.  The cache file"
    echo "    contains precomputed values of critical environment"
    echo "    variables.  It is automatically built whenever the backend"
    echo "    is called provided that the cache file does not exist and"
    echo "    -nocache is not used. Later executions of 'mcc' will use"
    echo "    it unless overridden by the '-nocache' argument.  The"
    echo "    arguments ... are passed to the backend."
    echo " "
    echo "    The cache file is located in:"
    echo " "
    echo '        $HOME/.matlab/'"$REL_VERSION"'/mcc.cache/<revision>:<hostname>:<cksum>'
    echo " "
    echo '        <revision> - RCS revision of this script.'
    echo '        <hostname> - name of host'
    echo '        <cksum>    - echo dirpath($0) | cksum'
    echo " "
#-----------------------------------------------------------------------
) >> $tmpbase.d
	fi
#
# Output the options for the backend
#

	if [ "$help" = "1" -o "$backend_help" = "1" ]; then
	    if [ "$load_cache" = "0" ]; then
                set_mcc_env
        	if [ $? -ne 0 ]; then
		    rm -f $cache_body
	    	    exit 1
		fi
		rm -f $cache_body
            fi
#
# Parse any remaining arguments
#
            while [ $# -gt 0 ]; do
                case "$1" in
                    -help|-\?|-cache|-rmcache|-nocache)
                        :
                        ;;
#========================= ARCH_LIST (start) ==============================
                    -sol2)
                        :
                        ;;
                    -hpux)
                        :
                        ;;
                    -glnx86)
                        :
                        ;;
                    -glnxi64)
                        :
                        ;;
                    -glnxa64)
                        :
                        ;;
                    -mac)
                        :
                        ;;
#========================= ARCH_LIST (end) ================================
                    *)
                        if [ "$use_bin_echo" = "1" ]; then
                            arglist=`$bin_echo $echo_option "${arglist}\n$1"`
                        else
                            arglist="${arglist}\n$1"
                        fi
                        ;;
                esac
                shift
            done

            if [ "$backend_help" = "0" ]; then
(
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "help for mcc back end . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> $tmpbase.d
            fi
#
(
#-----------------------------------------------------------------------
	    eval $MATLAB/bin/$ARCH/mcc `build_arglist` -?
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> $tmpbase.d
	    more $tmpbase.d
	    rm -f $tmpbase.d
	else
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------" >> $tmpbase.d
#-----------------------------------------------------------------------
	    more $tmpbase.d
	    rm -f $tmpbase.d
	fi
	exit 1
    fi
#
    if [ "$load_cache" = "0" ]; then
#
# Create the variables for the cache
#
        scriptpath
        if [ $? -ne 0 ]; then
            exit 1
        fi
        set_mcc_env
        if [ $? -ne 0 ]; then
	    exit 1
        fi
#
# Build the cache if -nocache was not used.
#
	if [ "$nocache" = "0" ]; then
	    build_cache
	    if [ $? -ne 0 ]; then
	        exit 1
	    fi
	else
	    rm -f $cache_body
        fi
    fi
#
    set_os_env
#
# To guarantee that mbuild is available. However, note that
# is is the mbuild in the same directory as mcc.
#
    PATH=$MATLAB/bin:$PATH; export PATH
#
# Work around shell bug on alpha with here documents.
#
    rm_trash_shfile
#
    mcc_cmd="$MATLAB/bin/$ARCH/mcc"

    if [ "$debugger" != "" ]; then
        if [ "$MATLAB_DEBUG" = "gdb" -o "$MATLAB_DEBUG" = "xxgdb" ]; then
            SHELL=/bin/sh; export SHELL
        fi
        eval exec $debugger $mcc_cmd `build_arglist` 
    else
        eval exec $mcc_cmd `build_arglist` 
    fi
