Index: trunk/tools/eam/rawfix.20230221/src/advance.rawfix.sh
===================================================================
--- trunk/tools/eam/rawfix.20230221/src/advance.rawfix.sh	(revision 42430)
+++ trunk/tools/eam/rawfix.20230221/src/advance.rawfix.sh	(revision 42440)
@@ -2,5 +2,5 @@
 
 if ($#argv < 1) then
-  echo "USAGE: advance.rawfix.sh all"
+  echo "USAGE: advance.rawfix.sh all (Nmax)"
   echo "USAGE: advance.rawfix.sh night (YYYYMMDD)"
   exit 2
@@ -9,13 +9,41 @@
 set mode = $1
 
+## include a max number of active programs
 if ($mode == "all") then
+  if ($#argv != 2) then
+    echo "USAGE: advance.rawfix.sh all (Nmax)"
+    exit 2
+  endif
+
+  set nMax = $2
+  set nActive  = 0
+  set nAdvance = 0
   set nights = `./check.rawfix.sh all | awk '(NR > 1){print $1}'`
   foreach f ($nights)
-    echo -n "$f "
     ./advance.rawfix.sh night $f 
+    set myResult = $status
+    if ($myResult == 1) continue
+    if ($myResult == 2) continue
+    if ($myResult == 3) continue
+    if ($myResult == 4) then
+      @ nActive ++
+    endif
+    if ($myResult == 5) then
+      @ nActive ++
+      @ nAdvance ++
+    endif
+    if ($nActive >= $nMax) then
+      echo "advance $nAdvance nights, total of $nActive active nights"
+      exit 0
+    endif
   end
+  echo "advance $nAdvance nights, total of $nActive active nights"
   exit 0
 endif
 
+# advance STAGE0.done to STAGE1.new
+# if already in STAGEx.new, return 4
+# if setting to STAGEx.new, return 5
+# otherwise, return 3
 if ($mode == "night") then
   if ($#argv != 2) then
@@ -29,4 +57,5 @@
 
   set tgtstate = "NONE"
+  if ($mystate == "new")             set tgtstate = "ckchip.new"
   if ($mystate == "ckchip.done")     set tgtstate = "fixchip.new"
   if ($mystate == "fixchip.done")    set tgtstate = "md5sum.new"    
@@ -37,14 +66,29 @@
   if ($mystate == "md5chk_s2.done")  set tgtstate = "fixchip_s3.new"   
   if ($mystate == "fixchip_s3.done") set tgtstate = "ckchip_s3.new"    
+  if ($mystate == "ckchip_s3.done")  set tgtstate = "DONE" 
 
-  if ($tgtstate == "NONE") then
-    echo "nothing to advance"
-    exit 0
-  endif
+  if ($mystate == "ckchip.new")      set tgtstate = "ACTIVE"
+  if ($mystate == "fixchip.new")     set tgtstate = "ACTIVE"
+  if ($mystate == "md5sum.new")      set tgtstate = "ACTIVE"
+  if ($mystate == "md5chk.new")      set tgtstate = "ACTIVE"
+  if ($mystate == "fixchip_s2.new")  set tgtstate = "ACTIVE"
+  if ($mystate == "md5sum_s2.new")   set tgtstate = "ACTIVE"
+  if ($mystate == "md5chk_s2.new")   set tgtstate = "ACTIVE"
+  if ($mystate == "fixchip_s3.new")  set tgtstate = "ACTIVE"
+  if ($mystate == "ckchip_s3.new")   set tgtstate = "ACTIVE"
 
+  echo "$night : $mystate : $tgtstate"
+
+  # do not set these two states
+  if ($tgtstate == "NONE") exit 3
+  if ($tgtstate == "ACTIVE") exit 4
+
+  # only set these states
   ./update.rawfix.sh $night $tgtstate
-  exit 0
+
+  if ($tgtstate == "DONE") exit 1
+  exit 5
 endif
 
-echo "ERROR: unknown mode: $mode
+echo "ERROR: unknown mode: $mode"
 exit 2
