Index: /trunk/tools/runcameraexp.pl
===================================================================
--- /trunk/tools/runcameraexp.pl	(revision 29158)
+++ /trunk/tools/runcameraexp.pl	(revision 29158)
@@ -0,0 +1,110 @@
+#!/bin/env perl
+
+use strict;
+use warnings;
+use DBI;
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Config 1.01 qw( :standard );
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+use File::Basename;
+use IO::File;
+
+
+my $dbname = "gpc1";
+my ($cam_id, $update, $redirect, $pretend, $no_verbose);
+
+GetOptions(
+    'cam_id=i'          => \$cam_id,
+    'pretend'           => \$pretend,
+    'redirect-output'   => \$redirect,
+    'update'            => \$update,
+    'dbname=s'          => \$dbname,
+    'no-verbose'        => \$no_verbose, 
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "-cam_id is required", -exitval => 2 ) if !$cam_id;
+
+my $ipprc =  PS::IPP::Config->new();
+my $dbh = getDBHandle();
+
+my $query = "SELECT rawExp.camera, rawExp.exp_tag, camProcessedExp.path_base, camRun.dvodb, camRun.reduction, camRun.state FROM camRun JOIN camProcessedExp USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) WHERE cam_id = $cam_id";
+
+my $stmt = $dbh->prepare($query);
+$stmt->execute();
+my $results = $stmt->fetchrow_hashref();
+die "query returned no results\n" if !$results;
+my $camera = $results->{camera};
+my $exp_tag = $results->{exp_tag};
+my $path_base = $results->{path_base};
+my $dvodb = $results->{dvodb};
+my $reduction = $results->{reduction};
+my $state = $results->{state};
+
+die "path_base not found\n" if !$path_base;
+die "exp_tag not found\n" if !$exp_tag;
+die "dvodb not found\n" if !$dvodb;
+die "camera not found\n" if !$camera;
+die "state not found\n" if !$state;
+die "reduction not found\n" if !$reduction;
+
+my  $run_state;
+if ($state eq 'full' or $state eq 'new') {
+    $run_state = 'new';
+} elsif ($state eq 'update') {
+    die "unexpected warpRun.state found: $state\n";
+}
+
+my $command = "camera_exp.pl --exp_tag $exp_tag --cam_id $cam_id --camera $camera --outroot $path_base --run-state $run_state ";
+
+$command .= " --reduction $reduction" if $reduction and ($reduction ne "NULL");
+$command .= " --dvodb $dvodb" if $dvodb and ($dvodb ne "NULL");
+$command .= " --redirect-output" if $redirect;
+$command .= " --no-update" unless $update;
+$command .= " --verbose" unless $no_verbose;
+$command .= " --dbname $dbname" if $dbname;
+
+
+if ($update) {
+    my $revert_command = "camtool -revertprocessedexp -cam_id $cam_id -dbname $dbname";
+    if ($pretend) {
+        print "skipping $revert_command\n";
+    } else {
+        my $rc = system $revert_command;
+        if ($rc != 0) {
+            my $status = $rc >> 8;
+            print STDERR "$revert_command failed: $rc $status\n";
+            exit $status;
+        }
+    }
+}
+print "command to process this camRun\n";
+print "$command\n";
+
+exit 0 if $pretend;
+
+exit system $command;
+
+
+sub getDBHandle {
+    my $dbserver = metadataLookupStr($ipprc->{_siteConfig}, "DBSERVER");
+    my $dbuser = metadataLookupStr($ipprc->{_siteConfig}, "DBUSER");
+    my $dbpassword = metadataLookupStr($ipprc->{_siteConfig}, "DBPASSWORD");
+    if (!$dbname) {
+        $dbname = metadataLookupStr($ipprc->{_siteConfig}, "DBNAME");
+    }
+
+    die "database configuration set up" unless defined($dbserver) and defined($dbuser)
+        and defined($dbpassword) and defined($dbname);
+
+    my $dsn = "DBI:mysql:host=$dbserver;database=$dbname";
+
+    my $dbh = DBI->connect($dsn, $dbuser, $dbpassword) 
+        or die "Cannot connect to database.\n";
+
+    return $dbh;
+}
+
