#
# usage:        update.sh
#
# abstract:     This Bourne Shell script updates the distribution files
#		There are three steps.
#
# note(s):      1. This routine must be called using a . (period)
#
# ------------------------------------------------------------------------
#
# file(s):	   temp_file	- all the piece_paths
#		   temp_file2	- all TMW_Archive lines
# 		   temp_file2a  - holds the list of files used in the deletion process
#		   temp_file3    
#
# Copyright (c) 1994-2000 by The MathWorks, Inc.
# $Revision: 1.32 $  $Date: 2000/07/27 19:42:12 $
#----------------------------------------------------------------------------
#
#=======================================================================
#
# Functions:
#
#   check_alone ()
#   mark_for_deletion ()
#
#=======================================================================
    check_alone () { # Determines any standalone products. The current
		     # list is:
		     #
		     #    cppmath
		     #
		     # Sets the variables:
		     #
                     #    extract_fix_only=1 (if standlone)
		     #    cppmath_only=1     (if it exists)
		     #    
		     # Always returns a 0 status.
                     #
                     # usage: check_alone
                     #
	trydir=$MATLAB/update/pd
#
	not_in_pd='matlab toolbox'
#
	standalone=""
#
# Check the standalone conditions:
#    1. No directory in not_in_pd list
#    2. .b only file other than directories at the top of standalone product
#       directory
#
        for prod in $not_in_pd
	do
	    if [ -d $trydir/$prod ]; then
		return 0
	    fi
	done
#
	cppmath_only=0
	if [ -d $trydir/cppmath ]; then
#
# Check that cppmath/.b is the only file other than directories.
#
	    list=`(cd $trydir/cppmath; ls -d * .[a-z]*) 2>/dev/null`
            found=1
            for file in $list
            do
                if [ ! -d $trydir/cppmath/$file -a "$file" != ".b" ]; then
                    found=0
                    break
                fi
            done
            if [ "$found" = "1" ]; then
		standalone=$standalone"cppmathlib"
		cppmath_only=1
		extract_fix_only=1
	    fi
	fi
	if [ "$cppmath_only" != "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '--------------------------------------------------------------------------------'
echo "Note: Standalone $standalone detected ..."
echo '--------------------------------------------------------------------------------'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	return 0
    }
#=======================================================================
    mark_for_deletion () { # Go through the update/pd tree and add an empty
			   # d file to all directories that need to be
			   # deleted.
			   #
			   # Currently the only pieces to be deleted end
			   # in sun4.
			   #
			   # IMPORTANT: It is assumed that the pieces are
			   #            constructed so that the arch is
			   #            ALWAYS terminal!
		     	   #    
		           # Always returns a 0 status.
                           #
                           # usage: mark_sun4_pieces dir_list
                           #
	grep '/sun4$' $1 | \
        (cd $ML_UPDATE/pd
	 while read piece_path
	 do
	     cat /dev/null > $piece_path/d
	 done)
	return 0
    }
#=======================================================================
#
# If no $MATLAB/update/pd directory do nothing - rmdeadf_sh has been removed
#
    if [ ! -d $ML_UPDATE/pd ]; then
#
        :
#
    else
	. $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo '                                Update MATLAB'
    echo '                                -------------'
    echo ''
    echo 'Working . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
        updating=0
#
# 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
#
	mark_for_deletion $temp_file
#
	nped=0
	cat /dev/null > $temp_file2
	cat /dev/null > $temp_file3
#
# $temp_file2a : holds the list of files used in the deletion process
#
	cat /dev/null > $temp_file2a
#
	(cd $ML_UPDATE/pd
	 while read piece_path
	 do
      	     PIECE_PATH=$piece_path
	     export PIECE_PATH
#
# Bits: d = 16, .b = 8, .r = 4, b = 2, r = 1
#
# 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
#	 4.  b and d
#	     Remove only $piece_path
#        5. .b and d
#	     Remove all.
#
#		Rest: Don't do right now - later
#
#	 6. .b and b r
#	 7. .b .r and b
#	 8. .b .r and b r
#	 
#		Rest: Are error cases
#
	     pecase=0
	     if [ -f $piece_path/d ]; then
	         pecase=`expr $pecase + 16` 
	     fi
	     if [ -f $piece_path/.b ]; then
	         pecase=`expr $pecase + 8`
             fi 
	     if [ -f $piece_path/.r ]; then
	         pecase=`expr $pecase + 4` 
	     fi
	     if [ -f $piece_path/b ]; then
	         pecase=`expr $pecase + 2`
             fi 
	     if [ -f $piece_path/r ]; then
	         pecase=`expr $pecase + 1` 
	     fi
#
# Do nothing if tar.enc is still there AND there is no d file
#
	     if [  -f $piece_path/tar.enc -a ! -f $piece_path/d ]; then
		 continue
	     fi
	     case $pecase in
		 0)		# case 0 - not a piece yet
		    :
		    ;;
	         8)		# case 1 - .b only
		    sed -n -e "1p" $piece_path/.b >> $temp_file2
		    sed -n -e '2,$s/^/+ /p' $piece_path/.b >> $temp_file2a
		    ;;
	         2)		# case 2 -  b only
		    if [ "$nped" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			nped=1
                        echo "1" > $temp_file3
		    fi
		    mv -f $piece_path/b $piece_path/.b
		    sed -n -e "1p" $piece_path/.b >> $temp_file2
		    sed -n -e '2,$s/^/+ /p' $piece_path/.b >> $temp_file2a
		    ;;
	        10)		# case 3 - .b and b
		    if [ "$nped" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			nped=1
                        echo "1" > $temp_file3
		    fi
		    . $dir/$ucleanpe_sh
		    sed -n -e '2,$s/^/- /p' $piece_path/.b >> $temp_file2a
		    sed -n -e '2,$s/^/+ /p' $piece_path/b >> $temp_file2a
		    mv -f $piece_path/b $piece_path/.b
		    sed -n -e "1p" $piece_path/.b >> $temp_file2
		    ;;
		18)		# case 4 - b and d
		    if [ "$nped" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			nped=1
                        echo "1" > $temp_file3
		    fi
		    echo "- update/pd/$piece_path/b" >> $temp_file2a
		    echo "- update/pd/$piece_path/d" >> $temp_file2a
		    ;;
		24)		# case 5 - .b and d
		    if [ "$nped" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Check for obsolete files and delete them (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			nped=1
                        echo "1" > $temp_file3
		    fi
		    . $dir/$ucleanpe_sh
		    sed -n -e '2,$s/^/- /p' $piece_path/.b >> $temp_file2a
		    echo "- update/pd/$piece_path/.b" >> $temp_file2a
		    echo "- update/pd/$piece_path/d" >> $temp_file2a
		    ;;
	        11)		# case 6 - .b and b r
                    echo "1" > $temp_file3
		    :
		    ;;
		14)		# case 7 - .b .r and b
                    echo "1" > $temp_file3
		    :
		    ;;
		15)		# case 8 - .b .r and b r
                    echo "1" > $temp_file3
		    :
		    ;;
		 *)		# error cases
                    echo "1" > $temp_file3
		    :
		    ;;
	     esac
	 done) < $temp_file
	if [ -f $ML_UPDATE/pe2ver ]; then
            oldname=pe2ver
            dir_oldname=$ML_UPDATE
            . $dir/$oldname_sh
            mv -f $ML_UPDATE/pe2ver $ML_UPDATE/old/$oldname
	fi
	sort -n +0 -1 $temp_file2 > $ML_UPDATE/pe2ver
	if [ "$updating" = "0" -a ! -f $temp_file3 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'No updating required . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	else
	    PIECE_PATH=
	    . $dir/$ucleanpe_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
#
	check_alone
#
	rm -f $temp_file $temp_file2 $temp_file2a $temp_file3
        . $dir/$cont_sh
        cd $WD
    fi
