#
# usage:        bld_lbin.sh
#
# abstract:     This Bourne Shell script creates links to the MATLAB
#		./bin scripts if desired.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-2004 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.24.14.2 $  $Date: 2004/10/08 20:10:56 $
#----------------------------------------------------------------------------
#
#         Create symbolic links to the binaries if requested [1, 2, 3]
#         --------------------------------------------------
#
    cd "$WD"
#
    . "$dir/$clearsc_sh"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '       B1. Create symbolic links to scripts in $MATLAB/bin directory'
echo '       -------------------------------------------------------------'
echo ' '
echo '    ------------------------------------------------------------------------'
echo "    | To make 'matlab' and 'mex' valid commands on your system you have    |"
echo '    | a choice of:                                                         |'
echo '    |                                                                      |'
echo '    | [a] Creating symbolic links to the $MATLAB/bin scripts in a          |'
echo '    |     directory of your choice that is already on your UNIX search     |'
echo '    |     path, or                                                         |'
echo '    |                                                                      |'
echo '    | [b] Adding the $MATLAB/bin directory to your UNIX search path.       |'
echo '    ------------------------------------------------------------------------'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    mesg='Do you want to create symbolic links to $MATLAB/bin scripts? ([y]/n) '
    . "$dir/$echon_sh"
    read ans
    if [ "$batch" = "1" ]; then
	echo "$ans"
    fi
    if [ `expr "//$ans" : '//[Nn].*'` -ne 0 ]; then
	:
    else
#
#		Get the MATLAB binary destination directory
#		-------------------------------------------
#
	. "$dir/$clearsc_sh"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo '    -------------------------------------------------------------------------'
echo '    ! Please specify the full pathname of the ACCESS directory for MATLAB.  |'
echo '    |                                                                       |'
echo '    | Choose a directory that exists and is appropriate to your needs.      |'
echo '    | Common example is:  /usr/local/bin                                    |'
echo '    |                                                                       |'
echo '    | The ACCESS directory will contain the following links to scripts      |'
echo '    | built by step A1:                                                     |'
echo '    |                                                                       |'
echo '    |   matlab -> $MATLAB/bin/matlab                                        |'
echo '    |   mex    -> $MATLAB/bin/mex                                           |'
echo '    |   mcc    -> $MATLAB/bin/mcc (if you have the MATLAB Compiler)         |'
echo '    |   mbuild -> $MATLAB/bin/mbuild (if you have the MATLAB Compiler)      |'
echo '    -------------------------------------------------------------------------'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	direrror=0
        while true
        do
	    Local_BinDir=''
	    (cd /usr/local/bin) >/dev/null 2>&1
	    if [ $? -eq 0 ]; then
	        Local_BinDir=/usr/local/bin
	    else
	       (cd /usr/bin) >/dev/null 2>&1
	       if [ $? -eq 0 ]; then
	           Local_BinDir=/usr/bin
	       fi
	    fi
	    mesg='MATLAB access directory? (['$Local_BinDir']) '
	    . "$dir/$echon_sh"
	    read ans
	    if [ "$batch" = "1" ]; then
		echo "$ans"
	    fi
	    if [ "$ans" != "" ]; then
	        Local_BinDir="$ans"
	    fi
	    (cd "$Local_BinDir") >/dev/null 2>&1
	    if [ $? -eq 0 ]; then
#
# Be sure that it doesn't put it anywhere inside the MATLAB tree.
#
		savepath=`pwd`
		cd "$Local_BinDir"
		fullpath=`/bin/pwd`
		cd "$ML_ROOT_DIR"
	        MLfullpath=`/bin/pwd`
		cd "$savepath"
		found=`echo "$fullpath $MLfullpath" | awk '
				{ if (index($1, $2) == 1)
				      print 1
				  else
				      print 0 }'`
		if [ "$found" = "0" ]; then
		    break
		else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo "    Error: Directory '$Local_BinDir'"
echo '           cannot be inside the MATLAB directory tree . . .'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        	    if [ "$batch" = "1" ]; then
			direrror=1
		    fi
		fi
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo "    Error: Directory '$Local_BinDir'"
echo '           does not exist . . .'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        	if [ "$batch" = "1" ]; then
		    direrror=1
		fi
	    fi
	    if [ "$direrror" = "1" -a "$batch" = "1" ]; then
	        break
	    fi
        done
	if [ "$direrror" = "1" -a "$batch" = "1" ]; then
	    :
	else
#
# This is not executed on the CDROM ever - no file mapping required.
#
	    bin_files='matlab mex mcc mbuild'
#
            cd "$ML_BIN_DIR"
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo 'Linking to $MATLAB/bin scripts . . . '
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    errors=0
            for file in $bin_files
            do
	        if [ -f "$file" ]; then
#
# Remove any existing link first
#
		   rm -f "$Local_BinDir/$file" > /dev/null 2>&1
		
#
# Make the final link
#
	           ln -s "$ML_BIN_DIR"/"$file" "$Local_BinDir"/"$file" > /dev/null 2>&1
		   if [ $? -eq 0 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    $file -> "'$MATLAB'/bin/$file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	               chmod 755 "$ML_BIN_DIR"/"$file"
		   else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    Error: Cannot link $Local_BinDir/$file to $ML_BIN_DIR/$file."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		       errors=1
		   fi
	        fi
            done
	    if [ "$errors" = "1" ]; then
echo "      Incorrect symbolic links can cause unexpected results."
echo "      You should fix the links in $Local_BinDir to point to the correct files."
echo "      The most likely cause for this is that you do not have permissions"
echo "      to create or change these links."
echo ''
	    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	. "$dir/$cont_sh"
    fi
