#! /bin/sh
#
# Usage:        batch1.sh install_arguments
#		batch.boot install_arguments (boot time only)
#
# Abstract:     This Bourne Shell script performs a normal
#		(type 1) type of installation in batch.
#
# Options:
#		install_arguments - arguments simply passed on without
#				    parsing to install_matlab.
#
# Note(s):	1. Supply values for the right hand side for the
#		   following variables in this script. See the
#		   README in the MATLAB ./install directory for further
#		   explanation.
#
#		   WATCH OUT!
#
#		   If the MATLAB root directory is part of an AUTOMOUNTED
#		   directory supply a directory path for MATLAB that does
#		   not include any mount point information. It must be a
#		   path that forces a mount to be done if it is currently
#		   unmounted.
#
#		   MATLAB=
#		   OUTPUT_FILE=
#		   WANT_ACCESS_DIR=	 	yes OR no
#		   ACCESS_DIR=
#		   WANT_LICENSE_MANAGER=	yes OR no
#		   WANT_LMBOOT_LINK=		yes OR no 
#
#		   NO BLANKS around the "=" in the assignments!
#		   Put strings with blanks in single quotes.
#
#		2. If any of the following are 'yes' you must be
#		   superuser to complete the steps successfully.
#
#		   WANT_ACCESS_DIR
#		   WANT_LMBOOT_LINK
#
#               3. If this routine is sourced then the variable arglist
#                  contains any extra arguments to be passed to
#		   install_matlab. arglist should never be the empty
#		   string even when there are no arguments.
#
#
# Example:	1. Here is a sample dataset
#
#		   MATLAB=/usr/local/matlab  
#		   OUTPUT_FILE=$MATLAB/install_matlab.out
#		   WANT_ACCESS_DIR=yes
#		   ACCESS_DIR=/usr/local/bin
#		   WANT_LICENSE_MANAGER=yes
#		   WANT_LMBOOT_LINK=no
#
# Copyright (c) 1992-2000 by The MathWorks, Inc.
# $Revision: 1.28 $  $Date: 2000/07/27 19:42:11 $
#----------------------------------------------------------------------------
#
    arg0_=$0
#+++++++++++++++ substitute right hand values as required ++++++++++++++++++
#
    MATLAB=
    OUTPUT_FILE=
    WANT_ACCESS_DIR=
    ACCESS_DIR=
    WANT_LICENSE_MANAGER=
    WANT_LMBOOT_LINK=
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#    
    trap "rm -rf /tmp/$$a > /dev/null 2>&1; exit" 0 1 2 3 15
#
#=======================================================================
# Functions:
#   scriptpath ()
#=======================================================================
    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"
	        ROOTDIR=`/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 ''
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    return 1
        fi
        cd $cpath
        return 0
    }
#=======================================================================
#
# Determine path of this script
#
    scriptpath
    if [ $? -ne 0 ]; then
        exit 1
    fi
#
    BATCH_FILE=$ROOTDIR/$CMDNAME; export BATCH_FILE
#
# Set batch in the environment for the bootstrap case
#
    batch=1; export batch
#
    DOLLAR_MATLAB='$MATLAB'
    if [ "$arglist" = "" ]; then
        cat > /tmp/$$a << EOF
#
# MATLAB installation: normal
#
MATLAB=$MATLAB
$DOLLAR_MATLAB/install_matlab $* -b 1 > $OUTPUT_FILE 2>&1 << EOF
EOF
    else
        cat > /tmp/$$a << EOF
#
# MATLAB installation: normal
#
MATLAB=$MATLAB
$DOLLAR_MATLAB/install_matlab $arglist -b 1 > $OUTPUT_FILE 2>&1 << EOF
EOF
    fi
#
cat >> /tmp/$$a << EOF
$MATLAB
EOF
#
#
    if [ `expr "//$WANT_ACCESS_DIR" : '//[Nn].*'` -gt 0 ]; then
        cat >> /tmp/$$a << EOF
$WANT_ACCESS_DIR
EOF
    else
        cat >> /tmp/$$a << EOF
$WANT_ACCESS_DIR
$ACCESS_DIR
EOF
    fi
#
    cat >> /tmp/$$a << EOF
$WANT_LICENSE_MANAGER
EOF
    if [ `expr "//$WANT_LICENSE_MANAGER" : '//[Nn].*'` -gt 0 ]; then
	:
    else
        cat >> /tmp/$$a << EOF
$WANT_LMBOOT_LINK
EOF
    fi
    cat >> /tmp/$$a << EOF2
EOF
EOF2
#
    . /tmp/$$a
