#! /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=
#		   CHECK_SYMBOLIC_FOR_MAPLE= 	yes OR no
#		   MAPLE_LIBRARIES_DIR=
#			(contains files:  maple.hdb, maple.ind, maple.lib)
#		   LICENSE_NUMBER=
#		   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.
#
#		   Also if the -w option is passed (FTP only) only
#		   MATLAB and OUTPUT_FILE need to be assigned values.
#
#		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.
#
#		4. If -w option is passed (FTP only) you can assign values
#		   to two additional variables. 
#
#		   FTP_ARCH_LIST: 
#		   -------------
#
#		   This is a optional list of architectures to be used in
#		   determining the list of ftp files to download. The list
#		   must be composed of names from the first column in
#		   any order.
#
#		   	sun4	-  Sun Sparc : SunOS 4
#		      	sol2	-  Sun Sparc : SunOS 5
#			hp700	-  HP PA-RISC : HP-UX
#			alpha	-  DEC Alpha : Digital Unix
#			ibm_rs  -  IBM RS/6000 : AIX	
#			sgi	-  SGI R4000 : IRIX
#			sgi64   -  SGI R8000/R10000 : IRIX64
#			lnx86   -  PC : Linux
#
#		   The names must be blank separated and the list enclosed
#		   in quotes.
#
#		   For example, to generate a list for sun4 and sol2, use
#
#		       FTP_ARCH_LIST='sun4 sol2'		   
#
#		   If you don't specify a list the architecture of your
#		   local host is used.
#
#		   IMPORTANT: If you specify a list and you want your local
#			      host architecture included be user it is in
#			      the list.
#
#		   FTP_FILE_LIST:
#		   -------------
#
#		   Path to save the ftp file list to download. If you leave
#		   the variable without a value then it assumes:
#
#		       $MATLAB/files.ftp
#		   
#
# Example:	1. Here is a sample dataset
#
#		   MATLAB=/usr/local/matlab  
#		   OUTPUT_FILE=$MATLAB/install_matlab.out
#		   CHECK_SYMBOLIC_FOR_MAPLE=yes
#		   MAPLE_LIBRARIES_DIR=/usr/local/maple/lib
#		   LICENSE_NUMBER='LN 12372'
#		   WANT_ACCESS_DIR=yes
#		   ACCESS_DIR=/usr/local/bin
#		   WANT_LICENSE_MANAGER=yes
#		   WANT_LMBOOT_LINK=no
#
# Copyright (c) 1992-1998 by The MathWorks, Inc.
# $Revision: 1.25 $  $Date: 1998/08/25 12:20:09 $
#----------------------------------------------------------------------------
#
    arg0_=$0
#+++++++++++++++ substitute right hand values as required ++++++++++++++++++
#
    MATLAB=
    OUTPUT_FILE=
    CHECK_SYMBOLIC_FOR_MAPLE=
    MAPLE_LIBRARIES_DIR=
    LICENSE_NUMBER=''
    WANT_ACCESS_DIR=
    ACCESS_DIR=
    WANT_LICENSE_MANAGER=
    WANT_LMBOOT_LINK=
#
# (FTP only: -w)
#
    FTP_ARCH_LIST=
    FTP_FILE_LIST=
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#    
    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
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Determine if -w option in affect. Always pass at least one blank
# line.
#
#========================= 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 (c) 1997-1998 by The MathWorks, Inc.
# $Revision: 1.3 $  $Date: 1998/11/03 22:06:40 $
#----------------------------------------------------------------------------
#
    ARCH_LIST='sol2 hp700 alpha ibm_rs sgi sgi64 lnx86'
#========================= archlist.sh (end) ==============================
#
    ftp_select=0
    if [ "$arglist" = "" ]; then
        i=1
        while [ $i -le $# ]
        do
            value=`eval echo '$'"$i"`
            case "$value" in
                -w)
		     ftp_select=1
                     break
                     ;;
                *)
                     :
                     ;;
            esac
            i=`expr $i + 1`
        done
    else
	for value in $arglist
	do
            case "$value" in
                -w)
		     ftp_select=1
                     break
                     ;;
                *)
                     :
                     ;;
            esac
	done
    fi
    if [ "$ftp_select" != "0" ]; then 
#
# Translate the arch names into arch indices
#
	arch_index_list=""
	if [ "$FTP_ARCH_LIST" != "" ]; then
            i=1
	    for arch in $ARCH_LIST
            do
		found=0
	        for arch_select in $FTP_ARCH_LIST
		do
		    if [ "$arch" = "$arch_select" -a "$found" = "0" ]; then
			found=1
		        if [ "$arch_index_list" = "" ]; then
			    arch_index_list="$i"
			else
			    arch_index_list="$arch_index_list $i"
			fi
		    fi
	        done
                i=`expr $i + 1`
	    done
	fi
	if [ "$arch_index_list" != "" ]; then
            cat >> /tmp/$$a << EOF
$arch_index_list
EOF
        fi
        cat >> /tmp/$$a << EOF

$FTP_FILE_LIST
EOF
    else
        cat >> /tmp/$$a << EOF
$MATLAB
EOF
    fi
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    if [ `expr "//$CHECK_SYMBOLIC_FOR_MAPLE" : '//[Nn].*'` -gt 0 ]; then
        cat >> /tmp/$$a << EOF
$CHECK_SYMBOLIC_FOR_MAPLE
EOF
    else
        cat >> /tmp/$$a << EOF
$CHECK_SYMBOLIC_FOR_MAPLE
$MAPLE_LIBRARIES_DIR
EOF
    fi
#
    cat >> /tmp/$$a << EOF
$LICENSE_NUMBER
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
