#
# usage:        ucleanpe.sh
#
# abstract:     Update cleaning of the $MATLAB files.
#
# note(s):      1. For all pieces start with a global list of all files
#		   in temp_file2. 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-2004 by The MathWorks, Inc.
# $Revision: 1.11.14.3 $  $Date: 2004/10/20 17:44:27 $
#----------------------------------------------------------------------------
#
    # When the default locale is not C this sort command can take a very
    # long time (from minutes to hours).  Temporarily set it to C just
    # for the sort command.  If we get rid of the +1 -2 options and just 
    # call sort, we wouldn't run into the speed problem.
    # We need to do the export to handle the case where LC_ALL or one of the
    # other LC_* variables is already set in the user's environment.
    origLC_ALL="$LC_ALL"
    LC_ALL=C
    export LC_ALL
    sort +1 -2 "$temp_file2" | awk '
#--------------------------------------------------------------------
    BEGIN { sump = 0; sumn = 0; file = "" }
  $2 == file { if ($1 == "+") sump = 1
               if ($1 == "-") sumn = 1
               next
             }
             { if (file != "" && sumn != 0 && sump == 0) print file
               file = $2
               sump = 0;
               sumn = 0;
               if ($1 == "+") sump = 1
               if ($1 == "-") sumn = 1
             }
    END { if (sumn != 0 && sump == 0) print file }' > "$temp_file5"
    LC_ALL="$origLC_ALL"
#--------------------------------------------------------------------
    if [ -s "$temp_file5" ]; then
#
# 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
    rm -f "$temp_file4" "$temp_file5"
