#
# usage:        ucleanpe.sh
#
# abstract:     Update cleaning of the $MATLAB files associated with
#               the piece designated by the value of the environment
#               variable PIECE_PATH or all the pieces with PIECE_PATH
#		is not defined.
#
# note(s):      1. For all pieces start with a global list of all files
#		   in temp_file2a. The format is:
#
#		   - file	- remove this file
#		   + file	- add this file
#
#		   Only delete files that have NO + versions.
#
#		   Delete any empty directories associated with the
#		   files that were deleted.
#
#		2. This routine must be called using a . (period)
#
# Copyright (c) 1994-1998 by The MathWorks, Inc.
# $Revision: 1.11 $  $Date: 1998/11/11 23:43:47 $
#----------------------------------------------------------------------------
#
#=======================================================================
#
# Functions:
#
#   add_installed_files ()
#
#=======================================================================
    add_installed_files () { # Add any associated install files to be
			     # deleted.
			     #
			     # Always return a 0 status.
			     #
			     # usage: add_installed_files list
			     #
#--------------------------------------------------------------------
# piece: 5.3 (R11) and later
#
	(
	echo 'bin/scripts/mbcxxopts.sh  bin/mbcxxopts.sh'
	echo 'bin/scripts/mbuild        bin/mbuild' 
	echo 'bin/scripts/mbuildopts.sh bin/mbuildopts.sh'
	) > $temp_file4
#--------------------------------------------------------------------
#
        echo 'EOF' >> $temp_file4
	cat $temp_file5 >> $temp_file4
#
	cat $temp_file4 | awk '
#--------------------------------------------------------------------
	BEGIN { state = 0 }
	$1 == "EOF" { state = 1; next }
	state == 0 { a[$1] = $2; next }
		   { if ( a[$1] == "" )
			  print $1
		     else {
			  print $1
			  print a[$1]
		     }}' > $temp_file5
#--------------------------------------------------------------------
	return 0
    }
#=======================================================================
#
    if [ "$PIECE_PATH" ]; then
        startpath=`/bin/pwd`
        cd $MATLAB
#
# 3 header lines at the beginning of the file
#
        cat > $temp_file4 << EOF
#
cd $MATLAB
#
EOF
        case "$PIECE_PATH" in
	    matlab)
#--------------------------------------------------------------------
#
# Delete 5.0 files from install package
#
	echo ./install/welcome2.sh >> $temp_file4
#--------------------------------------------------------------------
		;;
		
            *)
                :
                ;;
        esac
#
# Skip over the first 3 lines which are not file/directories
#
        sed -e '4,$s/^/rm -rf /' -e '4,$s%$% > /dev/null 2>\&1%' $temp_file4 > $temp_file5
        chmod +x $temp_file5
#
        . $temp_file5
        cd $startpath
    else
        sort +1 -2 $temp_file2a | awk '
#--------------------------------------------------------------------
    BEGIN { sump = 0; sumn = 0; file = "" }
  $2 == file { if ($1 == "+") sump = sump + 1
               if ($1 == "-") sumn = sumn - 1
               next
             }
             { if (file != "" && sumn < 0 && sump == 0) print file
               file = $2
               sump = 0;
               sumn = 0;
               if ($1 == "+") sump = sump + 1
               if ($1 == "-") sumn = sumn - 1
             }
    END { if (sumn < 0 && sump == 0) print file }' > $temp_file5
#--------------------------------------------------------------------
	if [ -s $temp_file5 ]; then
#
# Add any associated installed files to $temp_file5
#
	    add_installed_files $temp_file5
#
# 3 header lines at the beginning of the file
#
	    cat > $temp_file4 << EOF
#
cd $MATLAB
#
EOF
	    cat $temp_file5 >> $temp_file4
            sed -e '4,$s/^/rm -f /' -e '4,$s%$% > /dev/null 2>\&1%' $temp_file4 > $temp_file5
	    mv $temp_file5 $temp_file4
	    chmod +x $temp_file4
            startpath=`/bin/pwd`
	    . $temp_file4
	    cd $startpath
#
# Determine any possible empty directories. Local case only.
# Each path starts with a name
#
	    cat /dev/null > $temp_file5
	    cat $temp_file4 | awk '
#--------------------------------------------------------------------
    NR == 1, NR == 3 { next }
           { if ( $0 == ":" ) next
             n = split($3,a,"/")
             str="."
             for (i = 1; i < n; i = i + 1) {
                if ( b[str] != "1" )
                    b[str] = "1"
                str = str "/" a[i]
             }
             if ( b[str] == "1" ) next
                b[str] = "1"
           }
    END { for (str in b)
            if ( b[str] == "1" ) print str }' | sort -r > $temp_file5
#--------------------------------------------------------------------
#
# Remove any directories that are empty. They are in reverse lexical order so
# that subdirectories can be deleted recursively in one pass.
#
	    dirlist=`cat $temp_file5`
	    if [ "$dirlist" != "" ]; then
	        for olddir in $dirlist
	        do
		    ls $MATLAB/$olddir > $temp_file4 2>/dev/null
    		    nl=`cat $temp_file4 | wc -l | awk '{print $1}'`
    		    if [ "$nl" = "0" ]; then
			rm -rf $MATLAB/$olddir > /dev/null 2>&1
    		    fi
	        done
	    fi
	fi
    fi
    rm -f $temp_file4 $temp_file5
