#
# usage:        bld_sbin.sh
#
# abstract:     This Bourne Shell script builds the scripts and installs them
#		in the bin directory.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-94 by The MathWorks, Inc.
# $Revision: 1.20 $  $Date: 1994/11/22 21:16:32 $
#----------------------------------------------------------------------------
#
#	   Build scripts with MATLAB's MATLABPATH [1]
#	   ------------------------------------------
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '              A1. Create scripts in $MATLAB/bin directory'
    echo '              -------------------------------------------'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    cd $ML_TOOLBOX_DIR
#
# If not in batch and it does not exist then do not query.
#
    if [ "$batch" != "1" -a ! -d "simulink" ]; then
	simulink=
    else
        . $dir/$tbxquery_sh
    fi
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Checking for MATLAB toolboxes . . .'
    echo ''
    mesg='    fixing path for: '
    . $dir/$echon_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    cat /dev/null > $temp_file2a
    cat /dev/null > $temp_file2b
    n=1
    for file in *
    do
	if [ -d $file ]; then
	    case "$file" in
		matlab)
		    (while read filepath
                     do
    echo 'MATLABPATH=$MATLABPATH:$MATLAB/toolbox/'$filepath >> $temp_file3
		    done) < ./matlab/.PATHLIST
 		    ;;
		simulink)
		    if [ "$simulink" = "1" ]; then
		        (while read filepath
                         do
    echo 'MATLABPATH=$MATLABPATH:$MATLAB/toolbox/'$filepath >> $temp_file2a
		         done) < ./simulink/.PATHLIST
		    else
			skip=1
		    fi
 		    ;;
		codegen)
		    if [ "$simulink" = "1" ]; then
    echo 'MATLABPATH=$MATLABPATH:$MATLAB/toolbox/'$file >> $temp_file2b
		    else
			skip=1
		    fi
		    ;;
		*)
		    if [ -f $file/.PATHLIST ]; then
			(while read filepath
                     	 do
    echo 'MATLABPATH=$MATLABPATH:$MATLAB/toolbox/'$filepath >> $temp_file
		    	 done) < ./$file/.PATHLIST
		    else
    echo 'MATLABPATH=$MATLABPATH:$MATLAB/toolbox/'$file >> $temp_file
		    fi
		    ;;
	    esac
#
	    if [ "$skip" != "1" ]; then
	        if [ $n -lt 5 ]; then
		    mesg="$file "
		    . $dir/$echon_sh
		    n=`expr $n + 1`
	        elif [ $n -eq 5 ]; then
           	    echo "$file"
		    n=6
	        else
		    mesg="                     $file "
		    . $dir/$echon_sh
		    n=2 
	        fi
	    fi
	    skip=
	fi
    done
    if [ $n -ne 6 ]; then
	echo ''
    fi
#
    if [ "$simulink" = "1" ]; then
	cat $temp_file2a $temp_file2b > $temp_file2
    else
	cat /dev/null > $temp_file2
    fi
#
    cd $ML_SCRIPT_DIR
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Creating $MATLAB/bin scripts . . .' 
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    for file in *.bin .*.bin
    do
	if [ -f $file ]; then
	    binscript=`expr "//$file" : '.*/\(.*\)\.bin$'`
	    mesg="    ./bin/$binscript : "
	    . $dir/$echon_sh
	    if [ -f $ML_BIN_DIR/$binscript ]; then
		mv -f $ML_BIN_DIR/$binscript $ML_BIN_DIR/${binscript}.old
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(old copy saved as ./bin/${binscript}.old)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(initial copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	    cat $file | awk ' BEGIN {
	        quote = sprintf ("%c", 39)
	        dquote = sprintf ("%c", 34)
	        linea = "echo " quote "Please run " dquote "install_matlab" dquote " before trying to run this script" quote
	        lineb = "exit 1"; state = 0 }
		NR == 1 { if ( substr ($1, 1, 1) == "#" )
			     { print; savenext = 1; next }
			}
		NR == 2 { if ( savenext == 1 )
			     { print; next }
			}
		linea == $0 { next }
		lineb == $0 { if ( state == 0 ) state = 1; else print; next }
		$0 == "#! /bin/sh" { if ( savenext == 1 )
					next
				     else
					{ print; next }
				   }
		          { print }' | sed \
    -e "s%|>MATLAB<|%$origMATLAB%g" \
    -e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" \
    -e "/^#[	 ]*=====> PLACE STANDARD MATLABPATHS HERE <=====.*/r $temp_file3" \
    -e "/^#[	 ]*=====> PLACE STANDARD MATLABPATHS HERE <=====.*/d" \
    -e "/^#[	 ]*=====> PLACE ADDITIONAL MATLABPATHS HERE <=====.*/r $temp_file" \
    -e "/^#[	 ]*=====> PLACE ADDITIONAL MATLABPATHS HERE <=====.*/d" \
    -e "/^#[	 ]*=====> PLACE SIMULINKPATH HERE <=====.*/r $temp_file2" \
    -e "/^#[	 ]*=====> PLACE SIMULINKPATH HERE <=====.*/d" > $ML_BIN_DIR/$binscript
#
# Drop the following for Convex and Kron Shell. In a layered product and not
# always available. Keep for reference.
#
#	    if [ "$Arch" = "c2mp" -a "$binscript" = "matlab" ]; then
#		sed '1s/sh/ksh/' $ML_BIN_DIR/$binscript > $temp_file4
#		mv -f $temp_file4 $ML_BIN_DIR/$binscript
#	    fi
	    chmod 755 $ML_BIN_DIR/$binscript
	fi
    done
    rm -f $temp_file $temp_file2a $temp_file2b $temp_file2 $temp_file3 $temp_file4 > /dev/null 2>&1
#
# Handle .mexrc.sh.MW -> .mexrc.sh
#
    if [ -f $ML_BIN_DIR/.mexrc.sh ]; then
	if [ -f $ML_BIN_DIR/.mexrc.sh.MW ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Manually merge $MATLAB/bin/.mexrc.sh.MW into $MATLAB/bin/.mexrc.sh'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '$MATLAB/bin/.mexrc.sh.MW renamed to $MATLAB/bin/.mexrc.sh'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	mv -f $ML_BIN_DIR/.mexrc.sh.MW $ML_BIN_DIR/.mexrc.sh
    fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    . $dir/$cont_sh
