#!/bin/sh
#
#  Name:
#
#      mcc    script file for invoking MCC
#
#  Usage:
#
#      mcc [arguments] -help | [arguments] -? | -cache |
#          -rmcache | [-arch] [-cache | -nocache] 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/R12/mcc.cache/<revision>:<hostname:<cksum>
#
#	   <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.
#
#      arguments ...     - These are arguments to the mcc backend.
#                          Use -help or -? for the syntax. 
#			   Note: The '-Y license_file' arguments are
#			         removed from this list before being
#			         passed to the back end.
#
#  Copyright 1998-2000 The MathWorks, Inc.
#  $Revision: 1.20 $  $Date: 2000/05/19 13:44:03 $
#__________________________________________________________________________
#
    arg0_=$0
#
    trap "rm -f /tmp/$$a /tmp/$$b /tmp/$$c /tmp/$$d; rm_trash_shfile; exit 1" 1 2 3 15
#
# For oscheck.sh
#
    temp_file=/tmp/$$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-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:
#   scriptpath ()
#   source_cache ()
#   get_cache_file ()
#   gethostname ()
#   dirpath_arg0 ()
#   remove_cache ()
#   get_arch ()
#   set_mcc_env ()
#   set_matlab_env () 
#   print_cache ()
#   fix_license ()
#   fix_ldpath ()
#   check_env ()
#   check_matlab_bin ()
#   check_mcc
#   set_os_env ()
#   build_cache ()
#   build_arglist ()
#
#   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.
                      #
                      # 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
	        . $cache_file
		if [ "$verbose" = "1" ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo "Loaded cache file: $cache_file"
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
		fi
	        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`
#
# Take 1st field
# Bug: sgi(6.3) - first token of sum is treated as a number and
#                 not a string. So it can be negative.
#
        sum=`expr "$sum" : '\([^ 	]*\)'`
	echo "$HOME/.matlab/R12/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 ()  { # Determine values for the following environment
		      # variables:
		      #
                      # 1. ARCH 
		      # 2. MATLAB
		      # 3. AUTOMOUNT_MAP
		      # 4. LM_LICENSE_FILE
                      # 5. LDPATH_PREFIX
		      # 6. LDPATH_SUFFIX 
		      # 7. <load_library_path>   (platform dependent)
		      #
		      # Start with an initial value of variables normally
		      # obtained by sourcing .matlab6rc.sh. Then fix
		      # those values.
		      #
                      # 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
#
#  Initial values: Try to source .matlab6rc.sh. The search order is:
#
#       .               (current directory)
#       $HOME           (users home directory)
#       $MATLAB/bin     (MATLAB bin directory)
#
        if [ -f .matlab6rc.sh ]; then
	    set_matlab_env $cpath/.matlab6rc.sh
        elif [ -f $HOME/.matlab6rc.sh ]; then
            set_matlab_env $HOME/.matlab6rc.sh
        elif [ -f $MATLAB/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.
#
            set_matlab_env $MATLAB/bin/.matlab6rc.sh
        else
#
# Will use AUTOMOUNT_MAP if it exists in the environment. Fix MATLAB
# appropriately.
#
            AUTOMOUNT_MAP="$AUTOMOUNT_MAP"; export AUTOMOUNT_MAP
	    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
	    export MATLAB
            LM_LICENSE_FILE='$MATLAB/etc/license.dat'; export LM_LICENSE_FILE
#
# LDPATH_PREFIX, LDPATH_SUFFIX, and <load_library_path>
# from the environment.
#
        fi
#
# Now fix any variables
#
	fix_license
	if [ $? -ne 0 ]; then
	    return 1
	fi
#
	fix_ldpath
#
# Check the environment
#
	check_env
	return $?
    }
#=======================================================================
    set_matlab_env () { # Sources matlab_rcfile to determine
		        #
                        # 1. ARCH 
		        # 2. MATLAB
		        # 3. AUTOMOUNT_MAP
		        # 4. LM_LICENSE_FILE
                        # 5. LDPATH_PREFIX
			# 6. LDPATH_SUFFIX 
		        # 7. <load_library_path>   (platform dependent)
			#
                        # It writes them to a temporary file which
                        # is then sourced.
                        #
                        # Always returns a 0 status.
                        #
                        # usage: set_matlab_env matlab_rcfile
                        #
	matlab_rcfile=$1
#
cat << 'EOF' > /tmp/$$a
        (MATLABdefault=$MATLAB
         MATLAB_UTIL_DIRdefault=$MATLAB/bin/util
         . $matlab_rcfile
         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
         echo "ARCH='$ARCH'; export ARCH"
         echo "MATLAB='$MATLAB'; export MATLAB"
	 echo "AUTOMOUNT_MAP='$AUTOMOUNT_MAP'; export AUTOMOUNT_MAP"
         echo "LM_LICENSE_FILE='$LM_LICENSE_FILE'; export LM_LICENSE_FILE"
#
	 echo "LDPATH_PREFIX='$LDPATH_PREFIX'; export LDPATH_PREFIX"
	 echo "LDPATH_SUFFIX='$LDPATH_SUFFIX'; export LDPATH_SUFFIX"
         case "$ARCH" in
             sun4|sol2|alpha|sgi|lnx86)
                 echo "LD_LIBRARY_PATH='$LD_LIBRARY_PATH'; export LD_LIBRARY_PATH"
                 ;;
             hp700|hpux)
                 echo "SHLIB_PATH='$SHLIB_PATH'; export SHLIB_PATH"
                 ;;
             ibm_rs)
                 echo "LIBPATH='$LIBPATH'; export LIBPATH"
                 ;;
             sgi64)
                 echo "LD_LIBRARY64_PATH='$LD_LIBRARY64_PATH'; export LD_LIBRARY64_PATH"
                 ;;
             *)
                 echo "LD_LIBRARY_PATH='$LD_LIBRARY_PATH'; export LD_LIBRARY_PATH"
                 ;;
         esac) > /tmp/$$b
EOF
#
        . /tmp/$$a > /dev/null 2>&1
        . /tmp/$$b > /dev/null 2>&1
#
	rm -f /tmp/$$a /tmp/$$b
        return 0
    }
#=======================================================================
    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
    }
#=======================================================================
    fix_license () { # Fixes 
		     # passed as an argument.
		     #
                     # Returns a 0 status unless no cache file exists
		     #
                     # usage: fix_license
                     #
        if [ "$licensefile" != "" ]; then
            LM_LICENSE_FILE="$licensefile"
	else
            LM_LICENSE_FILE="`eval echo $LM_LICENSE_FILE`"
#
# Add any 'DEMO' licenses in $MATLAB/etc
#
	    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
#
	export LM_LICENSE_FILE
#
# 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 ':' ' '`
        cont=0
        nbadfiles=0
        nfiles=0
        for licensefile in $licensefilelist
        do
            if [ `expr "$licensefile" : '.*@.*'` -ne 0 ]; then
	        cont=1
	        break
	    elif [ ! -f $licensefile ]; then
	        nbadfiles=`expr $nbadfiles + 1`
	    fi
	    nfiles=`expr $nfiles + 1`
        done
        if [ "$cont" = "0" -a "$nfiles" != "$nbadfiles" ]; then
	    cont=1
        fi
        if [ "$cont" = "0" ]; then 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    if [ $nfiles -eq 1 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    mcc: license file in LM_LICENSE_FILE path does not exist'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    mcc: license files in LM_LICENSE_FILE path do not exist'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '         $LM_LICENSE_FILE'" = $LM_LICENSE_FILE"
    echo ''       
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            for licensefile in $licensefilelist
            do
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "         file = $licensefile"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            done
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            return 1
        fi
#
	return 0
    }
#=======================================================================
    fix_ldpath () { # Output the contents of the cache file which is
		    # passed as an argument.
		    #
                    # Returns a 0 status unless no cache file exists
		    #
                    # usage: fix_ldpath
                    #
#
# Determine <final_load_library_path> for each platform
#
        case "$ARCH" in
	    sol2|alpha|lnx86)
	        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
	        else
		    LD_LIBRARY_PATH=$LDPATH_MATLAB
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
		    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
	        ;;
	    hp700|hpux)
	        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
	        else
		    SHLIB_PATH=$LDPATH_MATLAB
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        SHLIB_PATH=$LDPATH_PREFIX:$SHLIB_PATH
		    fi
	        fi
	        if [ "$LDPATH_SUFFIX" != "" ]; then
	            LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	            if [ "$LDPATH_SUFFIX" != "" ]; then
                        SHLIB_PATH=$SHLIB_PATH:$LDPATH_SUFFIX
		    fi
	        fi
	        export SHLIB_PATH
	        ;;
	    ibm_rs)
	        LIBPATH="`eval echo $LIBPATH`"
	        LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH
	        LDPATH_SYSTEM=/usr/lib:/lib
	        if [ "$LIBPATH" != "" ]; then
		    LIBPATH=$LDPATH_MATLAB:$LIBPATH:$LDPATH_SYSTEM
	        else
		    LIBPATH=$LDPATH_MATLAB:$LDPATH_SYSTEM
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        LIBPATH=$LDPATH_PREFIX:$LIBPATH
		    fi
	        fi
	        if [ "$LDPATH_SUFFIX" != "" ]; then
	            LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	            if [ "$LDPATH_SUFFIX" != "" ]; then
                        LIBPATH=$LIBPATH:$LDPATH_SUFFIX
		    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
	        if [ "$LD_LIBRARY_PATH" != "" ]; then
		    LD_LIBRARY_PATH=$LDPATH_MATLAB:$LD_LIBRARY_PATH
	        else
		    LD_LIBRARY_PATH=$LDPATH_MATLAB
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
		    fi
	        fi
	        if [ "$LDPATH_SUFFIX" != "" ]; then
	            LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	            if [ "$LDPATH_SUFFIX" != "" ]; then
                        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LDPATH_SUFFIX
		    fi
	        fi
	        export LD_LIBRARY_PATH
	        ;;
	    sgi64)
	        LD_LIBRARY64_PATH="`eval echo $LD_LIBRARY64_PATH`"
	        LDPATH_MATLAB=$MATLAB/sys/os/$ARCH:$MATLAB/bin/$ARCH:$MATLAB/extern/lib/$ARCH
	        if [ "$LD_LIBRARY64_PATH" != "" ]; then
		    LD_LIBRARY64_PATH=$LDPATH_MATLAB:$LD_LIBRARY64_PATH
	        else
		    LD_LIBRARY64_PATH=$LDPATH_MATLAB
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        LD_LIBRARY64_PATH=$LDPATH_PREFIX:$LD_LIBRARY64_PATH
		    fi
	        fi
	        if [ "$LDPATH_SUFFIX" != "" ]; then
	            LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	            if [ "$LDPATH_SUFFIX" != "" ]; then
                        LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH:$LDPATH_SUFFIX
		    fi
	        fi
	        export LD_LIBRARY64_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
	        else
		    LD_LIBRARY_PATH=$LDPATH_MATLAB
	        fi
	        if [ "$LDPATH_PREFIX" != "" ]; then
	            LDPATH_PREFIX="`eval echo $LDPATH_PREFIX`"
	            if [ "$LDPATH_PREFIX" != "" ]; then
                        LD_LIBRARY_PATH=$LDPATH_PREFIX:$LD_LIBRARY_PATH
		    fi
	        fi
	        if [ "$LDPATH_SUFFIX" != "" ]; then
	            LDPATH_SUFFIX="`eval echo $LDPATH_SUFFIX`"
	            if [ "$LDPATH_SUFFIX" != "" ]; then
                        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LDPATH_SUFFIX
		    fi
	        fi
	        export LD_LIBRARY_PATH
	        ;;
        esac
	return 0
    }
#=======================================================================
    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
#
# 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
#
	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`
#
# Take 1st field
# Bug: sgi(6.3) - first token of sum is treated as a number and
#                 not a string. So it can be negative.
#
        sum=`expr "$sum" : '\([^ 	]*\)'`
#
	if [ ! -d $HOME/.matlab/R12 ]; then
	    mkdir -p $HOME/.matlab/R12 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo 'Warning: Could not create $HOME/.matlab/R12 directory . . .'
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    fi
	fi
	if [ ! -d $HOME/.matlab/R12/mcc.cache ]; then
	    mkdir $HOME/.matlab/R12/mcc.cache 2>/dev/null
	    if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo 'Warning: Could not create $HOME/.matlab/R12/mcc.cache directory . . .'
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	        return 1
	    fi
	fi
#
	dir=$HOME/.matlab/R12/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/R12/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/R12/mcc.cache/'$file
    echo "         Cache file not built . . ."
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
	    return 1
	fi
#
	(echo "#________________________________________________________________"
	 echo "#"
	 echo "#"
	 echo "# NAME: $dir/$file"
	 echo "#________________________________________________________________"
	 echo "#"
         echo "ARCH=$ARCH; export ARCH"
	 echo "#"
         echo "MATLAB=$MATLAB; export MATLAB"
	 echo "#"
         echo "LM_LICENSE_FILE=$LM_LICENSE_FILE; export LM_LICENSE_FILE"
	 echo "#"
#
         case "$ARCH" in
             sun4|sol2|alpha|sgi|lnx86)
                 echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export LD_LIBRARY_PATH"
                 ;;
             hp700|hpux)
                 echo "SHLIB_PATH=$SHLIB_PATH; export SHLIB_PATH"
                 ;;
             ibm_rs)
                 echo "LIBPATH=$LIBPATH; export LIBPATH"
                 ;;
             sgi64)
                 echo "LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH; export LD_LIBRARY64_PATH"
                 ;;
             *)
                 echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export LD_LIBRARY_PATH"
                 ;;
         esac
	 echo "#________________________________________________________________"
	 ) > $dir/$file
	 if [ "$verbose" = "1" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "Built cache file: $dir/$file"
    echo "-----------------------------------------------------------------"
    echo " "
#-----------------------------------------------------------------------
	 fi
	 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
                       #
                       # 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
    }
#=======================================================================
   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:
			#
			# alpha:
			#     /tmp/sh$$0  - Bourne Shell, BIN_SH undefined
			#     /tmp/sh$$.1 - POSIX Shell,  BIN_SH=xpg4
			# hp700,hpux:
			#     /tmp/sh$$.1
			# ibm_rs:
			#     /tmp/sh$$.n - n = 1, 2, . . . 
			# sgi64:
			#     /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.20 $  $Date: 2000/05/19 13:44:03 $'
    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
    if [ "`echo '\n' | awk 'NF == 1 { print 1 }'`" = "1" ]; then
        use_bin_echo=1
    fi
    if [ "$use_bin_echo" = "1" ]; then
        arglist=`/bin/echo '\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)
		if [ $# -eq 1 ]; then
		   msg="No license file provided with -Y option, type mcc -? for help"
		   stat=""
		else
		   shift
		   licensefile=$1
                   nocache=1
		fi
		;;
#========================= ARCH_LIST (start) ==============================
            -sol2)
                ARCH=sol2
                ;;
            -hpux)
                ARCH=hpux
                ;;
            -hp700)
                ARCH=hp700
                ;;
            -alpha)
                ARCH=alpha
                ;;
            -ibm_rs)
                ARCH=ibm_rs
                ;;
            -sgi)
                ARCH=sgi
                ;;
            -glnx86)
                ARCH=glnx86
                ;;
#========================= ARCH_LIST (end) ================================
	    -*v*)
		verbose=1
                if [ "$use_bin_echo" = "1" ]; then
                    arglist=`/bin/echo "${arglist}\n$1"`
                else
                    arglist="${arglist}\n$1"
                fi
		;;
	    *)
                if [ "$use_bin_echo" = "1" ]; then
                    arglist=`/bin/echo "${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 > /tmp/$$d
        if [ "$msg" != "" ]; then
(
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> /tmp/$$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 "                        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 "    arguments ...     - These are arguments to the mcc"
    echo "                        backend."
    echo "                        Note: The '-Y license_file'"
    echo "                              arguments are removed from"
    echo "                              this list before being passed"
    echo "                              to the backend. Also -v is"
    echo "                              checked for."
    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/R12/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 " "
#-----------------------------------------------------------------------
) >> /tmp/$$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
	    	    exit 1
		fi
            fi
#
# Parse any remaining arguments
#
            while [ $# -gt 0 ]; do
                case "$1" in
                    -help|-\?|-cache|-rmcache|-nocache)
                        :
                        ;;
#========================= ARCH_LIST (start) ==============================
                    -sol2)
                        :
                        ;;
                    -hpux)
                        :
                        ;;
                    -hp700)
                        :
                        ;;
                    -alpha)
                        :
                        ;;
                    -ibm_rs)
                        :
                        ;;
                    -sgi)
                        :
                        ;;
                    -glnx86)
                        :
                        ;;
#========================= ARCH_LIST (end) ================================
                    *)
                        if [ "$use_bin_echo" = "1" ]; then
                            arglist=`/bin/echo "${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 "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> /tmp/$$d
            fi
#
(
#-----------------------------------------------------------------------
	    eval $MATLAB/bin/$ARCH/mcc `build_arglist` -?
    echo "-----------------------------------------------------------------"
#-----------------------------------------------------------------------
) >> /tmp/$$d
	    more /tmp/$$d
	    rm -f /tmp/$$d
	else
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------" >> /tmp/$$d
#-----------------------------------------------------------------------
	    more /tmp/$$d
	    rm -f /tmp/$$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
        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
#
    eval exec $MATLAB/bin/$ARCH/mcc `build_arglist` 
