Index: /trunk/ippScripts/Build.PL
===================================================================
--- /trunk/ippScripts/Build.PL	(revision 26029)
+++ /trunk/ippScripts/Build.PL	(revision 26030)
@@ -60,4 +60,5 @@
         scripts/magic_destreak_revert.pl
         scripts/magic_destreak_cleanup.pl
+        scripts/magic_destreak_defineruns.pl
         scripts/ippdb.pl
         scripts/ipp_cleanup.pl
@@ -87,5 +88,5 @@
         scripts/dist_advancerun.pl
         scripts/dist_make_fileset.pl
-        scripts/dist_queue_runs.pl
+        scripts/dist_defineruns.pl
         scripts/receive_source.pl
         scripts/receive_fileset.pl
Index: /trunk/ippScripts/scripts/dist_defineruns.pl
===================================================================
--- /trunk/ippScripts/scripts/dist_defineruns.pl	(revision 26030)
+++ /trunk/ippScripts/scripts/dist_defineruns.pl	(revision 26030)
@@ -0,0 +1,132 @@
+#!/usr/bin/env perl
+#
+# dist_queue_runs.pl : run disttool -definebyquery for the various stages or one stage
+#
+
+use Carp;
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Temp qw( tempfile );
+use File::Basename qw( basename );
+use Digest::MD5::File qw( file_md5_hex );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use PS::IPP::Config 1.01 qw( :standard );
+
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+
+# Look for programs we need
+my $missing_tools;
+my $disttool   = can_run('disttool') or (warn "Can't find disttool" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+# Parse the command-line arguments
+my ($stage, $stage_limit, $dist_root, $no_magic);
+my ($dbname, $save_temps, $verbose, $no_update, $logfile);
+my @labels;
+
+GetOptions(
+           'stage=s'        => \$stage,      # stage to queue
+           'label=s'        => \@labels,     # labels
+           'stage_limit=s'  => \$stage_limit,# maximum number of runs queued for each stage
+           'dist_root=s'    => \$dist_root,  # root of distribution work area
+           'no_magic'       => \$no_magic,   # queue runs without requiring magic (for testing only)
+           'dbname=s'       => \$dbname,     # Database name
+           'verbose'        => \$verbose,    # Print stuff?
+           'no-update'      => \$no_update,  # Don't update the database
+           'save-temps'     => \$save_temps, # Save temporary files?
+           'logfile=s'      => \$logfile,
+           ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --label",
+           -exitval => 3) unless
+    (scalar @labels > 0);
+
+$ipprc->redirect_output($logfile) if $logfile;
+
+if (!$dist_root) {
+    $dist_root = metadataLookupStr($ipprc->{_siteConfig}, "DISTRIBUTION_ROOT");
+    &my_die("failed to find DISTRIBUTION_ROOT in site configuration", $PS_EXIT_CONFIG_ERROR) if !$dist_root;
+}
+
+my ($day, $month, $year) = (gmtime)[3,4,5];
+my $datestr = sprintf "%04d%02d%02d", $year+1900, $month + 1, $day;
+
+my $workdir = "$dist_root/$datestr";
+
+print "workdir is $workdir\n";
+
+# if stage is not supplied as an argument, loop over all stages
+my @stages;
+if ($stage) {
+    push @stages, $stage;
+} else {
+    @stages = qw( raw chip camera fake warp diff stack);
+}
+
+foreach my $stage (@stages) {
+    foreach my $label (@labels) {
+        my $command = "$disttool -definebyquery -stage $stage -workdir $workdir -label $label";
+        $command .= " -no_magic" if $no_magic;
+        $command .= " -dry_run" if $no_update;
+        $command .= " -limit $stage_limit" if $stage_limit;
+        $command .= " -set_label $label";
+        $command .= " -dbname $dbname" if defined $dbname;
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+            &my_die("Unable to perform $command error_code: $error_code", $error_code);
+        }
+        # display the output from the command
+        print STDERR join "", @$stdout_buf if $verbose;
+    }
+}
+
+if (0) {
+# notyet
+# queue rcRuns for any distRuns that have completed and have interested destinations
+    my $command = "$disttool -queuercrun";
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform $command error_code: $error_code", $error_code);
+    }
+    # display the output from the command
+    print STDERR join "", @$stdout_buf if $verbose;
+}
+
+exit 0;
+
+sub my_die {
+    my $msg = shift;
+    my $fault = shift;
+
+    print STDERR "$msg\n";
+
+    exit $fault;
+}
+
+__END__
Index: unk/ippScripts/scripts/dist_queue_runs.pl
===================================================================
--- /trunk/ippScripts/scripts/dist_queue_runs.pl	(revision 26029)
+++ 	(revision )
@@ -1,129 +1,0 @@
-#!/usr/bin/env perl
-
-use Carp;
-use warnings;
-use strict;
-
-## report the program and machine
-use Sys::Hostname;
-my $host = hostname();
-print "\n\n";
-print "Starting script $0 on $host\n\n";
-
-use vars qw( $VERSION );
-$VERSION = '0.01';
-
-use IPC::Cmd 0.36 qw( can_run run );
-use File::Temp qw( tempfile );
-use File::Basename qw( basename );
-use Digest::MD5::File qw( file_md5_hex );
-use PS::IPP::Metadata::Config;
-use PS::IPP::Metadata::List qw( parse_md_list );
-
-use PS::IPP::Config 1.01 qw( :standard );
-
-my $ipprc = PS::IPP::Config->new(); # IPP configuration
-
-use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
-use Pod::Usage qw( pod2usage );
-
-
-# Look for programs we need
-my $missing_tools;
-my $disttool   = can_run('disttool') or (warn "Can't find disttool" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
-
-# Parse the command-line arguments
-my ($stage, $stage_limit, $dist_root, $no_magic);
-my ($dbname, $save_temps, $verbose, $no_update, $logfile);
-
-GetOptions(
-           'dist_root=s'    => \$dist_root,  # root of distribution work area
-           'stage=s'        => \$stage,      # stage to queue
-           'stage_limit=s'  => \$stage_limit,# maximum number of runs queued for each stage
-           'no_magic'       => \$no_magic,   # queue runs without requiring magic
-           'dbname=s'       => \$dbname,     # Database name
-           'verbose'        => \$verbose,    # Print stuff?
-           'no-update'      => \$no_update,  # Don't update the database
-           'save-temps'     => \$save_temps, # Save temporary files?
-           'logfile=s'      => \$logfile,
-           ) or pod2usage( 2 );
-
-pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-if (0) {
-pod2usage( -msg => "Required options: --stage",
-           -exitval => 3) unless
-    defined $stage;
-}
-
-$ipprc->redirect_output($logfile) if $logfile;
-
-if (!$dist_root) {
-    $dist_root = metadataLookupStr($ipprc->{_siteConfig}, "DISTRIBUTION_ROOT");
-    &my_die("failed to find DISTRIBUTION_ROOT in site configuration", $PS_EXIT_CONFIG_ERROR) if !$dist_root;
-}
-
-my ($day, $month, $year) = (localtime)[3,4,5];
-my $datestr = sprintf "%04d%02d%02d", $year+1900, $month + 1, $day;
-
-my $workdir = "$dist_root/$datestr";
-
-print "workdir is $workdir\n";
-
-# if stage is not supplied as an argument, loop over all stages
-my @stages;
-if ($stage) {
-    push @stages, $stage;
-} else {
-    @stages = qw( raw chip camera fake warp diff stack);
-}
-
-# XXX: how shall we deal with labels?
-my $label = 'proc';
-
-foreach my $stage (@stages) {
-    my $command = "$disttool -definebyquery -stage $stage -workdir $workdir";
-    $command .= " -no_magic" if $no_magic;
-    $command .= " -dry_run" if $no_update;
-    $command .= " -limit $stage_limit" if $stage_limit;
-    $command .= " -set_label $label";
-    $command .= " -dbname $dbname" if defined $dbname;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
-    unless ($success) {
-        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform $command error_code: $error_code", $error_code);
-    }
-    # display the output from the command
-    print STDERR join "", @$stdout_buf if $verbose;
-}
-
-# queue rcRuns for any distRuns that have completed and have interested destinations
-{
-    my $command = "$disttool -queuercrun";
-    $command .= " -dbname $dbname" if defined $dbname;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
-    unless ($success) {
-        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform $command error_code: $error_code", $error_code);
-    }
-    # display the output from the command
-    print STDERR join "", @$stdout_buf if $verbose;
-}
-
-exit 0;
-
-sub my_die {
-    my $msg = shift;
-    my $fault = shift;
-
-    print STDERR "$msg\n";
-
-    exit $fault;
-}
-
-__END__
Index: /trunk/ippScripts/scripts/magic_destreak_defineruns.pl
===================================================================
--- /trunk/ippScripts/scripts/magic_destreak_defineruns.pl	(revision 26030)
+++ /trunk/ippScripts/scripts/magic_destreak_defineruns.pl	(revision 26030)
@@ -0,0 +1,105 @@
+#!/usr/bin/env perl
+#
+# dist_defineruns.pl : run magicdstool -definebyquery for the various stages
+#
+
+use Carp;
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Temp qw( tempfile );
+use File::Basename qw( basename );
+use Digest::MD5::File qw( file_md5_hex );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use PS::IPP::Config 1.01 qw( :standard );
+
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+
+# Look for programs we need
+my $missing_tools;
+my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+# Parse the command-line arguments
+my ($stage, $stage_limit, $workdir);
+my ($dbname, $save_temps, $verbose, $no_update, $logfile);
+my @labels;
+
+GetOptions(
+           'stage=s'        => \$stage,      # stage to queue
+           'label=s'        => \@labels,     # labels
+           'stage_limit=s'  => \$stage_limit,# maximum number of runs queued for each stage
+           'workdir=s'      => \$workdir,    # output destination
+           'dbname=s'       => \$dbname,     # Database name
+           'verbose'        => \$verbose,    # Print stuff?
+           'no-update'      => \$no_update,  # Don't update the database
+           'save-temps'     => \$save_temps, # Save temporary files?
+           'logfile=s'      => \$logfile,
+           ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --label --workdir",
+           -exitval => 3) unless
+    (scalar @labels > 0) and
+    defined $workdir;
+
+$ipprc->redirect_output($logfile) if $logfile;
+
+# if stage is not supplied as an argument, loop over all stages
+my @stages;
+if ($stage) {
+    push @stages, $stage;
+} else {
+    # raw is omitted for now
+    @stages = qw( chip camera warp diff );
+}
+
+foreach my $stage (@stages) {
+    foreach my $label (@labels) {
+        my $command = "$magicdstool -definebyquery -stage $stage -workdir $workdir -label $label";
+        $command .= " -dry_run" if $no_update;
+        $command .= " -limit $stage_limit" if $stage_limit;
+        $command .= " -set_label $label";
+        $command .= " -dbname $dbname" if defined $dbname;
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+            &my_die("Unable to perform $command error_code: $error_code", $error_code);
+        }
+        # display the output from the command
+        print STDERR join "", @$stdout_buf if $verbose;
+    }
+}
+
+exit 0;
+
+sub my_die {
+    my $msg = shift;
+    my $fault = shift;
+
+    print STDERR "$msg\n";
+
+    exit $fault;
+}
+
+__END__
Index: /trunk/ippTasks/survey.pro
===================================================================
--- /trunk/ippTasks/survey.pro	(revision 26029)
+++ /trunk/ippTasks/survey.pro	(revision 26030)
@@ -12,7 +12,17 @@
   book create SURVEY_MAGIC
 end
+book getbook SURVEY_DESTREAK -var isBook
+if ("$isBook" == "NULL")
+  book create SURVEY_DESTREAK
+end
+book getbook SURVEY_DIST -var isBook
+if ("$isBook" == "NULL")
+  book create SURVEY_DIST
+end
 
 $SURVEY_DIFF_DB = 0
 $SURVEY_MAGIC_DB = 0
+$SURVEY_DESTREAK_DB = 0
+$SURVEY_DIST_DB = 0
 
 $SURVEY_EXEC = 120
@@ -74,4 +84,57 @@
   end
   book listbook SURVEY_MAGIC
+end
+
+# user functions to manipulate destreak labels
+macro survey.add.destreak
+  if ($0 != 3)
+    echo "USAGE: survey.add.destreak (label) (workdir base)"
+    break
+  end
+  book newpage SURVEY_DESTREAK $1
+  book setword SURVEY_DESTREAK $1 WORKDIR $2
+  book setword SURVEY_DESTREAK $1 STATE PENDING
+end
+
+macro survey.del.destreak
+  if ($0 != 2)
+    echo "USAGE: survey.del.destreak (label)"
+    break
+  end
+  book delpage SURVEY_DESTREAK $1
+end
+
+macro survey.show.destreak
+  if ($0 != 1)
+    echo "USAGE: survey.show.destreak"
+    break
+  end
+  book listbook SURVEY_DESTREAK
+end
+
+# user functions to manipulate dist labels
+macro survey.add.dist
+  if ($0 != 2)
+    echo "USAGE: survey.add.dist (label)"
+    break
+  end
+  book newpage SURVEY_DIST $1
+  book setword SURVEY_DIST $1 STATE PENDING
+end
+
+macro survey.del.dist
+  if ($0 != 2)
+    echo "USAGE: survey.del.dist (label)"
+    break
+  end
+  book delpage SURVEY_DIST $1
+end
+
+macro survey.show.dist
+  if ($0 != 1)
+    echo "USAGE: survey.show.dist"
+    break
+  end
+  book listbook SURVEY_DIST
 end
 
@@ -229,2 +292,147 @@
 end
 
+task survey.destreak
+  host local
+ 
+  periods      -poll $SURVEY_POLL
+  periods      -exec $SURVEY_EXEC
+  periods      -timeout $SURVEY_TIMEOUT
+  npending     1
+
+  active false
+
+  stdout $LOGDIR/survey.destreak.log
+  stderr $LOGDIR/survey.destreak.log
+
+  # generate destreak runs
+  task.exec
+    book npages SURVEY_DESTREAK -var N
+    if ($N == 0)
+#      echo "No labels for processing"
+      break
+    endif
+
+    book getpage SURVEY_DESTREAK 0 -var label -key STATE NEW
+    if ("$label" == "NULL")
+      # All labels have been done --- reset
+#      echo "Resetting labels"
+      for i 0 $N
+        book getpage SURVEY_DESTREAK $i -var label
+	book setword SURVEY_DESTREAK $label STATE NEW
+      end
+      book getpage SURVEY_DESTREAK 0 -var label -key STATE NEW
+
+      # Select different database
+      $SURVEY_DESTREAK_DB ++
+      if ($SURVEY_DESTREAK_DB >= $DB:n)
+        set $SURVEY_DESTREAK_DB = 0
+      end
+    end
+
+    book setword SURVEY_DESTREAK $label STATE DONE
+    book getword SURVEY_DESTREAK $label WORKDIR -var workdir
+  
+    $run = magic_destreak_defineruns.pl --label $label --workdir $workdir/$label/
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      $run = $run -dbname $DB:$SURVEY_MAGIC_DB
+      option $DB:$SURVEY_MAGIC_DB
+    end
+    
+#    echo $run
+    command $run
+  end
+
+  # success
+  task.exit    0
+#    echo "Success"
+  end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task survey.dist
+  host local
+ 
+  periods      -poll $SURVEY_POLL
+  periods      -exec $SURVEY_EXEC
+  periods      -timeout $SURVEY_TIMEOUT
+  npending     1
+
+  stdout $LOGDIR/survey.dist.log
+  stderr $LOGDIR/survey.dist.log
+
+  # generate distribution runs
+  task.exec
+    book npages SURVEY_DIST -var N
+    if ($N == 0)
+#      echo "No labels for processing"
+      break
+    endif
+
+    book getpage SURVEY_DIST 0 -var label -key STATE NEW
+    if ("$label" == "NULL")
+      # All labels have been done --- reset
+#      echo "Resetting labels"
+      for i 0 $N
+        book getpage SURVEY_DIST $i -var label
+	book setword SURVEY_DIST $label STATE NEW
+      end
+      book getpage SURVEY_DIST 0 -var label -key STATE NEW
+
+      # Select different database
+      $SURVEY_DIST_DB ++
+      if ($SURVEY_DIST_DB >= $DB:n)
+        set $SURVEY_DIST_DB = 0
+      end
+    end
+
+    book setword SURVEY_DIST $label STATE DONE
+  
+    # note workdir is set by the script based on site.config
+    $run = dist_defineruns.pl --label $label
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      $run = $run --dbname $DB:$SURVEY_DIST_DB
+      option $DB:$SURVEY_DIST_DB
+    end
+    
+#    echo $run
+    command $run
+  end
+
+  # success
+  task.exit    0
+#    echo "Success"
+  end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
