#
# usage:        ucleanpe.sh
#
# abstract:     Update cleaning of the $MATLAB files associated with
#		the piece designated by the value of the envionment
#		variable PIECE_PATH. This is only for 4.2 and later.
#
# note(s):      1. We currently only handle the case when there are both
#		   files .b and b exist in
#
#			$MATLAB/update/pd/$piece_path/.b
#
#		   It is assumed that .b and b already exist.
#
#		   The complete algorithm will be implemented later.
#
#		2. The following variables must exist in the environment
#
#		       MATLAB
#		       PIECE_PATH
#
#		3. Current algorithm: (simple case)
#
#		   If .b and b are the same then nothing to do.
#
#		   Remove the first line of both files. Then determine the
#		   files in .b not in b. Then remove any files that are
#		   also in any of the current dependent .b files. The
#		   final list is the list to remove.
#
#		   For example, suppose
#
#			matlab/toolbox/doc
#
#		   	   .b - has tech support .html files
#			    b - does not have them
#
#		   The .b file in
#
#		        info/technote
#
#		   has the tech support .html files
#
#		   So when b -> .b it:
#
#			deletes all files in .b not in
#
#			    matlab/toolbox/doc/b
#			    info/technote/.b
#
#		4. This routine must be called using a . (period)
#
# Copyright (c) 1994-95 by The MathWorks, Inc.
# $Revision: 1.11 $  $Date: 1995/11/16 16:53:45 $
#----------------------------------------------------------------------------
#
    dotbfile=$MATLAB/update/pd/$PIECE_PATH/.b
    bfile=$MATLAB/update/pd/$PIECE_PATH/b
#
    if [ -f $dotbfile -a -f $bfile ]; then 
	diff $dotbfile $bfile > $temp_file4 2>&1
        nlines=`wc -l $temp_file4  | awk '{print $1}'`
	if [ "$nflines" != "0" ]; then
	    cat $bfile $dotbfile | awk '
#--------------------------------------------------------------------
    NR == 1 { next }
    NF > 2 { if (state == 0) state = 1; next }
	   { if (state == 0)
		 a[$NF] = "1"
	     else if ( a[$NF] != "1" )
		 print "rm -rf", $NF, " > /dev/null 2>&1" }
    END {print ":"}' > $temp_file4
#--------------------------------------------------------------------
	    case "$PIECE_PATH" in
		matlab)
#
# Be sure that bin/.mexrc.sh and ghostscript files are not deleted
#
sed -e "/bin\/\.mexrc\.sh /d" -e "/ghostscript/d" $temp_file4 > $temp_file5
mv $temp_file5 $temp_file4
		    ;;
		matlab/sun4)
sed -e "/ghostscript/d" $temp_file4 > $temp_file5
mv $temp_file5 $temp_file4
		    ;;
		matlab/hp700)
sed -e "/ghostscript/d" $temp_file4 > $temp_file5
mv $temp_file5 $temp_file4
		    ;;
		matlab/sol2)
sed -e "/ghostscript/d" $temp_file4 > $temp_file5
mv $temp_file5 $temp_file4
		    ;;
		toolbox/matlab/doc)
#
# Possible dependencies: info/technote
#
# Be sure that technote .html files are not deleted if they have been
# recorded as being installed, i.e. a .b file exists. If info/technote
# is processed after there is no problem.
#
dotbfile=$MATLAB/update/pd/info/technote/.b
if [ -f $dotbfile ]; then
    cat $dotbfile $temp_file4 | awk '
#--------------------------------------------------------------------
    NR == 1 { next }
    $1 == ":" { print; next }
    $1 == "rm" { if ( a[$3] != 1 ) print; next }
               { a[$NF] = 1 }' > $temp_file5
#--------------------------------------------------------------------
    mv $temp_file5 $temp_file4
fi
		    ;;
		toolbox/compiler/sun4)
trydir=$MATLAB/update/pd/mathlib/sun4
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
        if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/sun4/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/sun4\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
		    ;;
		toolbox/compiler/hp700)
trydir=$MATLAB/update/pd/mathlib/hp700
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
	if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/hp700/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/hp700\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
             	    ;;
		toolbox/compiler/sgi)
trydir=$MATLAB/update/pd/mathlib/sgi
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
	if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/sgi/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/sgi\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
		    ;;
		toolbox/compiler/ibm_rs)
trydir=$MATLAB/update/pd/mathlib/ibm_rs
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
	if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/ibm_rs/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/ibm_rs\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
		    ;;
		toolbox/compiler/sol2)
trydir=$MATLAB/update/pd/mathlib/sol2
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
	if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/sol2/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/sol2\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
		    ;;
		toolbox/compiler/alpha)
trydir=$MATLAB/update/pd/mathlib/alpha
if [ -d $trydir ]; then
    list=`(cd $trydir; ls -d * .[a-z]*) 2>/dev/null`
    found_dotb=0
    found_b=0
    found_tar=0
    for file in $list
    do
	if [ "$file" = ".b" ]; then
            found_dotb=1
	elif [ "$file" = "b" ]; then
	    found_b=1
	elif [ `expr "$file" : 'tar\.*'` -ne 0 ]; then
	    found_tar=1
        fi
    done
    if [ "$found_dotb" = "0" -a "$found_b" = "1" -a "$found_tar" = "1" ]; then
        :
    else
#
# Be sure that extern/lib/alpha/libmcc.a is not deleted.
#
	sed -e "/extern\/lib\/alpha\/libmcc\.a/d" $temp_file4 > $temp_file5
	mv $temp_file5 $temp_file4
    fi
fi
		    ;;
		*)
:
		    ;;
	    esac
    	    nl=`cat $temp_file4 | wc -l | awk '{print $1}'`
	    if [ "$nl" != "1" ]; then

#
# 3 header lines at the beginning of the file
#
		cat > $temp_file5 << EOF
#
cd $MATLAB
#
EOF
	        cat $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 is always of the form: ./...
#
	        cat /dev/null > $temp_file5
	        cat $temp_file4 | awk '
#--------------------------------------------------------------------
    NR == 1, NR == 3 { next }
           { if ( $0 == ":" ) next
             n = split($3,a,"/")
             str="."
             for (i = 2; i < n; i = i + 1)
                str = str "/" a[i]
             if ( str == "." )
                str = "./"
             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 > /dev/null 2>&1 
    fi
