#! /bin/sh
#
#  Name:
#
#     install  script to install the FLEXlm license manager
#	       into a standalone area.
#     
#  Usage:
#
#     install | install_lm [-h|-help] | [lmroot]
#
#  Description:
#
#     This Bourne Shell script installs the FLEXlm license
#     manager into a standalone area.
#
#     It can be located in the following places:
#
#     UNIX CD-ROM:  $CDROM/etc/install
#     UNIX FTP:     $ROOTDIR/install_lm
#     PC CD-ROM:    $CDROM/unix/etc/install
#
#     After installation it is located in:
#
#		    $LM_ROOT/install_lm
#
#     CD-ROM: [UNIX or PC]
#
#	  1. Query for LM_ROOT directory and
#	     be sure that it passes the standalone criteria.
#	  2. Query for the list of arches to install.
#	  3. Install pd files from CD-ROM for all selected arches.
#	  4. Move all files and directories from etc/
#	     up one level.
#	  5. Install license manager files.
#
#     Off CD-ROM:
#
#	  1. Query for LM_ROOT directory and be sure that
#	     it passes the standalone criteria. Initial LM_ROOT
#	     directory is the directory where this script is
#	     located.
#	  2. Install pd files from local update/pd.
#	  3. Move all files and directories from etc/
#	     up one level.
#	  4. Install license manager files.
#
#  Options:
#
#     -h | -help 	- help. Print usage.
#
#     lmroot	        - path of the license manager root directory
#
#  Note:
#
#     1. No call to oscheck is done.
#
#  Copyright (c) 1998-1999 by The MathWorks, Inc.
#  $Revision: 1.7 $  $Date: 1999/01/08 21:29:03 $
#-----------------------------------------------------------------------
#23456789012345678901234567890123456789012345678901234567890123456789012
#
    arg0_=$0
#
    temp_file=/tmp/$$a
    temp_file2=/tmp/$$b
    temp_file3=/tmp/$$c
    temp_file4=/tmp/$$d
    LSCREEN=/tmp/$$lscreen
#
    trap "abort; exit 1" 1 2 3 15
#
#=======================================================================
#
# Utility functions:
#
#   abort ()
#   interactive ()
#   scriptpath ()
#   ----				<- part from util.sh
#   actualpath ()		
#   center_line ()
#   clean_temp_areas ()			[changed]
#   clear_screen ()
#   echon ()
#   get_archlist ()			<- imbed archlist.sh
#   get_oldname ()
#   get_version ()			<- expandm R11
#   is_superuser ()
#   merge_license ()
#   query_to_continue ()		[changed]
#   search_path ()
#   stdpath ()
#   ----
#
# Functions:
#
#   ----				<- from license.sh
#   accept_license ()			<- imbed copyright.ins
#       answerquery ()
#       expand_tabs ()
#       intr_char ()
#       license_screens ()
#       lpr_license ()
#       printlicense ()
#       printquery ()
#       welcome ()			[changed]
#       window_rows ()
#   ----
#   cd_expand_pd_files ()
#   cd_offload_doc ()
#   cd_offload_install ()
#   check_for_standalone ()
#   expand_pd_files ()
#   fin ()
#   get_arch ()				<- imbed arch.sh
#   get_install_arches ()
#   get_lmroot ()
#   ----				<- from lm.sh
#   install_license_manager ()
#       build_lm_links ()
#       check_to_install_lm ()
#       create_license_file ()		[changed]
#       install_lm_scripts ()           [changed]
#       lm_txt ()			[changed]
#       merge_lsfile  ()
#       start_stop_mesg ()
#   ----
#   move_files ()
#   prepare_area ()
#   to9660 ()
#
#=======================================================================
    abort () { # Cleans up, prints an abort message, and exits.
               #
               # Always exits with a 1 status.
               #
               # usage: abort
               #
	clean_temp_areas
#-----------------------------------------------------------------------
    echo ''
    echo '    Sorry! License manager install for MATLAB aborted . . .'
    echo ''
#-----------------------------------------------------------------------
	exit 1
    }
#=======================================================================
    interactive () { # Determines whether you are running interactive
                     # or not.
                     #
                     # Returns 0 status if interactive else 1.
                     #
                     # usage: interactive
                     #
        if [ "$batch" = "" ]; then
            (tty) > /dev/null 2>&1
            return $?
        else
            return 0
        fi
    }
#=======================================================================
    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
    }
#=======================================================================
# From: util.sh
#=======================================================================
    actualpath () { # Writes the actual file path of the argument to
		    # standard output.  Only a real existing file returns
		    # a non-null value.
		    #
		    # Always returns a 0 status.
		    #
		    # usage: actualpath file
		    #
	File=$1
	Filepath=
	lsCmd=`ls -ld $File 2>/dev/null`
        if [ "$lsCmd" ]; then
#
# Check for link portably
#
	    if [ `expr "$lsCmd" : '.*->.*'` -eq 0 ]; then
    	        if [ ! -d $File ]; then
	            Filepath=$File
                fi
            else
#
# A directory link?
#
                (cd $File) > /dev/null 2>&1
                if [ $? -eq 0 ]; then
	            :
                else
#
# Now it is either a file, link to a file, or still a bad path.
#
	            cpath=`/bin/pwd`
                    localFile=$File
#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
	            n=1
    	            while [ $n -le 8 ]
    	            do
#
# Get directory correctly!
#
   	                newDir=`echo "$localFile" | awk '
#-----------------------------------------------------------------------
		{ tail = $0
                  np = index (tail, "/")
                  while ( np != 0 ) {
                      tail = substr (tail, np + 1, length (tail) - np)
                      if (tail == "" ) break
                          np = index (tail, "/")
                  }
                  head = substr ($0, 1, length ($0) - length (tail))
                  if ( tail == "." || tail == "..")
                      print $0
                  else
                      print head
                }'`
#-----------------------------------------------------------------------
	                if [ ! "$newDir" ]; then
	                    newDir="."
	                fi
	                (cd $newDir) > /dev/null 2>&1
	                if [ $? -ne 0 ]; then
		            break
	                fi
	                cd $newDir
	                newDir=`/bin/pwd`
	                newBase=`expr //$localFile : '.*/\(.*\)' \| $localFile`
                        lsCmd=`ls -l $newBase 2>/dev/null`
	                if [ ! "$lsCmd" ]; then
		            break
	                fi
#
# Check for link portably
#
	                if [ `expr "$lsCmd" : '.*->.*'` -ne 0 ]; then
	                    localFile=`echo "$lsCmd" | awk '{ print $NF }'`
	                else
#
# It's a file
#
	                    Filepath=$newDir/$newBase
	                fi
		        n=`expr $n + 1`
    	            done
    	            cd $cpath
	        fi
	    fi
        fi
	echo $Filepath
	return 0
    }
#=======================================================================
    center_line () { # Takes the input string argument and centers it
		     # in 80 characters to standard output.
		     #
		     # Always returns a zero status.
                     #
                     # usage: center_line string
                     #
	echo "$1" | awk '
#-----------------------------------------------------------------------
# blankhead is 40 characters
#
    BEGIN { blankhead = "                                        " }
	  { lend2 = 40 - (length($0) / 2 - length($0) % 2)
	    print substr(blankhead,1,lend2) $0 }'
#-----------------------------------------------------------------------
#
	return 0
    }
#=======================================================================
    clean_temp_areas () { # Removed any temporary files and directories.
		          #
                          # Always returns a 0 status.
                          #
                          # usage: clean_temp_areas
                          #
        rm -rf $temp_file $temp_file2 $temp_file3 $temp_file4 $LSCREEN
	return 0
    }
#=======================================================================
    clear_screen () { # Clear the screen if no arg and not in batch.
                      # Always output one line in case clear doesn't
                      # work.
                      #
                      # Always returns a 0 status.
                      #
                      # usage: clear_screen arg
                      #
#-----------------------------------------------------------------------
        echo ''
#-----------------------------------------------------------------------
        if [ $# -eq 0 ]; then
            if [ "$batch" != "1" ]; then
                clear
            fi
        fi
        return 0
    }
#=======================================================================
    echon () { # Echo's out a message without the newline so that the
               # user can be prompted.
               # 
               # Always returns a 0 status.
               # 
               # usage: echon mesg
               # 
        if [ "`echo -n`" != "" ]; then
            echo "$1\c"
        else
            echo -n "$1"
        fi
        return 0
    }
#=======================================================================
    get_archlist () { # Outputs the value of the archlist variable,
		      # ARCH_LIST.
                      #
                      # Always returns a 0 status.
                      #
                      # usage: get_archlist
                      #
#========================= 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) ==============================
	echo $ARCH_LIST
        return 0
    }
#=======================================================================
    get_oldname () { # This Bourne Shell script given oldname returns the
                     # unique name in old/ so that oldname can be placed
		     # there without overwriting any other file. The
		     # original oldname and old/ are in the directory
		     # specified by dir_oldname.  The format of a file
                     # in old/ is:
		     #         oldname.date.#
                     # date is taken from the file. If old/ does not
		     # exist it is created. We assume the file exists.
                     #
                     # note(s): 1. The variable 'oldname' holds the
		     #             filename.
                     #		3. The variable 'dir_oldname' holds the
		     #             directory where oldname and old/
		     #             exist.
                     #		4. date = ddmmmyy (mmm in lowercase)
		     #
                     # Always returns a 0 status.
                     # 
                     # usage: get_oldname dir_oldname file_oldname
                     # 
	dir_oldname=$1
	file_oldname=$2
#
        if [ ! -d $dir_oldname/old ]; then
	    mkdir $dir_oldname/old
	    chmod 755 $dir_oldname/old
        fi
#
        daymonth=`(cd $dir_oldname; ls -l $file_oldname | awk '{ print $(NF-2) $(NF-3) }' | tr '[A-Z]' '[a-z]')`
	year=`date | awk '{ print $NF }'`
	date=$daymonth$year
#
	nfiles=`(cd $dir_oldname/old; ls $file_oldname.$date.* 2>/dev/null) | awk '
#----------------------------------------------------------------------------
    BEGIN { maxn = 0; digits = "0123456789" }
          { n = split($0,a,".")
	    if (n >= 3) {
	        m = a[n]
		for (i = 1; i <= length(m); i = i + 1)
		    if (index(digits,substr(m,i,1)) == 0) 
			break
	        if (i == length(m) + 1) 
		    if (m > maxn)
			 maxn = m 
	    }
	  }
    END { print maxn }'`
#----------------------------------------------------------------------------
        nfiles=`expr $nfiles + 1`
	echo $file_oldname.$date.$nfiles
#
	return 0
    }
#=======================================================================
    get_version () { # Outputs the version.
		     #
                     # Always returns a 0 status.
                     #
                     # usage: get_version
                     #
	echo 'R11'
        return 0
    }
#=======================================================================
    is_superuser () { # Determines whether the current user is superuser
		      # or not. We assume uid=0 is always superuser.
		      # If id does not exist we try to create a file
		      # called $$check in / and delete it.
		      # 
                      # Returns a 0 status if superuser else 1.
                      #
                      # usage: is_superuser
                      #
        pathlist=`search_path id`
        if [ "$pathlist" != "" ]; then
            user=`id`
            if [  `expr "$user" : 'uid=0('` -ne 6 ]; then
		return 1
	    else
		return 0
	    fi
	else
#
# Command 'id' is not on the path. Try the file check method.
#
	    (cat /dev/null > /$$check) > /dev/null 2>&1
	    if [ $? -eq 0 ]; then
		rm -f /$$check
		return 0
	    else
		return 1
	    fi
	fi
    }
#=======================================================================
    merge_license () { # Merge license file into template file and
		       # write result to standard output. There is
		       # 'EOF' that separates the two files.
		       #
		       # Always returns a 0 status.
		       #
		       # usage: merge_license template_file license_file
		       #
#
# If license file already has SERVER lines, then assume that they are
# correct and use those. Always use the DAEMON line from the template
# file. The two exceptions that must be changed
#
#	server port number comes from the template file
#	daemon options file comes from the license file
#
# They should merge these into the template file.
#
	cat $1 $2 | awk '
#----------------------------------------------------------------------------
    BEGIN { state = 0; nc = 0; nf = 0; server_found = 0; nserver = 0 }
    $1 == "EOF" { state = 1; next }
	{ if (state == 0 ) { 
	      if ($1 == "SERVER") {
		  server = $0
		  if (NF == 4)
		      port = $4
		  else
		      port = ""
	      }
	      if ($1 == "DAEMON")
		  daemon = $0
	  }
	  else if ($1 == "SERVER") {
	      server_found = 1	
	      nserver = nserver + 1
# fix the port in each server line
	      if ( port != "") {
	          serverv[nserver] = $1 " " $2 " " $3 " " port
	      }
	      else
	          serverv[nserver] = $0
	  }
	  else if ($1 == "DAEMON") {
# add the options file to the the daemon line if it exists
	      if (NF == 4)
		  daemon = daemon " " $4
	  }
	  else if (substr($1,1,1) == "#" && (index($2,"BEGIN") != 1 && index($2,"END") != 1)) {
	      nc = nc + 1
	      comment[nc] = $0
	  }
	  else if (($1 == "FEATURE") || ($1 == "INCREMENT")) {
	      nf = nf + 1
	      feature[nf] = $0
	  }
	  next
	}
    END { if (nc > 0) for (i = 1; i <= nc; i = i + 1) print comment[i]
	  if (server_found != 0)
              for (i = 1; i <= nserver; i = i + 1)
		  print serverv[i]
          else 
	      print server
	  print daemon
          if (nf > 0) for (i = 1; i <= nf; i = i + 1) print feature[i]
	}'
#----------------------------------------------------------------------------
	return 0
    }
#=======================================================================
    query_to_continue () { # Query the user to continue if not in batch
			   # mode or no arguments.
			   #
		           # Always returns a 0 status.
		           #
			   # usage: query_to_continue arg
		           #
        if [ "$batch" = "0" -o $# -eq 0 ]; then
	    echon 'Continue? ([y]/n) '
            read ans
            if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
		clean_temp_areas
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "    $CMDNAME stopped prematurely . . ."
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		exit 1
            fi
        fi
	return 0
    }
#=======================================================================
    search_path () { # Searches all the directories in your PATH for
		     # the command argument and outputs those to
		     # standard out as a list each separated by a blank.
		     #
		     # Always returns a 0 status.
		     #
		     # usage: search_path command
		     #
	cmd=$1
	vpath=`echo $PATH | tr ":" " "`
#
	pathlist=""
	for dirname in $vpath
	do
	   if [ -d $dirname/$cmd ]; then
	      pathlist="$pathlist $dirname/$cmd"
	   else
	      lscmd=`ls -l $dirname/$cmd 2>/dev/null`
	      if [ "$lscmd" ]; then
	         pathlist="$pathlist $dirname/$cmd"
	      fi
	    fi
	done
	echo $pathlist
#
	return 0
    }
#=======================================================================
    stdpath () { # Remove any ., .., and // from the path argument. If
		 # it is not an absolute path create one using /bin/pwd.
		 #
		 # It is assumed that the path exists.
		 #
                 # Always returns a 0 status.
                 #
                 # usage: stdpath path
                 #
	if [ `expr "//$1" : "///.*"` -ne 0 ]; then
             echo $1 | awk '
#------------------------------------------------------------------------------
                { n=split($1,a,"/") 
                  k=0
                  for (i = 1; i <= n; i = i + 1) {
                      if (a[i] == "" || a[i] == ".") continue
                      if (a[i] == "..") {
                         if (k != 0) k = k - 1
                         continue   
                      }
                      k = k + 1  
                      b[k] = a[i]
                  }
                  p = "" 
                  if (k == 0)
                      p = p "/"
                  else
                      for (i = 1; i <= k; i = i + 1)
                          p = p "/" b[i]
                  print p }'
#------------------------------------------------------------------------------
	else
	    (cd $1; /bin/pwd)
	fi
	return 0
    }
#=======================================================================
# From: license.sh
#=======================================================================
    accept_license () { # Outputs the license agreement and queries the
			# user for acceptance or rejection
		 	#
		 	# This routine automatically accepts the license
		 	# agreement in batch mode. It is outputted first
		 	#
		 	# It divides up the agreement into pages and
			# allows the user to scroll through them forward
			# or backward
		 	#
		 	# The user can accept or reject the license at
			# any point
		 	#
		 	# The first page has the Copyright notice before
			# the beginning of the license agreement
		 	#
		 	# A rows output of stty -a is use to determine
			# the page size
		 	#
		 	# A page is of the form:
		 	#
		 	#     title
	         	#     <blankline>
	         	#     lines of text
                 	#     <blankline if last line of text is not
			#      blank>
		 	#          on the first screen
		 	#
#-----------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------
#                 >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:  <return>       a        r        p      ^c
# To:          [next screen] [accept] [reject] [print] [abort]
# ----------------------------------------------------------------------------
# >
			#	   on the inbetween screens
# ----------------------------------------------------------------------------
#                >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:   <return>      a        r        p          b           ^c
# To:          [next screen] [accept] [reject] [print] [back a screen] [abort]
# ----------------------------------------------------------------------------
# >
			#	   on the last screen 
# ----------------------------------------------------------------------------
#                 >>>>>>>> For this License Agreement <<<<<<<<
# Enter either:   a        r        p          b           ^c
# To:          [accept] [reject] [print] [back a screen] [abort]
# ----------------------------------------------------------------------------
# >
#
#-----------------------------------------------------------------------------
		 	#
		 	# files:
		 	#
		 	#     $LSCREEN  - (/tmp/$$lscreen) scratch
			#		   directory created if
			#		   necessary. 
		 	#
		 	# $temp_file	- copyright banner  (imbedded)
		 	# $temp_file2	- license banner
		 	# $LICENSE_FILE - license agreement
		 	# 		  (CD-ROM:
		 	# 		   $CDROM/license.txt   - not HP
		 	#		   $CDROM/LICENSE.TXT;1 - HP)
		 	#		  (FTP:
		 	# 		   $MATLAB/license.txt)
		 	#
                 	# Always returns a 0 status.
                 	# 
                 	# usage: accept_license
                 	# 
#//////////////////////////////////////////////////////////////////////////
# Copyright banner
cat > $temp_file << EOF
   ---------------------------------------------------------------------------
   |(c) Copyright 1984-1998 The MathWorks, Inc.                              |
   |    All Rights Reserved                                                  |
   |    MATLAB and Simulink are registered trademarks of The MathWorks, Inc. |
   ---------------------------------------------------------------------------
EOF
#
#//////////////////////////////////////////////////////////////////////////
# License banner
cat > $temp_file2 << 'EOF'
   ---------------------------------------------------------------------------
   | Please review the license agreement on the next set of screens. On any  |
   | screen the options to proceed are at the bottom. Type your response     |
   | at the prompt.                                                          |
   ---------------------------------------------------------------------------
EOF
#//////////////////////////////////////////////////////////////////////////
#
        LICENSE_FILE="$license_txt"
#
#                       Build temporary directory for screens
#                       -------------------------------------
#
        LSCREEN=/tmp/$$lscreen
        rm -rf $LSCREEN
        mkdir $LSCREEN
#
        if [ "$batch" = "1" ]; then
            if [ "$CD_ROOT" = "" ]; then
	        clear_screen
	        printlicense
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '--------------------------------------------------------------------------'
echo ' a (accept)     r (reject)'
echo '--------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        echon "> "
	        answerquery 'accept'
	    else
#
# License already approved by CD-ROM front end
#
	        clear_screen
		welcome
	        echo ""
	        cat $temp_file | expand_tabs
#------------------------------------------------------------------------------
    echo ''
    echo '    License Agreement accepted by customer . . .'
    echo ''
#------------------------------------------------------------------------------
	    fi
        else
	    NLSCREENS=`license_screens`
#
# loop for screens
#
            nls=1
            while [ $nls -le $NLSCREENS ]
            do  
	        clear_screen
                cat $LSCREEN/$nls
	        printquery $nls $NLSCREENS
	        answerquery
	        if [ $? -ne 0 ]; then
#
# Reject license agreement
#
#------------------------------------------------------------------------------
    echo ''
    echo '    License Agreement rejected by customer:  '"$CMDNAME"' terminated . . .'
    echo ''
#------------------------------------------------------------------------------
		    clean_temp_areas
    		    exit 1
	        fi
	    done
        fi
#
	rm -rf $temp_file $temp_file2 $temp_file3 $temp_file4 $LSCREEN
#
	return 0
    }
#=======================================================================
    answerquery () { # If no arguments then query the user for a
		     # response and determines how to continue.
		     #
		     #    1. ''    - next page (nls) but never more than 
		     #               $NLSCREENS
		     #    2. 'a'   - nls = $NLSCREENS + 1
		     #    3. 'r'   - returns a nonzero status
		     #    4. 'b'   - previous page (nls - 1) but never
		     #               less than 1
		     #    5. 'p'   - print the license file
		     #    6. other - print error and ask to continue
		     #
		     # If one argument then use this response.
		     #
                     # Returns a 0 status unless an error.
		     #
		     # answerquery ['continue'|'accept'|'reject']
		     #
	if [ $# -eq 0 ]; then
            read ans
            if [ "$ans" = "" ]; then
		if [ $nls -lt $NLSCREENS ]; then
                    nls=`expr $nls + 1`
		else
		    echon ''
	        fi
            elif [ `expr "//$ans" : '//[    ]*a.*'` -ne 0 ]; then
                nls=`expr $NLSCREENS + 1`
            elif [ `expr "//$ans" : '//[    ]*r.*'` -ne 0 ]; then
		return 1
            elif [ `expr "//$ans" : '//[    ]*b.*'` -ne 0 ]; then
		if [ $nls -gt 1 ]; then
                    nls=`expr $nls - 1`
		else
		    echon ''
	        fi
            elif [ `expr "//$ans" : '//[    ]*p.*'` -ne 0 ]; then
		lpr_license
	    else
#-----------------------------------------------------------------------------
    echo "Error: invalid input ($ans) . . ."
    echo ''
#-----------------------------------------------------------------------------
                echon 'Please press <return> to continue: '
                read ans
	    fi
	else
            echo $1
	fi
#
	return 0
    }
#=======================================================================
    expand_tabs() { # Filter to expand tabs. It is platform independent.
		    # Tab stops are at multiples of 8.
                    #
                    # Always returns a 0 status.
                    # 
                    # usage: expand_tabs
                    # 

	awk '
#-----------------------------------------------------------------------
    BEGIN { tab = "	" ; tabstop = 8 }
          { n = 0; line = ""
            for (i = 1; i <= length($0); i = i + 1) {
                c = substr($0,i,1)
                if (c != tab) {
                    n = n + 1
                    line = line c
                }
                else {
                    rem = n % tabstop
                    for (j = 1; j <= 8 - rem; j = j + 1) {
                        n = n + 1
                        line = line " "
                    }
                }
            }    
            print line
          }'
#-----------------------------------------------------------------------
	return 0
    }
#=======================================================================
    intr_char () { # Outputs the name of the interrupt character
		   # from stty -a.  It is platform specific:
		   #
		   # Older style: (sun4)
		   # erase  kill   werase rprnt  flush  lnext  susp   intr   quit   stop   eof
		   # ^?     ^U     ^W     ^R     ^O     ^V     ^Z/^Y  ^C     ^\     ^S/^Q  ^D     
		   # BUG: /usr/bin/stty can't be piped
		   #      Use /usr/5bin/stty if possible
		   #
		   # Newer style (SystemV): (the rest)
		   # intr = ^c; quit = ^|; erase = ^?; kill = ^u;
                   # 
                   # Always returns a 0 status.
                   # 
                   # usage: intr_char
                   # 
	case "$ARCH" in
	    sun4)
		if [ -f /usr/5bin/stty ]; then
                    /usr/5bin/stty -a | awk '
#-----------------------------------------------------------------------
      BEGIN { found = 0; default = "^C" }
            { if (found == 1) {
                   print $nf 
                   exit }
            } 
            { for ( i = 1; i <= NF; i = i + 1)
                  if ($i == "intr" ) {
                      nf = i
                      nr = NR;
                      found = 1
                  }
              next 
            } 
        END { if (found == 0) print default }'
#-----------------------------------------------------------------------
		else
		    echo "  "
		fi
		;;
	    *)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; default = "^C" }
	{ for (i = 1; i <= NF; i = i + 1)
		if ( $i == "intr" && $(i+1) == "=") {
		    intr = substr($(i+2),1,length($(i+2))-1)
		    print intr
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print default }'
#-----------------------------------------------------------------------
		;;
	esac
        return 0
    }
#=======================================================================
    license_screens () { # Builds the license screens
		         #
			 # 1. Determine the number of rows by calling
			 #    window_rows
			 #
			 #    Use rows = rows - 9
			 #
			 # 2. Uses first line from the license agreement
			 #    as the license title. The following is
			 #    appended to the license title:
			 #    ' [screen i of $NLSCREENS]'
			 # 
			 # 3. The total file to output is:
		         #
		         #	Version title (centered) - excluded in count
		         #	<blank line>             - excluded in count
		         #	copyright banner (5)  
			 #      license banner   (5)
		         #	<blank line>
		         #	license agreement first line
			 #	underline of first line
			 #	rest of license agreement file
			 #
			 #      length = total length - excluded count
			 #
			 # 4. The screens are of the form:
			 #
			 #	title (centered) - excluded in count
			 #      title underline  - excluded in count
			 #	<blankline>      - excluded in count
			 #	lines of text
			 #      <blankline>
			 #      6 query lines    - excluded in screen count
			 #	
			 #    NLSCREENS = ceil (length/nrows)
			 #
			 #    The number of lines on a screen <= nrows
			 #
			 # 5. The screens are named:
			 #
			 #    ${LSCREEN}$i, i = 1, ..., $NLSCREENS 
			 #
			 # 6. We assume that the copyright banner and
			 #    the first license agreement title are on the
			 #    first page
			 #
		         # Always returns a zero status.
		         #
			 # usage: license_screens
			 #
	nrows=`window_rows`
#
# Remove any leading whitespace from the license title
#
	lic_title=`cat "$LICENSE_FILE" | sed -n '1p' | expand_tabs | awk '
#-----------------------------------------------------------------------
		{ ix = index($0,$1); print substr($0,ix) }'`
#-----------------------------------------------------------------------
#
	length=`cat $temp_file $temp_file2 "$LICENSE_FILE" | wc -l | awk '
#-----------------------------------------------------------------------
		{ print ($1 + 2) }'`
#-----------------------------------------------------------------------
#
	NLSCREENS=`echo $length $nrows | awk '
#-----------------------------------------------------------------------
                { l = $1; r = $2; rem = l % r
                  if (rem > 0)
                        print (l - rem) / r + 1
                  else
                        print l/r }'`
#-----------------------------------------------------------------------
#
	echo $nrows $NLSCREENS $LSCREEN > $temp_file3
	welcome >> $temp_file3
	echo "$lic_title" >> $temp_file3
#
	cat $temp_file $temp_file2 | expand_tabs >> $temp_file3
	echo "" >> $temp_file3
#
	tmpline="$lic_title [screen 1 of $NLSCREENS]"
        underline=`echo "$tmpline" | awk '
#-----------------------------------------------------------------------
		{ ix = index($0,"[")
		  u = ""; for (i = 1; i <= ix - 2; i = i + 1) u = u "-" 
		  for (i = ix - 1; i <= length($0); i = i + 1) u = u " "
		  print u }'`
#-----------------------------------------------------------------------
	center_line "$tmpline" >> $temp_file3
	center_line "$underline" >> $temp_file3
	cat "$LICENSE_FILE" | sed -n '2,$p' | expand_tabs >> $temp_file3
#
# Build a script to build the screens.
#
# Screen #1:
#	     first title
#	     <blankline>
#	     nrows of text
#
# Screen #i: i = 2, ..., NLSCREEN
#	     second title with [screen 2 of $NLSCREEN] appended
#	     <blankline>
#	     nrows of text
#
# Pseudocode:
# 	      print "#!/bin/sh"
#  	      print "cat > $LSCREEN/$i << EOF"
#  	      print text
#  	      ...
#             print "EOF"
#  
	cat $temp_file3 | awk '
#-----------------------------------------------------------------------
# blankhead is 40 characters
#
    BEGIN { blankhead = "                                        "
	    iscreen = 1; count = 0
	    squote = sprintf ("%c", 39)   # set single quote
	  }
    NR == 1 { nrows = $1; nlscreen = $2; lscreen = $3; next }
    NR == 2 { title1 = $0; next }
    NR == 3 { titlex = $0; len = length(titlex)
	      u = ""; for (i = 1; i <= len; i = i + 1) u = u "-" 
	      next
	    }
    count == 0 { if (iscreen == 1) {
		      print "#!/bin/sh"
		      print "cat > " lscreen "/" iscreen " << " squote "EOF" squote
		      print title1
		      print ""
		 }
		 else {
		      print "cat > " lscreen "/" iscreen " << " squote "EOF" squote
		      title = titlex " [screen " iscreen " of " nlscreen "]"
	              len = 40 - (length(title) / 2 - length(title) % 2)
	              print substr(blankhead,1,len) title
		      print substr(blankhead,1,len) u
		      print ""
		 }
	       }
	       { if (count < nrows) {
		     count = count + 1
		     print $0
		 }
		 if (count == nrows) {
		     print "EOF"
		     count = 0
		     iscreen = iscreen + 1
		 }
	       }
    END { if (count != 0) print "EOF" }' > $temp_file4
#-----------------------------------------------------------------------
	chmod +x $temp_file4
	. $temp_file4
#
	echo $NLSCREENS
	return 0
    }
#=======================================================================
    lpr_license () { # Print the license file using lpr
                     # 
                     # Always returns a 0 status.
                     # 
                     # usage: lpr_license
                     # 
	while true
	do
#-----------------------------------------------------------------------------
    echo ' '
    echo 'Please specify printer name. Press <return> for default in [] . . .'
    echon "[$PRINTER] > "
#-----------------------------------------------------------------------------
	    read ans
            if [ "$ans" != "" ]; then
	        PRINTER=$ans; export PRINTER
	    fi
#-----------------------------------------------------------------------------
    echo ' '
#-----------------------------------------------------------------------------
	    lpr "$LICENSE_FILE"
	    if [ $? -eq 0 ]; then
#-----------------------------------------------------------------------------
    echo "lpr command succeeded. Press <return> to continue  . . ."
#-----------------------------------------------------------------------------
		read ans
		break
	    fi
#-----------------------------------------------------------------------------
    echo '--------------------------------------------------------------------------'
    echo "lpr command failed!  We are trying to print . . ."
    echo ' '
    echo "    $license_txt"
    echo ' '
    echon 'Do you want to try to print again? ([y]/n) '
#-----------------------------------------------------------------------------
            read ans
            if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
		break
	    fi
	done
        return 0
    }
#=======================================================================
    printlicense () { # Outputs to standard output: (for batch)
		      #
		      #		Version title (centered)
		      #		<blank line>
		      #	        copyright banner
		      #         license banner
		      #		<blank line>
		      #	        license agreement
		      #		<blank line>
                      #
		      # Always returns a zero status.
                      #
                      # usage: printlicense
                      #
#
	welcome
	echo ""
	cat $temp_file $temp_file2 | expand_tabs
	echo ""
#
# Underline the first line only
#
	cat "$LICENSE_FILE" | sed -n '1p' | expand_tabs | awk '
#-----------------------------------------------------------------------
		{ print $0
		  ix = index($0,$1)
		  u = ""; for (i = 1; i <= ix - 1; i = i + 1) u = u " " 
		  for (i = ix; i <= length($0); i = i + 1) u = u "-"
		  print u }'
#-----------------------------------------------------------------------
	cat "$LICENSE_FILE" | sed -n '2,$p' | expand_tabs
	echo ""
#
	return 0
    }
#=======================================================================
    printquery () { # Prints the query lines. If the two arguments are
		    # different then print the more case else print
		    # the end case
		    #
		    # Uses the intr parameter from stty -a to get the
		    # interrupt character.
		    #
		    # Always returns a zero status.
		    #
		    # usage: printquery n m
		    #
	n=$1
	m=$2
	intr=`intr_char`
	if [ "$n" != "$m" ]; then
	    if [ "$n" = "1" ]; then
# first screen
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:  <return>       a        r        p      $intr"
echo 'To:          [next screen] [accept] [reject] [print] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    else
# inbetween screens
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:   <return>      a        r        p          b           $intr"
echo 'To:          [next screen] [accept] [reject] [print] [back a screen] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	else
# last screen
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '----------------------------------------------------------------------------'
echo '                >>>>>>>> For this License Agreement <<<<<<<<'
echo "Enter either:   a        r        p          b           $intr"
echo 'To:          [accept] [reject] [print] [back a screen] [abort]'
echo '----------------------------------------------------------------------------'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	echon "> "
	return 0
    }
#=======================================================================
    window_rows () { # Outputs the number of rows in the display window
		     # from stty -a.  It is platform specific:
		     #
		     # sun4 style:
		     #
		     # speed 9600 baud, 66 rows, 118 columns
		     # ...
		     # BUG: /usr/bin/stty can't be piped.
		     #      Use /usr/5bin/stty if possible
		     #
		     # other style: (alpha,ibm_rs,sgi,sgi64,linux)
		     #
		     # speed 9600 baud; 66 rows; 118 columns
		     # ...
		     #
		     # another style: (lnx86)
		     #
		     # rows 66; columns 118;
		     #
		     # System V style: (sol2,hp700)
		     #
		     # speed 9600 baud; 
		     # rows = 66; columns = 118; ypixels = 858; ...
		     # ...
		     #
		     # Other platforms:
		     #    IRIX 5.3:  stty -a doesn't print the
		     #               rows and columens
                     # 
                     # Always returns a 0 status.
                     # 
                     # usage: window_rows
                     # 
	case "$ARCH" in
	    sun4)
		if [ -f /usr/5bin/stty ]; then
		    /usr/5bin/stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = 24; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows,")  {
		    nrows = $(i-1)
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		else
#
# Output (default - extra)
#
		    echo 16
		fi
		;;
	    alpha|ibm_rs|sgi|sgi64)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = 24; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows;")  {
		    nrows = $(i-1)
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		;;
	    lnx86)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = extra + 8; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows" ) {
		    nrows = substr($(i+1),1,length($(i+1))-1) + 0
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		;;
	    *)
		stty -a | awk '
#-----------------------------------------------------------------------
	BEGIN { found = 0; extra = 9; min = extra + 8; default = 24 }
	{ for (i = 1; i <= NF; i = i + 1)
		if ($i == "rows" && $(i+1) == "=") {
		    nrows = substr($(i+2),1,length($(i+2))-1) + 0
		    if (nrows < min) nrows = default
		    print (nrows - extra)
		    found = 1
		    exit
		}
	}
	END { if (found == 0) print (default - extra) }'
#-----------------------------------------------------------------------
		;;
	esac
        return 0
    }
#=======================================================================
    welcome () { # Output the welcome line.
                 #
                 # Always returns a 0 status.
                 #
                 # usage: welcome
                 #
	center_line "Welcome to the license manager install for MATLAB  `get_version`"
        return 0
    }
#=======================================================================
#=======================================================================
    cd_expand_pd_files () { # Expand the pd files for all arches on
			    # arch_list from the CD-ROM.
			    #
		            # Returns a 0 status unless a fatal error
			    # occurred.
		            #
                            # usage: cd_expand_pd_files
                            #
        clear_screen
#-----------------------------------------------------------------------
    echo '             Expand license product files from CD-ROM'
    echo '             ----------------------------------------'
    echo ''
#-----------------------------------------------------------------------
	mesg=`echo "lm" | awk '{printf "Working on %-10s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 update/pd/lm/tar.cmp`
	if [ -f "$CD_ROOT/../$path" ]; then
	    zcat < "$CD_ROOT/../$path" | (cd $LM_ROOT; tar -xf -)
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo ''
    echo ''
    echo "    Fatal Error: No file - $path on CD-ROM"
#-----------------------------------------------------------------------
	    return 1
	fi
        for arch in $arch_list
        do
	    mesg=`echo "lm/$arch" | awk '{printf "Working on %-10s . . . [Please wait] " , $1}'`
	    echon "$mesg"
	    path=`to9660 update/pd/lm/$arch/tar.cmp`
	    if [ -f "$CD_ROOT/../$path" ]; then
	        zcat < "$CD_ROOT/../$path" | (cd $LM_ROOT; tar -xf -)
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	    else
#-----------------------------------------------------------------------
    echo ''
    echo "    Error: No file - $path on CD-ROM"
#-----------------------------------------------------------------------
	    fi
	done
	mesg=`echo "lm/scripts" | awk '{printf "Working on %-10s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 update/pd/lm/scripts/tar.cmp`
	if [ -f "$CD_ROOT/../$path" ]; then
	    zcat < "$CD_ROOT/../$path" | (cd $LM_ROOT; tar -xf -)
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo ''
    echo ''
    echo "    Fatal Error: No file - $path on CD-ROM"
#-----------------------------------------------------------------------
	    return 1
	fi
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	query_to_continue
	return 0
    }
#=======================================================================
    cd_offload_doc () { # Take off the PostScript Installation Guide(s)
			# for UNIX from the CD-ROM.
			#
		        # Returns a 0 status unless a fatal error
			# occurred.
		        #
                        # usage: cd_offload_doc
                        #
        clear_screen
#-----------------------------------------------------------------------
    echo '             Offload PostScript Installation Guide for UNIX'
    echo '             ----------------------------------------------'
    echo ''
#-----------------------------------------------------------------------
	mesg=`echo "installguide.ps" | awk '{printf "Working on %-18s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 guide.ps`
	cp "$CD_ROOT/../$path" $LM_ROOT/installguide.ps
        chmod 644 $LM_ROOT/installguide.ps
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	mesg=`echo "installguide_a4.ps" | awk '{printf "Working on %-18s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 guide_a4.ps`
	cp "$CD_ROOT/../$path" $LM_ROOT/installguide_a4.ps
        chmod 644 $LM_ROOT/installguide_a4.ps
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
#
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	query_to_continue
	return 0
    }
#=======================================================================
    cd_offload_install () { # Take off the license manager installer
			    # for UNIX from the CD-ROM.
			    #
		            # Returns a 0 status unless a fatal error
			    # occurred.
		            #
                            # usage: cd_offload_install
                            #
        clear_screen
#-----------------------------------------------------------------------
    echo '             Offload license manager installer for UNIX'
    echo '             ------------------------------------------'
    echo ''
#-----------------------------------------------------------------------
	mesg=`echo "install_lm" | awk '{printf "Working on %-18s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 install`
	cp "$CD_ROOT/$path" $LM_ROOT/install_lm
        chmod 755 $LM_ROOT/install_lm
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	mesg=`echo "license.txt" | awk '{printf "Working on %-18s . . . [Please wait] " , $1}'`
	echon  "$mesg"
	path=`to9660 license.txt`
	cp "$CD_ROOT/../$path" $LM_ROOT/license.txt
        chmod 644 $LM_ROOT/license.txt
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
#
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	query_to_continue
	return 0
    }
#=======================================================================
    check_for_standalone  () { # Check whether area argement is standalone
			       # or not.
		    	       #
			       # Criteria:
			       # 1. It must not contain the install
			       #    directory for it to be standalone.
			       #
		    	       # Returns a 0 status if standalone else 1. 
			       #
                    	       # usage: check_for_standalone area
                    	       #
	if [ -d $1/install ]; then
	    return 1
	fi
	return 0
    }
#=======================================================================
    expand_pd_files () { # Expand any license pd files in
			 # $LM_ROOT/update/pd. Remove $LM_ROOT/update
			 # when done.
			 #
		         # Returns a 0 status unless a fatal error
			 # occurred.
		         #
                         # usage: expand_pd_files
                         #
        clear_screen
	file_list=""
        if [ -f $LM_ROOT/update/pd/lm/tar.cmp ]; then
            file_list="lm";
        fi
        if [ -f $LM_ROOT/update/pd/lm/scripts/tar.cmp ]; then
            file_list="$file_list lm/scripts";
        fi
        for arch in `get_archlist`
        do
	    if [ -f $LM_ROOT/update/pd/lm/$arch/tar.cmp ]; then
	        file_list="$file_list lm/$arch"
	    fi
	done
#
	if [ "$file_list" != "" ]; then
#-----------------------------------------------------------------------
    echo '                     Expand license product files'
    echo '                     ----------------------------'
    echo ''
#-----------------------------------------------------------------------
	    for file in $file_list
	    do
	        mesg=`echo "$file" | awk '{printf "Working on %-10s . . . [Please wait] " , $1}'`
	        echon  "$mesg"
	        zcat < $LM_ROOT/update/pd/$file/tar.cmp | (cd $LM_ROOT; tar -xf -)
		if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo ''
    echo '    Error: expand failure - $LM_ROOT/update/pd/'"$file"/tar.cmp
#-----------------------------------------------------------------------
		    return 1
		fi
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	    done
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	    query_to_continue
	fi
	return 0
    }
#=======================================================================
    fin () { # Output finish message
             #
             # Always returns a 0 status.
             #
             # usage: fin
             #
	clear_screen
#-----------------------------------------------------------------------
echo ''  
echo '    ---------------------------------------------------------------------'
echo '    | Finished! This completes the:                                     |' 
echo '    |                                                                   |' 
echo '    |       FLEXlm license manager installation for MATLAB              |'
echo '    |                                                                   |'
echo '    ---------------------------------------------------------------------' 
echo '' 
#-----------------------------------------------------------------------
        return 0
    }
#=======================================================================
    get_arch () { # Sets the value of ARCH.
                  #
                  # Always returns a 0 status.
                  #
                  # usage: get_arch
                  #
#
# Do not use ARCH if it exists in the environment
#
        ARCH=""
#
#============================= arch.sh (start) =============================
#!/bin/sh
#
# usage:        arch.sh
#
# abstract:     This Bourne Shell script determines the architecture
#		of the the current machine.
#
#		ARCH	  - Machine architecture
#
# note(s):	1. This routine must be called using a . (period)
#
# Copyright (c) 1986-1997 by The MathWorks, Inc.
# $Revision: 1.8 $  $Date: 1997/11/06 23:02:48 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#=======================================================================
	matlab5_arch () { # Determine the architecture for MATLAB 5
			  # and outputs it to standard output
                          #
                          # Always returns a 0 status.
                    	  #
                    	  # usage: matlab5_arch
                    	  #
	Arch="unknown"
#
	if [ -f /bin/uname ]; then
   	    case "`/bin/uname`" in
	        SunOS)					# sun4 and sol2
	            if [ -d /dev/pts ]; then
		        Arch="sol2"
	            else
		        Arch="sun4"
	            fi
	            ;;
	        HP-UX)					# hp700, hp800
	            if [ -f /bin/hp9000s700 ]; then
		        (/bin/hp9000s700) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="hp700"
		        fi
	            fi
	            if [ -f /bin/hp9000s800 ]; then
		        (/bin/hp9000s800) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="hp700"
		        fi
	            fi
	            ;;
	        IRIX)					# sgi
    	            if [ -f /bin/4d ]; then
		        (/bin/4d) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="sgi"
		        fi
	            fi
	            ;;
	        IRIX64)		# Want this to mean MIPS 4/64 bit OS sgi
    	            if [ -f /bin/4d ]; then
		        (/bin/4d) > /dev/null 2>&1
		        if [ $? -eq 0 ]; then
	    	            Arch="sgi64"
		        fi
	            fi
#
# Watch out! Some machines are not MIPS 4 (R4400)
#
		    if [ -f /bin/hinv ]; then
			cpu=`/bin/hinv | awk '
#-----------------------------------------------------------------------
	$1 == "CPU:" { print substr($3,1,2) }'`
#-----------------------------------------------------------------------
			if [ "$cpu" = "R4" ]; then
			    Arch=sgi
			fi
		    fi
	            ;;
	        OSF1)					# alpha
	    	    Arch="alpha"
	            ;;
	        AIX*)					# ibm_rs
#
# With AIX 4.1, uname can return more than just 'AIX'
#
	            Arch="ibm_rs"
	            ;;
	        Linux)
		    Arch="lnx86"
		    ;;
	        *)
		    :
	            ;;
	    esac
	elif [ -f /bin/arch ]; then				# early sun4
	    Arch="`/bin/arch`"
	fi
#
        echo $Arch
#
	return 0
    }
#
    if [ "$ARCH_CHECK" = "" -a "$ARCH_LIST" != "" ]; then
#
# Check input arguments $1, ..., $$# for -ARCH
#
	i=1
	while [ $i -le $# ]
	do
	    value=`eval echo '$'"$i"`
	    ARCH=`echo "$ARCH_LIST EOF $value" | awk '
#-----------------------------------------------------------------------
	{ for (i = 1; i <= NF; i = i + 1)
	      if ($i == "EOF") {
		  narch = i - 1
		  if (NF != i + 1) exit
	          break
	      }
	  for (i = 1; i <= narch; i = i + 1)
		if ("-" $i == $NF) {
		    print $i
		    exit
		}
	}'`
#-----------------------------------------------------------------------
	    if [ "$ARCH" != "" ]; then
		break
	    fi
	    i=`expr $i + 1`
	done
    fi
#
    if [ "$ARCH" = "" ]; then
	ARCH=`matlab5_arch`
    fi
    Arch=$ARCH
#============================= arch.sh (end) ===============================
#
        return 0
    }
#=======================================================================
    get_install_arches () { # Query the user for arches to install
		    	    #
		    	    # Either exists with a 0 status or returns
		    	    # with a 0 status.
		    	    #
                    	    # usage: get_install_arches
                    	    #
        clear_screen
#-----------------------------------------------------------------------
    echo '                      Select Architectures to Install'
    echo '                      -------------------------------'
    echo ''
    echo ' ------------------------------------------------------------------------'
    echo ' sol2   - Sun Sparc   (Solaris 2.5.1)      sgi     - SGI (IRIX 6.3)'
    echo ' hp700  - HP 9000     (HP-UX 10.20)        sgi64   - SGI (IRIX 6.4)'
    echo ' alpha  - DEC Alpha   (Digital UNIX 4.0D)  lnx86   - Linux (Linux 2.0.34)'
    echo ' ibm_rs - IBM RS/6000 (AIX 4.2)'
    echo ' ------------------------------------------------------------------------'
    echo ''
    echo ' You will now be prompted for each architecture . . .'
    echo ' Enter at each prompt one of the following . . .'
    echo ' ------------------------------------------------------------------------'
    echo ' y  (install)    Press <return>  (ignore)    n (ignore)'
    echo ' ------------------------------------------------------------------------'
    echo ''
#-----------------------------------------------------------------------
        arch_list=
        for arch in `get_archlist`
        do
	    mesg=`echo "$arch" | awk '{printf "%-7s ? (y/[n]) > " , $1}'`
	    echon  "$mesg"
            read ans
            if [ `expr "//$ans" : '//[Yy].*'` -gt 0 ]; then
	        if [ "$arch_list" = "" ]; then
	            arch_list=$arch
	        else
	            arch_list="$arch_list $arch"
	        fi
	    else
		continue
	    fi
        done
        if [ "$arch_list" = "" ]; then
	    clean_temp_areas
#-----------------------------------------------------------------------
    echo ''
    echo '^G    No architectures selected. program terminated . . .'
    echo ''
#-----------------------------------------------------------------------
	    exit 0
        fi
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
	query_to_continue
	return 0
    }
#=======================================================================
    get_lmroot () { # Verifies the value of LM_ROOT and determines
		    # AUTOMOUNT_MAP.
		    #
		    # LM_ROOT       - path without AUTOMOUNT prefix
		    # origLM_ROOT   - path with AUTOMOUNT prefix
		    # AUTOMOUNT_MAP - automount prefix mapping
		    #
		    # Always returns a 0 status.
		    #
                    # usage: get_lmroot
                    #
#
#                       Get "correct" LM_ROOT path from user
#                       ------------------------------------
#
        clear_screen
        if [ "$LM_ROOT" = "" ]; then
#-----------------------------------------------------------------------
    echo '              Determine license manager root directory (LM_ROOT) path'
    echo '              -------------------------------------------------------'
    echo ' '
    echo '    -------------------------------------------------------------------------'
    echo '    | Please enter the LM_ROOT directory path . . .                         |'
    echo '    |                                                                       |'
    echo '    | This path will be used in the following files:                        |'
    echo '    |                                                                       |'
    echo '    |     . $LM_ROOT/license.dat                                            |'
    echo '    |     . $LM_ROOT/lmopts.sh                                              |'
    echo '    |                                                                       |'
    echo '    | Be sure that if you have . . .                                        |'
    echo '    |     1. An AUTOMOUNTED filesystem - the path must force a mount        |'
    echo '    |     2. An AFS filesystem         - the path must be read-write        |'
    echo '    -------------------------------------------------------------------------'
    echo ''
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo '            License manager root directory path (LM_ROOT) information'
    echo '            ---------------------------------------------------------'
    echo ''
    echo "LM_ROOT directory: $LM_ROOT"
#-----------------------------------------------------------------------
	fi
#
        AUTOMOUNT_MAP=
        cpath=`/bin/pwd`
        while true
        do
            if [ "$LM_ROOT" = "" ]; then
                if [ "$CD_ROOT" != "" ]; then
#-----------------------------------------------------------------------
    echo  "LM_ROOT root directory? [$cpath]"
    echon '---------------------->  '
#-----------------------------------------------------------------------
                else
#-----------------------------------------------------------------------
    echo  "LM_ROOT root directory? [$ROOTDIR]"
    echon '---------------------->  '
#-----------------------------------------------------------------------
                fi
                read ans
                if [ "$batch" = "1" ]; then
                    echo $ans
                fi
                if [ "$ans" != "" ]; then
                    newLM_ROOT=$ans
                else
                    if [ "$CD_ROOT" != "" ]; then
                        newLM_ROOT=$cpath
                    else
                        newLM_ROOT=$ROOTDIR
                    fi
                fi
                (cd $newLM_ROOT) >/dev/null 2>&1
                if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo ''
    echo "    Sorry! Directory '$newLM_ROOT' does not exist . . ."
    echo ''
#-----------------------------------------------------------------------
                    if [ "$batch" = "1" ]; then
                        abort
                    fi
                    continue
                fi    
                if [ "$CD_ROOT" != "" ]; then
                    inside_cdrom=`echo $newLM_ROOT $ROOTDIR | awk '
#-----------------------------------------------------------------------
        { if (index($1,$2) == 1) print 1; else print 0 }'`
#-----------------------------------------------------------------------
                    if [ "$inside_cdrom" = "1" ]; then
#-----------------------------------------------------------------------
    echo ''
    echo "    Sorry! LM_ROOT directory cannot be inside of CD-ROM . . ."
    echo ''
#-----------------------------------------------------------------------
                        if [ "$batch" = "1" ]; then
                            abort
                        fi
                        continue
                    fi
                fi
	        check_for_standalone $newLM_ROOT
	        if [ $? -ne 0 ]; then
#-----------------------------------------------------------------------
    echo ''
    echo "    Sorry! LM_ROOT directory not standalone . . ."
    echo "           It contains an install directory."
    echo ''
#-----------------------------------------------------------------------
		    continue
		fi
		
	    else
		newLM_ROOT=$LM_ROOT
	    fi
	    LM_ROOT=`(cd $newLM_ROOT; /bin/pwd)`
#
# Determine the common suffix. Ask if the prefix mapping is an automount
# mapping.
#
# Algorithm:
#
#	1. start with the first path and get the first directory
#          ignore . separated by / and consecutive //.
#	2. get the first directory from the second path like in 1.
#	   If the directories match then go to 3
#	3. repeat steps 1 and 2 until either there is a difference
#	   or one of them runs out.
#
	    if [ "$LM_ROOT" != "$newLM_ROOT" ]; then
		AUTOMOUNT_MAP=`echo $LM_ROOT $newLM_ROOT | awk '
#-----------------------------------------------------------------------
			{ dquote = sprintf ("%c", 34)      # set double quote
			  i1 = length($1); i2 = length($2)
			  for (;;) {
			      dir1 = "."; dir2 = "."
			      while (dir1 == ".") { 
		                  for (;;) {
				      if (i1 == 0) break
			              c = substr($1,i1,1)
			              if (c == "/") {
				          i1 = i1 - 1
				          continue
				      }
				      else {
				          finish1 = i1
				          start1 = i1
				          break 
				      }
			          }
			          if (i1 == 0) break
			          for (;;) {
				      if (i1 == 0) {
				          start1 = 1
				          break
				      }
			              c = substr($1,i1,1)
			              if (c == "/") {
				          start1 = i1 + 1
				          break
				      }
				      i1 = i1 - 1
			          }
			          dir1 = substr($1,start1,finish1 - start1 + 1)
			      }
			      while (dir2 == ".") { 
		                  for (;;) {
				      if (i2 == 0) break
			              c = substr($2,i2,1)
			              if (c == "/") {
				          i2 = i2 - 1
				          continue
				      }
				      else {
				          finish2 = i2
				          start2 = i2
				          break 
				      }
			          }
			          if (i2 == 0) break
			          for (;;) {
				      if (i2 == 0) {
				          start2 = 1
				          break
				      }
			              c = substr($2,i2,1)
			              if (c == "/") {
				          start2 = i2 + 1
				          break
				      }
				      i2 = i2 - 1
			          }
			          dir2 = substr($2,start2,finish2 - start2 + 1)
			      }
			      if (i1 == 0 || i2 == 0) break
			      if (dir1 == dir2)
				  continue
			      else
				  break
			  }
			  if (i1 == 0 || (i1 == 0 && i2 == 0))
			      print ""
			  else if (i2 == 0)
			      print dquote substr($1,1,finish1), "->" dquote
			  else
			      print dquote substr($1,1,finish1), "->", substr($2,1,finish2) dquote
			  }'`
#-----------------------------------------------------------------------
		if [ "$AUTOMOUNT_MAP" != "" ]; then
#-----------------------------------------------------------------------
    echo ''
    echo 'Path prefix mapping is   '$AUTOMOUNT_MAP 
    echo ''
#-----------------------------------------------------------------------
		    echon 'Is this mapping due to AUTOMOUNTING? ([y]/n) '
		    if [ "$batch" = "1" ]; then
			ans='y'
		    else
			read ans
		    fi
		    if [ "$batch" = "1" ]; then
			echo $ans
            	    fi
        	    if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
			AUTOMOUNT_MAP=
			LM_ROOT=$newLM_ROOT
		    else
			origLM_ROOT=$LM_ROOT
			LM_ROOT=$newLM_ROOT
		    fi
		else
		    LM_ROOT=$newLM_ROOT
	        fi
	    fi
	    break
        done
        if [ "$origLM_ROOT" = "" ]; then
	    origLM_ROOT=$LM_ROOT
        fi
#-----------------------------------------------------------------------
    echo ''
#-----------------------------------------------------------------------
        query_to_continue
	return 0
    }
#=======================================================================
# From: lm.sh
#=======================================================================
    install_license_manager  () { # Install the FLEXlm license manager.
                    		  #
                	 	  # Returns a 0 status or exits.
                	 	  #
                	 	  # usage: install_license_manager
                	 	  #
        check_to_install_lm
        if [ $? -ne 0 ]; then
            clean_temp_areas
#-----------------------------------------------------------------------
    echo ''
    echo "    $CMDNAME stopped prematurely . . ."
    echo ''
#-----------------------------------------------------------------------
	    exit 1
        fi
#
        install_lm_scripts
#
        if [ "$minimal_install" != "1" ]; then
	    build_lm_links
        fi
#
        create_license_file
#    
        start_stop_mesg
#
	return 0
    }
#=======================================================================
    build_lm_links () { # Build symbolic links for lmboot and lmdown
			# if desired and possible.
                	#
                	# Returns a 0 status or exits.
                	#
                	# usage: build_lm_links
                	#
	clear_screen
	lm_txt 'links'
        if [ "$usermode" != "1" ]; then
	    lm_txt 'root_links'
            echon 'Do you want to create the symbolic links now? ([y]/n) '
            read ans
            if [ "$batch" = "1" ]; then
                echo $ans
            fi
            if [ `expr "//$ans" : '//[Nn].*'` -ne 0 ]; then
	        lm_txt 'root_links2'
		query_to_continue
            else
                rm -rf /etc/lmboot_TMW5 > /dev/null 2>&1
                ln -s $LM_ROOT/lmboot /etc/lmboot_TMW5
		lm_txt 'root_links3'
                rm -rf /etc/lmdown_TMW5 > /dev/null 2>&1
                ln -s $LM_ROOT/lmdown /etc/lmdown_TMW5
		lm_txt 'root_links4'
		query_to_continue
	    fi
        else
	    lm_txt 'user_links'
	    query_to_continue
        fi
	return 0
    }
#=======================================================================
    check_to_install_lm () { # Check to install license manager.
                	     #
                	     # Returns a 1 status if on return we are
			     # to finish up else a 0 status.
                	     #
                	     # usage: check_to_install_lm
                	     #
#
#		Install License Manager [1, 3, 4]
#               -----------------------
#
	clear_screen
        lm_txt 'install'
#
# Install lmhostid script at a minimum.
#
	if [ ! -d $LM_SCRIPTS ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    We found no license manager scripts . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	fi
	cd $LM_SCRIPTS
        file=$lmhostid_etc
        if [ -f $file ]; then
	    etcscript='lmhostid'
	    if [ -f $LM_ROOT/$etcscript ]; then
	        oldname=`get_oldname $LM_ROOT $etcscript`
	        mv -f $LM_ROOT/$etcscript $LM_ROOT/old/$oldname
	    fi
	    cp $file $LM_ROOT/$etcscript
	    chmod 755 $LM_ROOT/$etcscript
        fi
        if [ ! -d $LM_ROOT/$ARCH ]; then
	    otherarch=''
	    for arch in `get_archlist`
	    do
	        if [ -d $LM_ROOT/$arch ]; then
		    otherarch="$otherarch $arch"
	        fi
	    done
	    if [ "$otherarch" != "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    We found license manager executables for at least one architecture . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    We found no license manager executables for any architecture . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	    fi
        fi
        lm_txt 'preserve'
#
# Check for a demo license. The condition is that all noncommented
# FEATURE (or INCREMENT) lines except TWM_Archive must have DEMO in the last 
# field to be considered a DEMO license. Return the date on the first FEATURE
# line other than TWM_Archive if a DEMO file.
#
	if [ -f $LM_ROOT/license.dat ]; then
            ExpireDate=`cat $LM_ROOT/license.dat | awk '
#----------------------------------------------------------------------------
		BEGIN { state = 1; first = 1 }
		$1 != "FEATURE" && $1 != "INCREMENT" { next }
		$2 == "TMW_Archive" { next }
		    { if ( $NF != "DEMO" ) {
			  state = 0
			  exit
		      }
		      else if (first == 1) {
			  expiredate = $5
			  first = 0 
		      }
		    }
		END { if (state == 1) print expiredate }'`
#----------------------------------------------------------------------------
            if [ "$ExpireDate" ]; then
	        lm_txt 'demo'
	    fi
        fi
#
	echon 'Install the FLEXlm License Manager for MATLAB? ([y]/n) '
        read ans
        if [ "$batch" = "1" ]; then
            echo $ans
        fi
        if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
	    return 1
	fi
#
	return 0
    }
#=======================================================================
    create_license_file () { # Create license.dat file. Always create
			     # a skeleton file and merge in any existing
			     # license.dat file in $LM_ROOT
                	     #
                	     # Returns a 0 status or exits.
                	     #
                	     # usage: create_license_file
                	     #
	clear_screen
	lm_txt 'create'
	cd $LM_ROOT
        file=license.dat
#
        if [ "`search_path hostname`" = "" ]; then
            Hostname=`uname -n`
        else
            Hostname=`hostname`
        fi
#
        HostID=`$lmhostid_etc | sed -n -e '/FLEXlm host ID/s/^[^"]*"//p' \
			      | sed -n -e '/"/s/".*//p'`
#
	if [ "`actualpath $LM_ROOT/license.dat`" != "" ]; then
	    sed -e "s%|>HOSTNAME<|%$Hostname%g" \
   	        -e "s%|>HOST_ID<|%$HostID%g" \
	        -e "s%|>LM_ROOT<|%$LM_ROOT%g" \
   	            $file.skel > $temp_file
	    echo 'EOF' >> $temp_file
	    cat $LM_ROOT/license.dat >> $temp_file
	    merge_license $temp_file /dev/null > $file
	    rm -f $temp_file > /dev/null 2>&1
	    lm_txt 'merged'
	else
	    sed -e "s%|>HOSTNAME<|%$Hostname%g" \
   	        -e "s%|>HOST_ID<|%$HostID%g" \
	        -e "s%|>LM_ROOT<|%$LM_ROOT%g" \
   	            $file.skel > $file
	    chmod 644 $file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "    Created skeleton 'license.dat' file . . ."
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    lm_txt 'not_merged'
	fi
    	query_to_continue
#
	return 0
    }
#=======================================================================
    install_lm_scripts () { # Install the license manager scripts.
                	    #
                	    # Returns a 0 status or exits.
                	    #
                	    # usage: install_lm_scripts
                	    #
	clear_screen
	lm_txt 'scripts'
	cd $LM_SCRIPTS
#
        update=0
#
# Assume you cannot write in $LM_SCRIPTS
#
# 1. lmvendor is the 8.3 name of lm_matlab. Change it to lm_matlab if you
#    find it.
# 2. lmswitch is the 8.3 name of lmswitchr. Change it to lmswitchr if you
#    find it. 
#
# Watch out for ISO 9660 names.
#
        for file in *
        do
	    if [ -f $file ]; then
	        etcscript=`expr "//$file" : '.*/\([^.;]*\).*'`
	        etcscript=`echo $etcscript |  tr 'A-Z' 'a-z'`
	        case "$etcscript" in
		    lm*)
		        :
		        ;;
		    *)
		        continue
		        ;;
	        esac
	        if [ "$etcscript" = "lmvendor" ]; then
		    etcscript=lm_matlab
	        fi
	        if [ "$etcscript" = "lmswitch" ]; then
		    etcscript=lmswitchr
	        fi
	        if [ "$etcscript" = "lmopts" ]; then
		    etcscript=lmopts.sh
	        fi
                mesg=`echo "$etcscript" | awk '{ printf ("    %-15s", $1) }'`
	        case "$etcscript" in
		    lmopts.sh)
		        echon "$mesg"
                        merge_lsfile "$file" $LM_ROOT $etcscript
                        if [ $? -ne 0 ]; then
                            update=1
		        fi
		        ;;
		    *)
			echon "$mesg"
                        if [ -f $LM_ROOT/$etcscript ]; then
#----------------------------------------------------------------------------
                            cat "$file" | sed \
                                -e "s%|>LM_ROOT<|%$origLM_ROOT%g" \
                                -e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" \
                                > $temp_file
#----------------------------------------------------------------------------
                            diff $LM_ROOT/$etcscript $temp_file > /dev/null 2>&1
                            if [ $? -ne 0 ]; then
	        	        oldname=`get_oldname $LM_ROOT $etcscript`
                                mv -f $LM_ROOT/$etcscript $LM_ROOT/old/$oldname
                                mv $temp_file $LM_ROOT/$etcscript
                                chmod 755 $LM_ROOT/$etcscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "Replaced       $oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                            else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    fi
			    rm -f $temp_file
                        else
                            cp "$file" $LM_ROOT/$etcscript
                            chmod 755 $LM_ROOT/$etcscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial Copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                        fi
		        ;;
	        esac
            fi
        done 
        if [ "$update" != "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '    
    echo '    * '"'diff'"' output saved at bottom of any Updated script file.'
    echo '      Review old local changes at bottom and fix top of file if'
    echo '      necessary.'
    echo ' '    
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        else 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '    
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        fi
#
	query_to_continue
#
	return 0
    }
#=======================================================================
    lm_txt () { # Outputs license manager text
                #
                # Returns a 0 status unless msg_num out of range
                #
                # usage: lm_txt msg_pointer
                #
	msg_pointer=$1
	case "$msg_pointer" in
	    install)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '               Install the FLEXlm Network License Manager'
    echo '               ------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    preserve)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo '    ----------------------------------------------------------------------'
echo '    | NOTE: A pre-existing license file (license.dat) in the $LM_ROOT    |'
echo '    |       directory will be MERGED by this installation.               |'
echo '    ----------------------------------------------------------------------'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    demo)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    There is a EXISTING license.dat file and it appears to be a' 
    echo '    DEMO license with expiration date:'
    echo ' '
    echo "                          $ExpireDate"
    echo ' '
    echo '    You are NOT required to install the license manager for a DEMO'
    echo '    license.'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    scripts)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '                  Create scripts in $LM_ROOT directory'
    echo '                  ------------------------------------'
    echo ' '
    echo 'Creating $LM_ROOT scripts . . .'
    echo ''
    echo '    Script         Changed?       Old Copy in $LM_ROOT/old'
    echo '    ------         -------        ------------------------ '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '         Build symbolic links /etc/lmboot_TMW5, /etc/lmdown_TMW5'
    echo '         --------------------------------------------------------'
    echo ' '
    echo '    ------------------------------------------------------------------------'
    echo '    | If you want to be able to reboot your machine and have the license   |'
    echo '    | manager running after the reboot you need to create link files       |'
    echo '    |             /etc/lmboot_TMW5 -> $LM_ROOT/lmboot                      |'
    echo '    |             /etc/lmdown_TMW5 -> $LM_ROOT/lmdown                      |'
    echo '    | These links are OUTSIDE the LM_ROOT directory and REQUIRE superuser  |'
    echo '    | privilege to create.                                                 |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |                                                                      |'
    echo '    | You have the choice of creating the symbolic links:                  |'
    echo '    |      [a] Now.                                                        |'     
    echo '    |      [b] After the install is finished.                              |'
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links2)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    ------------------------------------------------------------------------'
    echo '    |  To create the links after the install is finished, execute the      |'
    echo '    |  the following UNIX command as superuser:                            |'
    echo '    |                                                                      |'
    echo '    |                         $LM_ROOT/lmboot -s                           |'
    echo '    |                                                                      |'
    echo '      $LM_ROOT = '$LM_ROOT
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links3)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '    /etc/lmboot_TMW5 -> $LM_ROOT/lmboot link created . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    root_links4)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    /etc/lmdown_TMW5 -> $LM_ROOT/lmdown link created . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    user_links)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    ------------------------------------------------------------------------'
    echo '    Skipped. Not superuser . . .'
    echo '    ------------------------------------------------------------------------'
    echo '    | To create the links after the install is finished, first become      |'
    echo '    | superuser, and then execute the following command at the UNIX        |'
    echo '    | prompt:                                                              |'
    echo '    |                                                                      |'
    echo '    |                       $LM_ROOT/lmboot -s                             |'
    echo '    |                                                                      |'
    echo '      $LM_ROOT = '$LM_ROOT
    echo '    ------------------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    create)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '               Create license.dat file in $LM_ROOT directory'
    echo '               ----------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    merged)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "    Merged in 'license.dat' file found in "'$LM_ROOT . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    not_merged)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '  ----------------------------------------------------------------------------'
echo '  | After completing this install program edit the $LM_ROOT/license.dat      |'
echo '  | file and make it match the relevant parts of the license passcode file   |'
echo '  | sent to you via E-mail or FAX. Skip this step if you were able to do it  |'
echo '  | earlier. If you did not receive this license passcode file or you think  |'
echo '  | you need more recent license passcode data PLEASE contact:               |'
echo '  |                                                                          |'
echo '  |           The MathWorks, Inc.           (508) 647-7000                   |'
echo '  |    email: service@mathworks.com                                          |'
echo '  |                                                                          |'
echo "  | Have available the 'lmhostid' string for your license server.            |"
echo "              The 'lmhostid' for this host($Hostname) is :   $HostID"
echo '  |                                                                          |'
echo '  | See the MATLAB installation guide for the structure of the license file. |'
echo '  ----------------------------------------------------------------------------'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    start_stop)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "                   Starting/Stopping the License Manager"
echo ' '
echo '   ----------------------------------------------------------------------'
echo '   | If your license file requires you to run the license manager then  |'
echo '   | at the UNIX prompt, login to your license manager host. Change     |'
echo '   | your directory to $LM_ROOT and execute the command:                |'
echo '   |                                                                    |'
echo '   |         lmstart         (to start the license manager)             |'
echo '   |         lmdown          (to stop the license manager)              |'
echo '   |                                                                    |'
echo "   | The same user that executed 'lmstart' should ALWAYS execute        |"
echo "   | 'lmdown'.                                                          |"
echo '    ---------------------------------------------------------------------'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		;;
	    *)
		return 1
		;;
	    esac
	return 0
    }
#=======================================================================
    merge_lsfile () { # Construct license script new file. If current
		      # file does not exist save new file and you are
		      # done. Otherwise, diff current file with newly
		      # built one. If the RCS Revision has not change
		      # then nothing to do. Otherwise add any
		      # differences to the end as comments. For
		      # example,
		      # 
		      # current:   base
		      # 	   #EOF-----------------------------
		      #            # diff ...
		      #	           # date: ....
		      #	           # > ...		
		      #	           # < ...
		      #
		      # 	   base
		      #	           differences
		      #
		      # new:	   file
		      #
		      # Strip off #EOF---- ... tail of file
		      #
		      # diff file base > new_differences
		      #
		      # If new_differences then replace current by:
		      #
		      # 	   file
		      # 	   <differences>
		      # 	   #EOF-----------------------------
		      #	           # diff file base
		      #	           # date: `date`
		      #	           # <new_differences>
		      #
		      # else
		      #
		      #     current is unchanged
                      #
                      # Returns a status of 1 if updated else 0.
                      #
                      # usage: merge_sfile name target_dir target_name
                      #
	name="$1"
	target_dir=$2
	target_name=$3
	current_path=$target_dir/$target_name
#
	if [ ! -f $current_path ]; then
	    cat "$name" | sed \
    		-e "s%|>LM_ROOT<|%$LM_ROOT%g" \
    		-e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" > $current_path
	    chmod 755 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else 
	    cat "$name" | sed \
    		-e "s%|>LM_ROOT<|%$LM_ROOT%g" \
    		-e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" > $temp_file
	    revnew=`cat $temp_file | awk '
#----------------------------------------------------------------------------
        BEGIN { crev = "#$" "Revision"; rev = "$" "Revision" }
            crev == $1 { print $2; exit }
             rev == $2 { print $3; exit }'`
#----------------------------------------------------------------------------
	    cat $current_path | awk '
#----------------------------------------------------------------------------
	BEGIN { state = 1 }
	/^#EOF----/ { state = 0 }
		    { if (state == 1) print }' >  $temp_file2
#----------------------------------------------------------------------------
	    rev=`cat $temp_file2 | awk '
#----------------------------------------------------------------------------
        BEGIN { crev = "#$" "Revision"; rev = "$" "Revision" }
            crev == $1 { print $2; exit }
             rev == $2 { print $3; exit }'`
#----------------------------------------------------------------------------
	    if [ "$rev" = "$revnew" -a "$rev" != "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 0
	    fi
	    cat $current_path | awk '
#----------------------------------------------------------------------------
	BEGIN { state = 0 }
	/^#EOF----/ { state = 1 }
		    { if (state == 1) print }' >  $temp_file3
#----------------------------------------------------------------------------
	    diff $temp_file $temp_file2 > $temp_file4 2>/dev/null
	    if [ -s $temp_file4 ]; then
		oldname=`get_oldname $target_dir $target_name`
		mv -f $current_path $target_dir/old/$oldname
#
		cat $temp_file $temp_file3 > $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "#EOF--------------------------------------------------------------------" >> $current_path
    echo "# diff $name(new) $name(old)" >> $current_path
    echo "# date: `date`" >> $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        cat $temp_file4 | sed 's/^/# /' >> $current_path
	        chmod 755 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Updated*       '"$oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	fi
        return 0
    }
#=======================================================================
    start_stop_mesg () { # Output message about starting and stopping
			 # the license manager.
                	 #
                	 # Returns a 0 status or exits.
                	 #
                	 # usage: start_stop_mesg
                	 #
	clear_screen
	lm_txt 'start_stop'
	query_to_continue
#
	return 0
    }
#=======================================================================
#=======================================================================
    move_files() { # Move all files in $LM_ROOT/etc up one directory
		   # and then delete $LM_ROOT/etc. Do this only if
		   # $LM_ROOT/etc exist initially.
		   #	
		   # Note: ALWAYS remove $LM_ROOT/etc/install_lm since
		   #       a copy already exists in $LM_ROOT and it
		   #       could be executing.
		   #
		   # At the end remove any $LM_ROOT/update directory
		   # 
		   # Returns a 0 status unless a fatal error occurred.
		   #
                   # usage: move_files
                   #
	if [ -d $LM_ROOT/etc ]; then
	    if [ -f $LM_ROOT/etc/install_lm ]; then
	        rm -f $LM_ROOT/etc/install_lm
	    fi
	    (cd $LM_ROOT/etc; tar -cf - .) | (cd $LM_ROOT; tar -xf -)
	    rm -rf $LM_ROOT/etc
	fi
	rm -rf $LM_ROOT/update
	return 0
    }
#=======================================================================
    prepare_area () { # Remove any etc directory.
		      #
		      # Returns a 0 status unless a fatal error
		      # occurred.
		      #
                      # usage: prepare_area
                      #
	rm -rf $LM_ROOT/etc
	return 0
    }
#=======================================================================
    to9660 () { # Convert ufs path to ISO 9660 form if is9660 is set to
		# 1 otherwise the path remains unchanged. Output the
		# result to standard output. The path must end in a /
	        # if the last item is a directory.
		#
                # Directories:   x -> upper(X)
                # Files:         x -> upper(X).;1
		#		 x.y -> Upper(X).Upper(Y);1
		#
		# We assume that all directories and files are 8.3
		# format.
	        #
		# Always returns 0 status.
		#
                # usage: to9660 path
                #
	if [ "$is9660" != "1" ]; then
	    echo $1
        else
	    echo "$1" | tr 'a-z' 'A-Z' | awk '
#-----------------------------------------------------------------------
	{ if (substr($1,length($1),1) == "/") 
		print $1
	  else {
	      n = split($1,a,"/")
	      if (index(a[n],".") != 0)
		  print $1 ";1"
	      else
		  print $1 ".;1"
	  }
	}'
#-----------------------------------------------------------------------
	fi
	return 0
    }
#=======================================================================
#
#=========================== hpuxbug.sh (start) ============================
#
# usage:        hpuxbug.sh
#
# abstract:     This Bourne Shell checks for and handles the HP-UX
#		10.x 'expr' bug.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1996-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.4 $  $Date: 1998/12/24 17:31:57 $
#----------------------------------------------------------------------------
#
# Check for HP-UX 10.x 'expr' bug.
#
    if [ "`expr 'CASE' : '\(CA=\)'`" = "0" ]; then 
	Arch="unknown"
        if [ -f /bin/hp9000s700 ]; then
	    (/bin/hp9000s700) > /dev/null 2>&1
            if [ $? -eq 0 ]; then
		Arch="hp700"
	    fi
	fi
        if [ -f /bin/hp9000s800 ]; then
            (/bin/hp9000s800) > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                Arch="hp700"
            fi
	fi
	if [ "$Arch" = "hp700" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '-------------------------------------------------------------------'
    echo '    Sorry! MATLAB cannot be installed on this HP system! We have'
    echo "           detected a HP bug in the 'expr' command that prevents"
    echo '           you from installing and running MATLAB. It affects'
    echo '           HP-UX 10.0, 10.01, 10.10, and 10.20 systems. A patch'
    echo '           directory has been provided and we will create it now'
    echo '           in /tmp. You must install this patch on every affected'
    echo '           HP system.'
    echo ' '    
#
	    d=`dirname $0`
	    cp "$d/HPUX10.PCH;1" /tmp/$$a
	    (cd /tmp; tar -xf /tmp/$$a; rm -f /tmp/$$a)
#
    echo '    Created patch directory: /tmp/hpux10.pch'
    echo ' '
    echo '    See the README file in this directory for installation'
    echo '    instructions.'
    echo '-------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
        else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '-------------------------------------------------------------------'
    echo '    Sorry! MATLAB cannot be installed on this system! We have'
    echo "           detected a bug in the 'expr' command that prevents"
    echo '           you from installing and running MATLAB. For help'
    echo '           please contact The MathWorks Technical Support:'
    echo ' '
    echo '               The MathWorks, Inc.'
    echo '               phone: (508) 647-7000'
    echo '               email: support@mathworks.com'
    echo '               web:   http://www.mathworks.com'
    echo ' '    
    echo '-------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    exit 1
	fi
    fi
#=========================== hpuxbug.sh (end) ==============================
#
# Determine if in batch
#
    interactive
    if [ $? -ne 0 ]; then
        batch=1
    else
	batch=0
    fi
#
# Determine the MATLAB (CDROM) root directory (ROOTDIR)
#
    scriptpath
    if [ $? -ne 0 ]; then
	exit 1
    fi
#
    case "$CMDNAME" in
	'INSTALL.;1')
		is9660=1
	        oncdrom=1
		license_txt="$ROOTDIR/../LICENSE.TXT;1"
		;;
	'install')
	        is9660=0
		oncdrom=1
		license_txt=$ROOTDIR/../license.txt
		;;
	'install_lm')
		is9660=0
		oncdrom=0
		license_txt=$ROOTDIR/license.txt
		;;
    esac
#
# Get the ARCH
#
    get_arch
#
# Verify input
#
    stat="OK"
    msg=""
    if [ $# -gt 1 ]; then
	stat=""
    fi
    while [ "$stat" = "OK" -a $# -gt 0 ]; do
	case "$1" in
	    -h|-help)		# -help: Help option.
		stat=""
		;;
	    *)
		if [ -d $1 ]; then 
                    LM_ROOT=`stdpath $1`
		else
		    msg="lmroot ($1) does not exist . . ."
		    stat=""
		fi
	        ;;
	esac
	shift
    done
#
    if [ "$stat" = "" ]; then
	if [ "$msg" != "" ]; then
#-----------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
#-----------------------------------------------------------------------
	fi
        if [ "$oncdrom" != "1" ]; then
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo " "
    echo "    Usage: install_lm  [-h|-help] | [lmroot]"
    echo " "
    echo "    -h|-help          - Help."
    echo "    lmroot            - License manager root directory" 
    echo " "
    echo "    Installs just the UNIX FLEXlm license manager manager for"
    echo "    MATLAB into a standalone area ONLY."
    echo " "
    echo "-----------------------------------------------------------------" 
#-----------------------------------------------------------------------
	else
#-----------------------------------------------------------------------
    echo "-----------------------------------------------------------------"
    echo " "
    echo "    Usage: install  [-h|-help] | [lmroot]"
    echo " "
    echo "    -h|-help          - Help."
    echo "    lmroot            - License manager root directory" 
    echo " "
    echo "    Installs just the UNIX FLEXlm license manager manager for"
    echo "    MATLAB from the CD-ROM into a standalone area ONLY."
    echo " "
    echo "-----------------------------------------------------------------" 
#-----------------------------------------------------------------------
	fi
	exit 1
    fi
#
    accept_license
#
    if [ "$oncdrom" = "1" ]; then
	CD_ROOT=$ROOTDIR
	get_lmroot
	get_install_arches
	prepare_area
#
# Expand update/pd files from CD-ROM into $LM_ROOT
#
	cd_expand_pd_files
	if [ $? -ne 0 ]; then
	    abort
	fi
	cd_offload_doc
        cd_offload_install
    else
	CD_ROOT=
	get_lmroot
	prepare_area
#
# Expand any pd files from $LM_ROOT/update/pd into $LM_ROOT
# and remove the update files
#
	expand_pd_files
	if [ $? -ne 0 ]; then
	    abort
	fi
    fi
    move_files
#
# Install the license manager.
#
    LM_SCRIPTS=$LM_ROOT/scripts
    lmhostid_etc=lmhostid
    minimal_install=0
    is_superuser
    if [ $? -ne 0 ]; then
	usermode=1
    else
	usermode=0
    fi
#
    install_license_manager
#
    fin
#
    clean_temp_areas
    exit 0
