#
# 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. Updates the bin scripts.
#
#		3. Always change
#
#		   MATLAB:
#
#		   TOOLBOXES:
#
#		   a. Simulink: put in include file links
#		   b. Runtime: Add a link to the MATLAB startup script
#			       in toolbox/runtime
#
#
# Copyright (c) 1992-2003 by The MathWorks, Inc.
# $Revision: 1.54.14.3 $  $Date: 2003/12/11 18:21:40 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#   merge_sfile  ()
#=======================================================================
    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
    }
#=======================================================================
#
#	   Build scripts with MATLAB's MATLABPATH [1]
#	   ------------------------------------------
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '              A1. Create scripts in $MATLAB/bin directory'
    echo '              -------------------------------------------'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    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
		    ;;
		*)
	            . $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_file2 $temp_file3 $temp_file4 
#
#//////////////////////////////////////////////////////////////////////////
#
    . $dir/$cont_sh
