Index: trunk/tools/eam/rebalance/neb_check_reinsert.pl
===================================================================
--- trunk/tools/eam/rebalance/neb_check_reinsert.pl	(revision 41384)
+++ trunk/tools/eam/rebalance/neb_check_reinsert.pl	(revision 41385)
@@ -108,5 +108,5 @@
 
     unless (-e "$tgt_subdir/$movefile") {
-	print "MISSING FILE: $tgt_subdir/$movefile\n";
+	print "ERROR : MISSING FILE: $tgt_subdir/$movefile\n";
 	next;
     }
@@ -114,5 +114,5 @@
     # print "movefile: $movefile\n";
 
-    my ($ins_id) = $movefile =~ m|[0-9a-fA-F][0-9a-fA-F]/(\d+)\..*|;
+    my ($ins_id) = $movefile =~ m|^[0-9a-fA-F][0-9a-fA-F]/(\d+)\..*|;
     my $summary = "$tgt_subdir/$movefile";
 
Index: trunk/tools/eam/rebalance/rebalance.mana
===================================================================
--- trunk/tools/eam/rebalance/rebalance.mana	(revision 41384)
+++ trunk/tools/eam/rebalance/rebalance.mana	(revision 41385)
@@ -12,10 +12,14 @@
 $VERSION = NONE
 
-$NSUBDIR = 1
-# $NSUBDIR = 0x100
-
-macro run.find
+# $NSUBDIR = 1
+$NSUBDIR = 0x100
+
+# alternatve code for subdirs:
+# list subdirs -glob $source/[0-9,a-f][0-9,a-f]
+# for i 0 $subdirs:n
+
+macro run.find.test
   if ($0 != 7)
-    echo "USAGE: run.find (srchost) (srcvol) (tgthost) (tgtvol) (VERSION) Fraction)"
+    echo "USAGE: run.find.test (srchost) (srcvol) (tgthost) (tgtvol) (VERSION) (Fraction)"
     echo "srchost, tgthost : e.g., ipp070"
     echo "srcvol, tgtvol   : e.g., 0 (for /data/ipp070.0/nebulous)"
@@ -26,4 +30,5 @@
   local srchost srcvol tgthost tgtvol Fraction
 
+  # NOTE: tgthost and tgtvol are not actually used in this step
   $srchost = $1
   $srcvol = $2
@@ -43,5 +48,5 @@
   ## partitions
 
-  $result = `df -l $source | tail -n 1`
+  $result = `df -l -m $source | tail -n 1`
   if ("$result" == "") 
     echo "invalid source partition or partition is not local"
@@ -53,4 +58,78 @@
 
   $Nbytes = 1024*1024*((1 - $Fraction)*$word:1 - $word:3)
+
+  echo "MB total : $word:1";                            # e.g. 100M
+  echo "MB used  : $word:2";                            # e.g.  95M
+  echo "MB avail : $word:3";                            # e.g.   5M
+
+  echo goal used : {$Fraction*$word:1};                 # e.g., 10M
+  echo to remove : {$word:2 - $Fraction*$word:1};       # e.g., 95M - 90M = 5M
+  echo alt calc  : {(1 - $Fraction)*$word:1 - $word:3}; # e.g., 10M - 5M = 5M
+
+  echo Nbytes to remove: $Nbytes
+  echo NGB to remove   : {$Nbytes/1024^3}
+
+  if ($Nbytes < 0)
+    echo "source partition is already below goal fraction"
+    echo "Nbytes to remove : $Nbytes"
+    break
+  end
+
+  # calculate the number of bytes to remove per subdir
+  $NremoveSub = $Nbytes / 256
+
+  echo "** removing {$NremoveSub/$TB_BYTE} TB per nebulous subdir for total of {$Nbytes/$TB_BYTE} TB"
+end
+
+macro run.find
+  if ($0 != 7)
+    echo "USAGE: run.find (srchost) (srcvol) (tgthost) (tgtvol) (VERSION) (Fraction)"
+    echo "srchost, tgthost : e.g., ipp070"
+    echo "srcvol, tgtvol   : e.g., 0 (for /data/ipp070.0/nebulous)"
+    echo "Fraction : disk fraction target"
+    break
+  end
+
+  local srchost srcvol tgthost tgtvol Fraction
+
+  # NOTE: tgthost and tgtvol are not actually used in this step
+  $srchost = $1
+  $srcvol = $2
+  $tgthost = $3
+  $tgtvol = $4
+  $VERSION = $5
+  $Fraction = $6
+
+  # generate the full paths:
+  $source = /data/$srchost.$srcvol/nebulous
+  $target = /data/$tgthost.$tgtvol/rebalance/$srchost.$srcvol/nebulous
+
+  ## make sure target exists
+  mkdir $target
+
+  ## this script must be run on the same machine as the source
+  ## partitions
+
+  $result = `df -l -m $source | tail -n 1`
+  if ("$result" == "") 
+    echo "invalid source partition or partition is not local"
+    echo "source: $source"
+    break
+  end
+
+  list word -split $result
+
+  $Nbytes = 1024*1024*((1 - $Fraction)*$word:1 - $word:3)
+
+  echo "MB total : $word:1";                            # e.g. 100M
+  echo "MB used  : $word:2";                            # e.g.  95M
+  echo "MB avail : $word:3";                            # e.g.   5M
+
+  echo goal used : {$Fraction*$word:1};                 # e.g., 10M
+  echo to remove : {$word:2 - $Fraction*$word:1};       # e.g., 95M - 90M = 5M
+  echo alt calc  : {(1 - $Fraction)*$word:1 - $word:3}; # e.g., 10M - 5M = 5M
+
+  echo Nbytes to remove: $Nbytes
+  echo NGB to remove   : {$Nbytes/1024^3}
 
   if ($Nbytes < 0)
@@ -135,7 +214,4 @@
   $target = /data/$tgthost.$tgtvol/rebalance/$srchost.$srcvol/nebulous
 
-# list subdirs -glob $source/[0-9,a-f][0-9,a-f]
-# for i 0 $subdirs:n
-
   for i 0 $NSUBDIR
     sprintf srcdir "%s/%02x" $source $i
@@ -201,6 +277,8 @@
   end
 
-  if ($NtotalError) 
+  if ($NtotalError > 0) 
     echo "ERROR: $NtotalError errors with insert"
+    echo "use this command to see the errors:"
+    fprint "grep ERROR %s/??/rebalance/insert.%s.log" $source $VERSION
   else
     echo "SUCCESS: no insert errors detected"
@@ -267,5 +345,5 @@
 macro run.trash
   if ($0 != 6)
-    echo "USAGE: run.insert (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
+    echo "USAGE: run.trash (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
     echo "srchost, tgthost : e.g., ipp070"
     echo "srcvol, tgtvol   : e.g., 0 (for /data/ipp070.0/nebulous)"
@@ -286,7 +364,4 @@
     break
   end
-
-# list subdirs -glob $source/[0-9,a-f][0-9,a-f]
-# for i 0 $subdirs:n
 
   for i 0 $NSUBDIR
@@ -309,9 +384,93 @@
 end
 
+macro run.check.trash
+  if ($0 != 6)
+    echo "USAGE: run.check.trash (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
+    echo "srchost, tgthost : e.g., ipp070"
+    echo "srcvol, tgtvol   : e.g., 0 (for /data/ipp070.0/nebulous)"
+    break
+  end
+
+  local srchost tgthost srcvol tgtvol
+
+  $srchost = $1
+  $srcvol = $2
+
+  $VERSION = $5
+
+  $myhost = `hostname`
+  if ("$myhost" != "$srchost")
+    echo "please run on $srchost"
+    break
+  end
+
+  for i 0 $NSUBDIR
+    sprintf trashdir "/data/%s.%s/nebulous/%02x/trash" $srchost $srcvol $i
+    sprintf trashlog "/data/%s.%s/nebulous/%02x/rebalance/check.trash.%s.log" $srchost $srcvol $i $VERSION
+
+     echo ./neb_check_trash.pl --srchost $srchost --trashdir $trashdir > $trashlog
+    $EXEC ./neb_check_trash.pl --srchost $srchost --trashdir $trashdir > $trashlog
+  end
+
+  $NtotalError = 0
+  for i 0 $NSUBDIR
+    sprintf trashlog "/data/%s.%s/nebulous/%02x/rebalance/check.trash.%s.log" $srchost $srcvol $i $VERSION
+    # count total number of errors
+    $NgroupError = `grep ERROR $trashlog | wc -l`
+    $NtotalError += $NgroupError
+  end
+
+  if ($NtotalError > 0) 
+    echo "ERROR: $NtotalError errors with check.trash"
+  else
+    echo "SUCCESS: no check errors detected"
+  end
+end
+
+macro run.empty.trash
+  if ($0 != 6)
+    echo "USAGE: run.empty.trash (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
+    echo "srchost, tgthost : e.g., ipp070"
+    echo "srcvol, tgtvol   : e.g., 0 (for /data/ipp070.0/nebulous)"
+    break
+  end
+
+  local srchost tgthost srcvol tgtvol
+
+  $srchost = $1
+  $srcvol = $2
+
+  $VERSION = $5
+
+  $myhost = `hostname`
+  if ("$myhost" != "$srchost")
+    echo "please run on $srchost"
+    break
+  end
+
+  ### *** I need a test to confirm I have run check.trash *** ####
+
+  for i 0 $NSUBDIR
+    sprintf trashdir "/data/%s.%s/nebulous/%02x/trash" $srchost $srcvol $i
+
+    # list of files which have been moved & checked
+    sprintf movelist /data/%s.%s/nebulous/%02x/rebalance/movefiles.%s.txt $srchost $srcvol $i $VERSION
+
+    list files -x "cat $movelist"
+    for j 0 $files:n
+      basename $files:$j -var myfile
+
+      # remove the file from the trash directory
+      echo rm $trashdir/$myfile
+      exec rm $trashdir/$myfile
+    end
+  end
+end
+
 if ($SCRIPT) 
   if ($argv:n < 6) 
     echo "USAGE: rebalance.mana (MODE) (srchost) (srcvol) (tgthost) (tgtvol) (VERSION) [options]"
     echo ""
-    echo "MODE: find, rsync, insert, check, trash"
+    echo "MODE: find.test, find, rsync, insert, check, trash, check.trash, empty.trash"
     echo ""
     echo "srchost, tgthost : e.g., ipp070"
@@ -320,4 +479,15 @@
     echo "VERSION : free-form string (e.g., YYYYMMDD)"
     exit 2
+  end
+
+  #### find.test ####
+  if ("$argv:0" == "find.test")
+    if ($argv:n != 7)
+      echo "USAGE: rebalance.mana (find.test) (srchost) (srcvol) (tgthost) (tgtvol) (VERSION) (Fraction)"
+      echo "Fraction : disk fraction target"
+      break
+    end
+    run.find.test $argv:1 $argv:2 $argv:3 $argv:4 $argv:5 $argv:6
+    exit 0
   end
 
@@ -373,4 +543,24 @@
   end
 
+  #### check.trash ####
+  if ("$argv:0" == "check.trash")
+    if ($argv:n != 6)
+      echo "USAGE: rebalance.mana (check.trash) (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
+      break
+    end
+    run.check.trash $argv:1 $argv:2 $argv:3 $argv:4 $argv:5
+    exit 0
+  end
+
+  #### empty.trash ####
+  if ("$argv:0" == "empty.trash")
+    if ($argv:n != 6)
+      echo "USAGE: rebalance.mana (check.trash) (srchost) (srcvol) (tgthost) (tgtvol) (VERSION)"
+      break
+    end
+    run.empty.trash $argv:1 $argv:2 $argv:3 $argv:4 $argv:5
+    exit 0
+  end
+
   exit 0
 end
