#
# usage:        update.sh
#
# abstract:     This Bourne Shell script updates the distribution files.
#
# note(s):      1. This routine must be called using a . (period)
#
# file(s):	   temp_file	- all the piece_paths
# 		   temp_file2  - holds the list of files used in the deletion process
#
# Copyright (c) 1994-2003 by The MathWorks, Inc.
# $Revision: 1.32.14.2 $  $Date: 2003/12/11 18:21:51 $
#----------------------------------------------------------------------------
#
#=======================================================================
#
#
# If no $MATLAB/update/pd directory do nothing 
#
    if [ -d $ML_UPDATE/pd ]; then
#
	. $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '                                Update MATLAB'
    echo '                                -------------'
    echo ''
    echo 'Working . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Run through the pd directory
#
	cd $ML_ROOT_DIR
#
# Step 1
#
	(cd $ML_UPDATE/pd; find . -type d -print | awk '{print substr($0,3)}') > $temp_file
#
	alreadywarned=0
#
# $temp_file2 : holds the list of files used in the deletion process
#
	cat /dev/null > $temp_file2
#
	(cd $ML_UPDATE/pd
	 while read piece_path
	 do
#
# Bits: .b = 8, b = 2 
#
# Cases: 
#	 0.  Not a piece yet
#	     Nothing to do
#	 1. .b only		
#	     Nothing to do
#	 2.  b only
#	     Initial case.
#	 3. .b and b 
#	     Remove any differences
#	 
#		Rest: Are error cases
#
	     pecase=0
	     if [ -f $piece_path/.b ]; then
	         pecase=`expr $pecase + 8`
             fi 
	     if [ -f $piece_path/b ]; then
	         pecase=`expr $pecase + 2`
             fi 
#
# Do nothing if tar.enc is still 
#
	     if [  -f $piece_path/tar.enc ]; then
		 continue
	     fi
	     case $pecase in
		 0)		# case 0 - not a piece yet
		    :
		    ;;
	         8)		# case 1 - .b only
		    sed -n -e '2,$s/^/+ /p' $piece_path/.b >> $temp_file2
		    ;;
	         2)		# case 2 -  b only
		    if [ "$alreadywarned" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			alreadywarned=1
		    fi
		    mv -f $piece_path/b $piece_path/.b
		    sed -n -e '2,$s/^/+ /p' $piece_path/.b >> $temp_file2
		    ;;
	        10)		# case 3 - .b and b
		    if [ "$alreadywarned" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			alreadywarned=1
		    fi
		    sed -n -e '2,$s/^/- /p' $piece_path/.b >> $temp_file2
		    sed -n -e '2,$s/^/+ /p' $piece_path/b >> $temp_file2
		    mv -f $piece_path/b $piece_path/.b
		    ;;
		 *)		# error cases
		    :
		    ;;
	     esac
	 done) < $temp_file
	. $dir/$ucleanpe_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
	rm -f $temp_file $temp_file2 
        . $dir/$cont_sh
        cd $WD
    fi
