#
# usage:        verifyp.sh
#
# abstract:     This Bourne Shell script verifies the MATLAB path determined
#		by install_matlab with the user. This is necessary to cover
#		all cases including the case when the MATLAB root directory
#		is in an automounted directory. A new path may be supplied by
#		the user.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-2003 The MathWorks, Inc. 
# $Revision: 1.21.4.2 $  $Date: 2003/12/11 18:21:53 $
#----------------------------------------------------------------------------
#
#                       Get "correct" MATLAB path from user [all]
#                       -----------------------------------
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '                      Verify MATLAB root directory path'
    echo '                      ---------------------------------'
    echo '    -------------------------------------------------------------------------'
    echo '    | The MATLAB root directory path has been determined to be:             |'
    echo ''
    echo '        $MATLAB = '$MATLAB
    echo ''
    case "$whichi" in
	1)     
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    | This path affects filesystem changes made in the following steps:     |'
    echo '    |                                                                       |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     A1. Create scripts in $MATLAB/bin directory                       |'
    echo '    |     A2. Generate MATLAB path in $MATLAB/toolbox/local/pathdef.m       |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    if [ "$minimal_install" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     B1. Create symbolic links to scripts in $MATLAB/bin directory     |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	    if [ "$usermode" = "0" -a "$minimal_install" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     C2. Build symbolic links /etc/lmboot_TMW, /etc/lmdown_TMW         |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     C3. Create template license.dat file in $MATLAB/etc directory     |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    ;;
	4)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    | This path affects filesystem changes made in the following steps:     |'
    echo '    |                                                                       |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    if [ "$usermode" = "0" -a "$minimal_install" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     C2. Build symbolic links /etc/lmboot_TMW, /etc/lmdown_TMW         |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |     C3. Create template license.dat file in $MATLAB/etc directory     |'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    ;;
    esac
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    |                                                                       |'
    echo '    | Check the MATLAB root directory path carefully! If it is part of:     |'
    echo '    |     1. An AUTOMOUNTED filesystem - the path must force a mount        |'
    echo '    |     2. An AFS filesystem         - the path must be read-write        |'
    echo '    | Press <return> if OK. Otherwise, provide the correct pathname below.  |' 
    echo '    -------------------------------------------------------------------------'
    echo ''
    echo 'MATLAB root directory? (['$MATLAB'])'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    AUTOMOUNT_MAP=
    while true
    do
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	mesg='--------------------->   '
	. $dir/$echon_sh
	read ans
	if [ "$batch" = "1" ]; then
	    echo $ans
	fi
	if [ "$ans" != "" ]; then
	    newMATLAB=$ans
	else
	    break
	fi
	(cd $newMATLAB) >/dev/null 2>&1
	if [ $? -ne 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "    Sorry! Directory '$newMATLAB' does not exist . . ."
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            if [ "$batch" = "1" ]; then
                . $dir/$abort_sh
	    fi
	else
#
# 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 [ "$MATLAB" != "$newMATLAB" ]; then
#==============================================================================
		AUTOMOUNT_MAP=`echo $MATLAB $newMATLAB | 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 ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    mesg='Is this mapping due to AUTOMOUNTING ([y]/n) '
		    . $dir/$echon_sh
		    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=
			MATLAB=$newMATLAB
		    else
			origMATLAB=$MATLAB
			MATLAB=$newMATLAB
		    fi
		else
		    MATLAB=$newMATLAB
		fi
	    fi
	    break
	fi
    done
    if [ "$origMATLAB" = "" ]; then
	origMATLAB=$MATLAB
    fi
#
