#
# usage:        bld_sbin.sh
#
# abstract:     This Bourne Shell script installs the scripts from the
#		bin/scripts directory to the bin directory and
#		installs help related files and links.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Creates pathdef.m in $MATLAB/toolbox/local
#
#		3. Updates the bin scripts.
#
#		4. Always change
#
#		   MATLAB:
#
#		   a. Fix invisibly any $MATLAB/sys/os/$ARCH/ library links
#		   b. Fix invisibly any $MATLAB/sys/opengl/$ARCH library links
#
#		   TOOLBOXES:
#
#		   a. Simulink: put in include file links
#		   b. Runtime: Add a link to the MATLAB startup script
#			       in toolbox/runtime
#
#
# Copyright (c) 1992-2000 by The MathWorks, Inc.
# $Revision: 1.53 $  $Date: 2000/05/30 20:51:33 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#   merge_sfile  ()
#   runtime_matlab_link ()
#=======================================================================
    merge_sfile () { # Construct 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
                     #
	name=$1
	target_dir=$2
	current_path=$target_dir/$name
#
	if [ ! -f $current_path ]; then
	    cat $name | sed \
    		-e "s%|>MATLAB<|%$origMATLAB%g" \
    		-e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" > $current_path
	    chmod 755 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else 
	    cat $name | sed \
    		-e "s%|>MATLAB<|%$origMATLAB%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=$name
		dir_oldname=$target_dir
		. $dir/$oldname_sh
		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
    }
#=======================================================================
    runtime_matlab_link () { # Create matlab link:
                             #
                             #     ln -s ../../bin/matlab matlab
                             #
                             # in:
                             #    
                             #     $MATLAB/toolbox/runtime
                             #
                             # if the directory exists.
                             #
                             # Always returns a 0 status.
                             #
                             # usage: runtime_matlab_link
                             #
        if [ -d $MATLAB/toolbox/runtime ]; then
            (cd $MATLAB/toolbox/runtime
             rm -f matlab
             ln -s ../../bin/matlab matlab)
        fi
        return 0
    }
#=======================================================================
#
#	   Build scripts with MATLAB's MATLABPATH [1]
#	   ------------------------------------------
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '              A1. Create scripts in $MATLAB/bin directory'
    echo '              -------------------------------------------'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    if [ -d ${ML_TOOLBOX_DIR}/local/path ]; then
        cd ${ML_TOOLBOX_DIR}/local/path
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Creating MATLAB path . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
        cat /dev/null > $temp_file	# MATLAB
        cat /dev/null > $temp_file2a	# Simulink
        cat /dev/null > $temp_file2b	# Stateflow
        cat /dev/null > $temp_file2c	# RTW
        cat /dev/null > $temp_file3	# All except MATLAB & Simulink & RTW
#
        for file in *.phl
        do
	    case "$file" in
		matlab*.phl)
	    	    cat $file |
	            (while read filepath
                     do
		        if [ -d "${ML_TOOLBOX_DIR}/../$filepath" -a "$filepath" != "" ]; then
#----------------------------------------------------------------------------
    echo "        matlabroot,'/"$filepath >> $temp_file
#----------------------------------------------------------------------------
		        fi
	            done) 
 		    ;;
		simulink*.phl)
		    cat $file |
		    (while read filepath
                     do
		        if [ -d "${ML_TOOLBOX_DIR}/../$filepath" -a "$filepath" != "" ]; then
#----------------------------------------------------------------------------
    echo "        matlabroot,'/"$filepath >> $temp_file2a
#----------------------------------------------------------------------------
			fi
	            done) 
 	            ;;
		stateflow*.phl)
		    cat $file |
		    (while read filepath
                     do
		        if [ -d "${ML_TOOLBOX_DIR}/../$filepath" -a "$filepath" != "" ]; then
#----------------------------------------------------------------------------
    echo "        matlabroot,'/"$filepath >> $temp_file2b
#----------------------------------------------------------------------------
			fi
	            done) 
 	            ;;
		rtw*.phl)
	   	    cat $file |
	            (while read filepath
                    do
		        if [ -d "${ML_TOOLBOX_DIR}/../$filepath" -a "$filepath" != "" ]; then
#----------------------------------------------------------------------------
    echo "        matlabroot,'/"$filepath >> $temp_file2c
#----------------------------------------------------------------------------
			fi
	            done) 
	            ;;
	        *)
	    	    cat $file |
		    (while read filepath
                    do
		        if [ -d "${ML_TOOLBOX_DIR}/../$filepath" -a "$filepath" != "" ]; then
#----------------------------------------------------------------------------
    echo "        matlabroot,'/"$filepath >> $temp_file3
#----------------------------------------------------------------------------
			fi
	    	     done) 
	            ;;
	    esac
        done
#
# Concatenate all files and add ":',..." to the end of all lines except
# for the last one where we use "',...".  Remove any duplicates.
#
        cat $temp_file $temp_file2a $temp_file2b $temp_file2c $temp_file3 | awk '
#----------------------------------------------------------------------------
    line[$1] == 1 || NF == 0 { next }
    { line[$1] = 1; print $1 }' |  awk '
#----------------------------------------------------------------------------
    BEGIN { squote = sprintf ("%c", 39)   # set single quote
            start = 0 }
    start == 0 { line = $0; start = 1; next }
               { print line ":" squote ",..."; line = $0 }
    END { print line squote ",..." }' > $temp_file4
#----------------------------------------------------------------------------
#
# Fix pathdef.m and put in local toolbox
#
        cd ${ML_TOOLBOX_DIR}
        cat local/template/pathdef.m | sed \
        -e "/^[^%].*<PLEASE FILL IN ONE DIRECTORY PER LINE>.*/r $temp_file4" \
        -e "/^[^%].*<PLEASE FILL IN ONE DIRECTORY PER LINE>.*/d" > local/pathdef.m
        chmod 644 local/pathdef.m
    fi
#
    cd $ML_BSCRIPTS_DIR
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Creating $MATLAB/bin scripts . . .' 
    echo ''
    echo '    Script         Changed?       Old Copy in bin/old'
    echo '    ------         -------        ------------------- '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    update=0
    for binscript in * .*
    do
	if [ -f $binscript ]; then
            mesg=`echo "$binscript" | awk '
#----------------------------------------------------------------------------
	{ printf ("    %-15s", $1) }'`
#----------------------------------------------------------------------------
	    case "$binscript" in
		*.sh)
	            . $dir/$echon_sh
		    merge_sfile $binscript $ML_BIN_DIR
		    if [ $? -ne 0 ]; then
			update=1
		    fi
		    ;;
		.*.sh)
	            . $dir/$echon_sh
		    merge_sfile $binscript $ML_BIN_DIR
		    if [ $? -ne 0 ]; then
			update=1
		    fi
		    ;;
		cmex|fmex)	# obsolete
	    	    if [ -f $ML_BIN_DIR/$binscript ]; then
		        oldname=$binscript
			dir_oldname=$ML_BIN_DIR
		        . $dir/$oldname_sh
		        mv -f $ML_BIN_DIR/$binscript $ML_BIN_DIR/old/$oldname
			cp $binscript $ML_BIN_DIR/$binscript
	    		chmod 755 $ML_BIN_DIR/$binscript
	    	    else
			cp $binscript $ML_BIN_DIR/$binscript
	    		chmod 755 $ML_BIN_DIR/$binscript
		    fi
		    ;;
		*)
	            . $dir/$echon_sh
	    	    if [ -f $ML_BIN_DIR/$binscript ]; then
#----------------------------------------------------------------------------
                        cat $binscript | sed \
                            -e "s%|>MATLAB<|%$origMATLAB%g" \
                            -e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" \
                            > $temp_file
#----------------------------------------------------------------------------
                        diff $ML_BIN_DIR/$binscript $temp_file > /dev/null 2>&1
                        if [ $? -ne 0 ]; then
                            oldname=$binscript
                            dir_oldname=$ML_BIN_DIR
                            . $dir/$oldname_sh
                            mv -f $ML_BIN_DIR/$binscript $ML_BIN_DIR/old/$oldname
                            mv $temp_file $ML_BIN_DIR/$binscript
                            chmod 755 $ML_BIN_DIR/$binscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "Replaced       $oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                        else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                        fi
	    	    else
#----------------------------------------------------------------------------
	    	        cat $binscript | sed \
    			    -e "s%|>MATLAB<|%$origMATLAB%g" \
    			    -e "s%|>AUTOMOUNT_MAP<|%$AUTOMOUNT_MAP%g" \
			    > $ML_BIN_DIR/$binscript
#----------------------------------------------------------------------------
	    		chmod 755 $ML_BIN_DIR/$binscript
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    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

    rm -f $temp_file $temp_file2a $temp_file2b $temp_file2c $temp_file2 $temp_file3 $temp_file4 
#
#//////////////////////////////////////////////////////////////////////////
#
# Fix $MATLAB/sys/os/$ARCH library links
#    
    (cd $ML_ROOT_DIR
     if [ -d sys/os ]; then
	cd sys/os
	sysls=`ls -d [a-z]*`
	if [ "$sysls" != "" ]; then
	    for dir in $sysls
	    do
	        if [ ! -d $dir ]; then
		    continue
		elif [ -f $dir/.LINKLIST ]; then
                    (cd $dir
         	     while read linkname arrow filename
         	     do
                         if [ -f "$filename" ]; then
                    	     rm -f $linkname
                    	     ln -s $filename $linkname
                 	 fi
         	     done) < $dir/.LINKLIST
		fi
	    done
	fi
     fi)
#//////////////////////////////////////////////////////////////////////////
#
# Fix $MATLAB/sys/opengl/$ARCH library links
#    
    (cd $ML_ROOT_DIR
     if [ -d sys/opengl/lib ]; then
	cd sys/opengl/lib
	sysls=`ls -d [a-z]*`
	if [ "$sysls" != "" ]; then
	    for dir in $sysls
	    do
	        if [ ! -d $dir ]; then
		    continue
		elif [ -f $dir/.LINKLIST ]; then
                    (cd $dir
         	     while read linkname arrow filename
         	     do
                         if [ -f "$filename" ]; then
                    	     rm -f $linkname
                    	     ln -s $filename $linkname
                 	 fi
         	     done) < $dir/.LINKLIST
		fi
	    done
	fi
     fi)
#//////////////////////////////////////////////////////////////////////////
#
# Add links to ./extern/include for Simulink
#
    if [ -d $ML_ROOT_DIR/toolbox/simulink ]; then 
	(cd $ML_ROOT_DIR/extern/include;
	 while read app linkname arrow filename
	 do
	     if [ "$app" = "simulink" ]; then
		 if [ -f $filename ]; then
		    rm -f $linkname
		    ln -s $filename $linkname
		 fi
	     fi
	 done) < $ML_ROOT_DIR/extern/include/.SIMLINKLIST
    fi
#
#//////////////////////////////////////////////////////////////////////////
#
# Add a link to the MATLAB startup script in toolbox/runtime
#
    runtime_matlab_link
#
#//////////////////////////////////////////////////////////////////////////
#
#
    . $dir/$cont_sh
