#
# 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-2000 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.23 $  $Date: 2000/05/22 20:03:14 $
#----------------------------------------------------------------------------
#
#         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 '    |                                                                      |'
echo '    | *   matlab4   -> $MATLAB4/bin/matlab                                 |'
echo '    | *   cmex      -> $MATLAB4/bin/cmex                                   |'
echo '    | *   fmex      -> $MATLAB4/bin/fmex                                   |'
echo '    |                                                                      |'
echo '    | * rename link only if it exists                                      |'
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 cmex fmex'
#
            cd $ML_BIN_DIR
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo 'Linking to $MATLAB/bin scripts . . . '
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            for file in $bin_files
            do
#
# Remove any matlab5, mex5, cmex5, fmex5 files
#
	        if [ -f $Local_BinDir/${file}5 ]; then
	            rm -f $Local_BinDir/${file}5
                    if [ ! -f $Local_BinDir/${file}5 ] ; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    ${file}5 - deleted . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	            else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    ${file}5 - CANNOT delete . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	            fi
	        fi
#
# Determine the link type
#
	        if [ -f $file ]; then
		    Linktype=0
	            if [ -f $Local_BinDir/$file ]; then
#
# Does this point to v3, v4, alpha/beta v5, or v5.
#
# 1. $MATLAB/test directory exists - alpha/beta v5
# 2. $MATLAB/.VERSION file exists  - check for 4 or 5
# 3. $MATLAB/mex directory exits   - v3
# 4. Otherwise                     - v4
#
# Linktype: 0  - no file
#	    1  - points to shipping v5
#	    2  - points to alpha/beta v5
#	    3  - points to v4
#	    4  - points to v3
#	    5  - bad link
#
#
                        File=$Local_BinDir/$file
                        . $dir/$actualp_sh
                        if [ "$Filepath" != "" ]; then
			    Dirpath=`expr "$Filepath" : '\(.*\)/[^/]*'`
			    Dirpath=$Dirpath/..
			    (cd $Dirpath/test) > /dev/null 2>&1
			    if [ $? -eq 0 ]; then
			        Linktype=2
			    else
			        if [ -f $Dirpath/.VERSION ]; then
				    Ver=`cat $Dirpath/.VERSION | awk '{print substr($1,1,1)}'`
				    if [ "$Ver" = "5" ]; then
				        Linktype=1
				    else
				        Linktype=3
				    fi
			        else
				    (cd $Dirpath/mex) > /dev/null 2>&1
				    if [ $? -eq 0 ]; then
			    	        Linktype=4
				    else
				        Linktype=3
			            fi
			        fi
			    fi
		        else
			    Linktype=5
		        fi
		    fi
#
# Move the existing link first
#
		    fixlinkerr=0
		    case "$Linktype" in
		        0)		# no file
			    :
			    ;;
		        1)		# points to shipping v5
		            rm -f $Local_BinDir/$file
			    if [ -f $Local_BinDir/$file ]; then
			        fixlinkerr=1
			    fi
			    ;;
		        2)		# points to alpha/beta v5
		            rm -f $Local_BinDir/$file
			    if [ -f $Local_BinDir/$file ]; then
			        fixlinkerr=1
			    fi
			    ;;
		        3)		# points to v4
			    case $file in 
			        matlab)
		            	    mv -f $Local_BinDir/$file $Local_BinDir/${file}4 > /dev/null 2>&1
			    	    if [ -f $Local_BinDir/$file ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    CANNOT move link $file to ${file}4 . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			        	fixlinkerr=1
			    	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    ${file}4 -> "'$MATLAB4/bin/'$file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    	    fi
				    ;;
				cmex|fmex)
		            	    mv -f $Local_BinDir/$file $Local_BinDir/$file > /dev/null 2>&1
			    	    if [ -f $Local_BinDir/$file ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    CANNOT move link $file to $file . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			        	fixlinkerr=1
			    	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    $file -> "'$MATLAB4/bin/'$file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    	    fi
				    ;;
				*)
				    :
				    ;;
			    esac
			    ;;
		        4)		# points to v3
		            mv -f $Local_BinDir/$file $Local_BinDir/${file}4 > /dev/null 2>&1
			    if [ -f $Local_BinDir/$file ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    CANNOT move link $file to ${file}3 . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			        fixlinkerr=1
			    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    ${file}3 -> "'$MATLAB3/bin/'$file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			    fi
			    ;;
		        5)		# bad link
		            rm -f $Local_BinDir/$file
			    if [ -f $Local_BinDir/$file ]; then
			        fixlinkerr=1
			    fi
			    ;;	
		    esac
#
# Make the final link
#
		    case $file in
		        cmex|fmex)
			    :
			    ;;
			*)
		    	    if [ "$fixlinkerr" = "0" ]; then
	                        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 "    $file - CANNOT create link . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		        	fi
		    	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "    $file - CANNOT create link . . ."
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    	    fi
			    ;;
		    esac
	        fi
            done
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	. $dir/$cont_sh
    fi
