#! /bin/sh
#
# usage:        local.sh
#
# abstract:     This Bourne Shell script:
#		1. Installs template M-files found in the lists
#		   template/.MWLIST*
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-2000 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.25 $  $Date: 2000/05/30 20:51:26 $
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#   merge_mfile  ()
#=======================================================================
    merge_mfile () { # Construct new M-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_mfile name target_dir
                     #
	name=$1
	target_dir=$2
	current_path=$target_dir/$name
#
	if [ ! -f $current_path ]; then
	    cat $name > $current_path
	    chmod 644 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "(Initial copy)"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else 
	    cat $name > $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 644 $current_path
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Updated*       '"$oldname"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		return 1
	    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "  No"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    fi
	fi
        return 0
    }
#=======================================================================
#			Install M-files in toolbox/local [1]
#			--------------------------------
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '          A2. Install M-files in $MATLAB/toolbox/local directory'
    echo '          -----------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    cd $ML_TOOLBOX_DIR/local/template
#
# Read the .MWLIST* files to get the MathWorks provided files.
#
    list=`cat .MWLIST*`
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Creating $MATLAB/toolbox/local M-files . . .' 
    echo ''
    echo '    M-file         Changed?       Old Copy in toolbox/local/old'
    echo '    ------         -------        -----------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    update=0
    for mfile in $list
    do
	if [ -f $mfile ]; then
            mesg=`echo "$mfile" | awk '
#----------------------------------------------------------------------------
	{ printf ("    %-15s", $1) }'`
#----------------------------------------------------------------------------
	    case "$mfile" in
		*)
	            . $dir/$echon_sh
		    merge_mfile $mfile $ML_TOOLBOX_DIR/local
		    if [ $? -ne 0 ]; then
			update=1
		    fi
		    ;;
	    esac
	fi
    done
    if [ "$update" != "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    * '"'diff'"' output saved at bottom of any Updated M-file.'
    echo '      Review old local changes at bottom and fix top of file if'
    echo '      necessary.'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fi
#
    . $dir/$cont_sh
#
