#
# usage:        update.sh
#
# abstract:     This Bourne Shell script updates the distribution files
#		There are three steps.
#
#		1. attempt to decrypt any files
#		2. initial cleaning MATLAB area
#		3. decompress and detar any files
#		4. final cleaning MATLAB area (any early 5.0 files)
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Decryption of files
#
#		   loop through all potential TMW_Archive INCREMENT lines
#		   and save the output of each attempt. If all attempts
#		   fail then output the file. Otherwise, output no
#		   special output.
#
# ------------------------------------------------------------------------
# 1. INCREMENT TMW_Archive MLM 5 01-feb-1994 0 01234567890123456789 "81" DEMO
#    License checkout failed.
#    Software Expired.
#---------------------
#    Good INCREMENT line with same string, "81", CAN decrypt this file. 
#---------------------
#    Good INCREMENT line with same string, "81", CANNOT decrypt this file.
#---------------------
# 2. INCREMENT TMW_Archive MLM 5 01-feb-1996 0 2BBD539889183D29B4D9 "81" DEMO
#    License checkout succeeded, but CANNOT decrypt this file.
# ------------------------------------------------------------------------
#
# ------------------------------------------------------------------------
#    'license.dat' file, $MATLAB/etc/license.dat, does not exist . . .
#
#    MATLAB =  ...
#
#    Decryption skipped . . .'
#
#---------------------
#    No TMW_Archive INCREMENT lines in $MATLAB/etc/license.dat . . .
# ------------------------------------------------------------------------
#
# ------------------------------------------------------------------------
#    No decryption software found. Looked for . . .
#
#        $MATLAB/update/bin/$ARCH/dear
#
#    Found decryption software for other MATLAB supported architectures:
#
#	sun4 dec_risc
#
#    If decryption is required, please use a system with one of these other
#    architectures to install MATLAB.
#
#---------------- If no other architectures then Contact the MathWorks.
#
#    Found NO decryption software for any other MATLAB supported architectures.
#
#    Please contact:    The MathWorks, Inc. Technical Support:
#                       Phone: 508-647-7000
#                       FAX:   508-647-7201
#                       Email: support@mathworks.com
#
# file(s):	1. Decryption of files
#
#		   temp_file	- all the piece_paths
#		   temp_file2a	- all TMW_Archive lines
#		   temp_file2b	- individual TMW_Archive line
#		   temp_file3   - dear output for one TMW_Archive line
#		   temp_file4   - all the output for all TMW_Archive lines
#
# ------------------------------------------------------------------------
#
# Copyright (c) 1994-1998 by The MathWorks, Inc.
# $Revision: 1.28 $  $Date: 1998/11/30 18:21:04 $
#----------------------------------------------------------------------------
#
#=======================================================================
#
# Functions:
#
#   check_alone ()
#   mark_for_deletion ()
#
#=======================================================================
    check_alone () { # Determines any standalone products. The current
		     # list is:
		     #
		     #    mathlib
		     #    cppmath
		     #
		     # Sets the variables:
		     #
                     #    extract_fix_only=1 (if standlone)
		     #    mathlib_only=1     (if it exists)
		     #    cppmath_only=1     (if it exists)
		     #    
		     # Always returns a 0 status.
                     #
                     # usage: check_alone
                     #
	trydir=$MATLAB/update/pd
#
	not_in_pd='ada applink 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
#
	mathlib_only=0
	if [ -d $trydir/mathlib ]; then
#
# Check that mathlib/.b is the only file other than directories.
#
	    list=`(cd $trydir/mathlib; ls -d * .[a-z]*) 2>/dev/null`
            found=1
            for file in $list
            do
                if [ ! -d $trydir/mathlib/$file -a "$file" != ".b" ]; then
                    found=0
                    break
                fi
            done
            if [ "$found" = "1" ]; then
		standalone="mathlib "
		mathlib_only=1
		extract_fix_only=1
	    fi
	fi
	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 [ "$mathlib_only" != "0" -o "$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 f -name '*.enc' -print) > $temp_file
	nfiles=`wc -l $temp_file  | awk '{print $1}'`
	if [ "$nfiles" != "0" ]; then
            updating=1
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'Found encrypted product files. Start decryption (please wait) . . .'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Determine if dear exists.
#
	    dear_exist=1
            File=$ML_UPDATE/bin/$ARCH/dear
            . $dir/$actualp_sh
            if [ "$Filepath" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '--------------------------------------------------------------------------------'
echo ''
echo '    No decryption software found. Looked for . . .'
echo ''
echo '        $MATLAB/update/bin/'"$ARCH"'/dear'
echo ''
echo "    MATLAB =  $MATLAB"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Look for other architectures in $MATLAB/update/bin
#
                other_dear=0
	        (cd $ML_UPDATE/bin) > /dev/null 2>&1
                if [ $? -eq 0 ]; then
	            start_archlist=`(cd $ML_UPDATE/bin; ls)`
	            if [ "$start_archlist" != "" ]; then
		        archlist=
		        for single_arch in $start_archlist
		        do
		            if [ -f $ML_UPDATE/bin/$single_arch/dear ]; then
			        archlist="$archlist $single_arch"
		            fi
		        done
		        if [ "$archlist" != "" ]; then
		            other_dear=1
		        fi
	            fi
	        fi
	        if [ "$other_dear" != "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '    Found decryption software for other MATLAB supported architectures:'
echo ''
echo "       $archlist"
echo ''
echo '    If decryption is required, please use a system with one of these other'
echo '    architectures to install MATLAB.'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    	else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '    Found NO decryption software for any other MATLAB supported architectures.'
echo ''
echo '    Please contact:    The MathWorks, Inc. Technical Support:'
echo '                       Phone: 508-647-7000' 
echo '                       FAX:   508-647-7201'
echo '                       Email: support@mathworks.com'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    	fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '    Decryption skipped . . .'
echo ''
echo '--------------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	        dear_exist=0
	    fi
#
# Determine if license.dat file exists
#
	    if [ "$dear_exist" = "1" ]; then
		license_exist=1
	        LM_LICENSE_FILE=$MATLAB/etc/license.dat; export LM_LICENSE_FILE
                File=$LM_LICENSE_FILE
                . $dir/$actualp_sh
                if [ "$Filepath" = "" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '--------------------------------------------------------------------------------'
echo ''
echo "    'license.dat'"' file, $MATLAB/etc/license.dat, does not exist . . .'
echo ''
echo "    MATLAB =  $MATLAB"
echo ''
echo '    Decryption skipped . . .'
echo ''
echo '--------------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		    license_exist=0
		fi
	    fi
	    if [ "$dear_exist" = "1" -a "$license_exist" = "1" ]; then
#
# Get all TMW_Archive lines that do not start with a #. Put in temp_file2a
#
            	cat /dev/null > $temp_file2a 2>&1
                cat $LM_LICENSE_FILE | awk '
#--------------------------------------------------------------------------
    BEGIN { block = 0; line = ""
            backslash = sprintf ("%c", 92)   # set backslash
          }
          { if (substr($NF,length($NF),1) != backslash) {
                if (block == 1) {
                    print line $0
                    line = ""
                    block = 0
                }
                else
                    print
                next
            }
# find index of last backlash
            for (i = length($0); i > 0; i = i - 1)
                if (substr($0,i,1) == backslash)
                    break
            if (block == 0)
                block = 1
            line = line substr($0,1,i-1)
        }
    END { if (block == 1) print line }' | awk '
#--------------------------------------------------------------------------
    $1 == "FEATURE" && $2 == "TMW_Archive"   { print }
    $1 == "INCREMENT" && $2 == "TMW_Archive" { print }' > $temp_file2a
#--------------------------------------------------------------------------
		nlines=`wc -l $temp_file2a  | awk '{print $1}'`
		if [ "$nlines" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ''
echo '--------------------------------------------------------------------------------'
echo ''
echo '    No TMW_Archive INCREMENT lines in $MATLAB/etc/license.dat . . .'
echo ''
echo "    MATLAB =  $MATLAB"
echo ''
echo '    Decryption skipped . . .'
echo ''
echo '--------------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		else
#
#
# Create filelist for 'dear' - strip off leading ./ and the tar.enc or tarr.enc
#
# Determine the list of directories that have tar.enc and/or tarr.enc files
#
	            (cd $ML_UPDATE/pd; find . -type f -name '*.enc' -print | tr '/' ' ' | awk '
#--------------------------------------------------------------------
    { s = $2; for (i = 3; i < NF; i = i + 1) s = s "/" $i
      if ( a[s] != 1 ) {
          print s
	  a[s] = 1
      }
    }' ) > $temp_file
#--------------------------------------------------------------------
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            	     LM_LICENSE_FILE=$temp_file2b; export LM_LICENSE_FILE
		     (while read piece_path
		      do
#--------------------------------------------------------------------
#  bpiece_path=`expr "//$piece_path" : '.*/\(.*\)'
#  bpiece_path_noext=`expr "$bpiece_path" : '\(.*\)\.[^.]*'`
#  dpiece_path=`expr "//$piece_path" : '//\(.*\)/.*'
#--------------------------------------------------------------------
#
# Map piece_path into tbx.'toolbox' name
#
			  tbxname=`echo $piece_path | awk '
#--------------------------------------------------------------------
	{ n = split($1, a,"/"); print "tbx." a[2]}'`
#--------------------------------------------------------------------
		          echo "$piece_path" > $temp_file2
#
#  Loop for: i = initial, r = revision -  currently 'dear' supports i only.
#
		          for version in i r
		          do
			      if [ "$version" = "i" ]; then
			          if [ -f $ML_UPDATE/pd/$piece_path/tar.enc ]; then
    			              mesg="    $piece_path: tar.enc -> "
	    	                      . $dir/$echon_sh
			          else
				      continue
			          fi
			      elif [ "$version" = "r" ]; then
			          if [ -f $ML_UPDATE/pd/$piece_path/tarr.enc ]; then
    			              mesg="    $piece_path: tarr.enc -> "
	    	                      . $dir/$echon_sh
			          else
				      continue
			          fi
			      fi
#
# All error output goes in $temp_file4
#
            		      cat /dev/null > $temp_file4 2>&1
#
# Loop for all TMW_Archive lines.
#
###############################################################################
        (n_archivel=0
	 decrypted=0
	 while read line
	 do
	     n_archivel=`expr $n_archivel + 1`
#--------------------------------------------------------------------------
    echo "$line" > $temp_file2b
#--------------------------------------------------------------------------
    	     echo "$n_archivel. $line" | awk '
#--------------------------------------------------------------------------
    { printf "%d.", $1
      for (i = 2; i <= NF; i = i + 1) printf " %s", $i 
      printf "\n" }' >> $temp_file4
#--------------------------------------------------------------------------
#
# Call dear to see if license can be checked out.
#
#    MATLAB is in the environment already.
#    LM_LICENSE_FILE has been set to $temp_file2b and put in the environment.
#
	     checkout=1
	     $ML_UPDATE/bin/$ARCH/dear > $temp_file3 2>&1
	     if [ $? -ne 0 ]; then
		  checkout=0
	     fi
	     vendor_string=`echo "$line" | awk '
#--------------------------------------------------------------------------
    BEGIN { squote = sprintf ("%c", 39)   # set single quote
	    dquote = sprintf ("%c", 34)   # set double quote
           }
    { vendorstring = $8
      ix = index(vendorstring,squote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,squote)
      }
      ix = index(vendorstring,dquote)
      while ( ix != 0 ) {
	  vendorstring = substr(vendorstring,1,ix-1) substr(vendorstring,ix + 1)
          ix = index(vendorstring,dquote)
      }
      print vendorstring }'` 
#--------------------------------------------------------------------------
	     if [ "$checkout" = "0" ]; then
    		 cat $temp_file3 | awk '
#--------------------------------------------------------------------------
    NR == 1       { printf "   Error: %-s\n", $0; next}
    $1 != "Press" { printf "          %-s\n", $0 }' >> $temp_file4
#--------------------------------------------------------------------------
                 . $dir/$decryptl_sh
		 if [ "$DECRYPT_PRODUCTS" != "" ]; then
		     found=`echo $tbxname $DECRYPT_PRODUCTS | awk '
#--------------------------------------------------------------------------
    { name = $1
      for (i = 2; i <= NF; i = i + 1)
         if ($i == name) {
	     print 1
	     exit
      	 }
      print 0 }'`
#--------------------------------------------------------------------------
		     if [ "$found" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '   Good INCREMENT line with same string, "'$vendor_string'", CAN decrypt this file.' >> $temp_file4
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		     else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '   Good INCREMENT line with same string, "'$vendor_string'", CANNOT decrypt this file.'  >> $temp_file4
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		     fi
		 else
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '   Good INCREMENT line with same string, "'$vendor_string'", CANNOT decrypt this file.'  >> $temp_file4
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		 fi
	     else
#
# Now call dear again and try to decrypt the file.
#
		 rm -f $temp_file3
	         $ML_UPDATE/bin/$ARCH/dear -f $temp_file2 > $temp_file3 2>&1
		 if [ "$version" = "i" ]; then
		     if [ -f $ML_UPDATE/pd/$piece_path/tar.Z ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'tar.Z'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		         decrypted=1
		         break
		     else
    		 	 cat $temp_file3 | awk '
#--------------------------------------------------------------------------
    NR == 1       { printf "   Error: %-s\n", $0; next}
    $1 != "Press" { printf "          %-s\n", $0 }' >> $temp_file4
#--------------------------------------------------------------------------
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '   License checkout succeeded, but CANNOT decrypt this file.' >> $temp_file4
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		     fi
		 elif [ "$version" = "r" ]; then
		     if [ -f $ML_UPDATE/pd/$piece_path/tarr.enc ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'tarr.Z'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		         decrypted=1
			 break
		     else
    		 	 cat $temp_file3 | awk '
#--------------------------------------------------------------------------
    NR == 1       { printf "   Error: %-s\n", $0; next}
    $1 != "Press" { printf "          %-s\n", $0 }' >> $temp_file4
#--------------------------------------------------------------------------
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '   License checkout succeeded, but CANNOT decrypt this file.' >> $temp_file4
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		     fi
		 fi
	     fi
	 done
	 if [ "$decrypted" = "0" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo 'not done'
echo '--------------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	     cat $temp_file4 | awk '{print "" $0}'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '--------------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	 fi
###############################################################################
	 ) < $temp_file2a
		          done
		      done) < $temp_file
		fi
	    fi
	fi
#
# Steps 2-3
#
	(cd $ML_UPDATE/pd; find . -type f -name '*.cmp' -print) > $temp_file
#	
# Change the extension from .cmp to .Z if required.
#
	(cd $ML_UPDATE/pd
	 while read file
	 do
	     filemext=`expr "$file" : '\(.*\).cmp'`
	     mv -f ${filemext}.cmp ${filemext}.Z
	 done) < $temp_file
#
# Determine the list of directories that have tar.Z and/or tarr.Z files
#
	(cd $ML_UPDATE/pd; find . -type f -name '*.Z' -print | tr '/' ' ' | awk '
#--------------------------------------------------------------------
    { s = $2; for (i = 3; i < NF; i = i + 1) s = s "/" $i
      if ( a[s] != 1 ) {
          print s
	  a[s] = 1
      }
    }' ) > $temp_file
#--------------------------------------------------------------------
	nfiles=`wc -l $temp_file  | awk '{print $1}'`
	if [ "$nfiles" != "0" ]; then
            updating=1
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo 'Extracting files: any initial cleaning done first (please wait) . . .'
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	    (cd $ML_UPDATE/pd
	     while read piece_path
	     do
#
# Initial cleaning - MATLAB and PIECE_PATH are assumed to be in
#		     the environment
#
        	 PIECE_PATH=$piece_path
		 export PIECE_PATH
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    mesg="    $PIECE_PATH: "
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	         . $dir/$echon_sh
#
		 . $dir/$icleanpe_sh
#
		 if [ -f $piece_path/tar.Z ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    mesg='tar.Z '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	             . $dir/$echon_sh
#
		     case "$ARCH" in
			sgi|sgi64)
	             	    zcat $piece_path/tar.Z | (cd $ML_ROOT_DIR; tar -xfB -)
			    ;;
			*)
	             	    zcat $piece_path/tar.Z | (cd $ML_ROOT_DIR; tar -xf -)
			    ;;
		     esac
		     if [ $? -eq 0 ]; then
		         rm -f $piece_path/tar.Z
		     fi
		 fi
		 if [ -f $piece_path/tarr.Z ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    mesg='tarr.Z '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	             . $dir/$echon_sh
#
		     case "$ARCH" in
			sgi|sgi64)
	             	    zcat $piece_path/tarr.Z | (cd $ML_ROOT_DIR; tar -xfB -)
			    ;;
			*)
	             	    zcat $piece_path/tarr.Z | (cd $ML_ROOT_DIR; tar -xf -)
			    ;;
		     esac
		     if [ $? -eq 0 ]; then
		         rm -f $piece_path/tarr.Z
		     fi
		 fi
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	     done) < $temp_file
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
#
# Step 4
#
	(cd $ML_UPDATE/pd; find . -type d -print | awk '{print substr($0,3)}') > $temp_file
#
	mark_for_deletion $temp_file
#
# Create the piece to version cross reference list as a side effect
#
	nfiles=`wc -l $temp_file  | awk '{print $1}'`
#
	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 and/or tarr.enc are still there AND there is
# no d file
#
#
	     if [ \( -f $piece_path/tar.enc -o -f $piece_path/tarr.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
		    . $dir/$fcleanpe_sh
		    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
