#! /bin/sh
#
# usage:        local.sh
#
# abstract:     This Bourne Shell script:
#		1. Installs template M-files found in the lists
#		   template/.MWLIST*
#		2. Creates license.m file
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1992-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.23 $  $Date: 1998/12/24 17:31:39 $
#----------------------------------------------------------------------------
#
#=======================================================================
# 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
#
#
#			Inquire for License Number [1]
#			--------------------------
#
    cd $ML_TOOLBOX_DIR/local
#
    . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '    A3. Customize license M-file in $MATLAB/toolbox/local directory'
    echo '    ---------------------------------------------------------------'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    if [ ! -f license.m ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Create license.m in toolbox/local . . .'
    echo ' '
    echo '    You will now need to enter your MATLAB license number.'
    echo ' '
    echo '    Check your packing slip first if you need to locate it. If you'
    echo '    cannot find it or you do not have one at this time type in'
    echo '    some string that seems appropriate for now. For example common'
    echo '    strings are:' 
    echo ' '
    echo '        unknown     trial     demo'
    echo ' '
    echo '    NOTE: This Number allows The MathWorks to internally reference'
    echo '          your account when you call for technical support or other'
    echo "          service. The MATLAB 'license' command displays its value." 
    echo ' '
    echo '    If you need to fix the value at a later time simply reinstall'
    echo '    or edit the file $MATLAB/toolbox/local/license.m'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	LicenseNum=""
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'license.m already exists in toolbox/local . . .'
    echo ' '
    echo '    You will now need to verify your MATLAB license number.'
    echo ' '
    echo '    Check your packing slip first if you need to locate it. If you'
    echo '    cannot verify it at this time continue normally by pressing'
    echo '    the <return> key at the next prompt. If it is incorrect supply'
    echo '    the correct value.'
    echo ' '
    echo '    If you need to fix the value at a later time simply reinstall'
    echo '    or edit the file $MATLAB/toolbox/local/license.m'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	LicenseNum=`cat license.m | awk '
#----------------------------------------------------------------------------
	$1 == "a" { print substr($3,2,length($3)-3) }'`
#----------------------------------------------------------------------------
    fi
#
    while true
    do
	mesg="MATLAB License Number? ([$LicenseNum]) "
        . $dir/$echon_sh
	read ans
	ans=`expr "$ans" : '[ 	]*\([^ 	]*\)[ 	]*'`
	if [ "$batch" = "1" ]; then
	    if [ ! "$ans" ]; then
		ans=$LicenseNum
		if [ ! "$ans" ]; then
		    ans="unknown"
		fi
		echo $ans
	    else
	        echo $ans
	    fi
	elif [ ! "$ans" ]; then
	    ans=$LicenseNum
	fi
	if [ "$ans" -o "$LicenseNum" ]; then 
	    break
	fi
    done
#
#			Create license.m in toolbox/local [1]
#			--------------------------------
#
    if [ "$ans" != "$LicenseNum" ]; then
        file=license.m
        sed -e "s%|>LICENSE_NO<|%$ans%g" \
	    template/$file > $file
        chmod 644 $file
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    license.m created . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ' '
    echo '    license.m unchanged . . .'
    echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fi
	
    . $dir/$cont_sh
