Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 35558)
+++ trunk/ippTools/share/Makefile.am	(revision 35566)
@@ -449,4 +449,5 @@
 	diffphottool_data.sql \
 	laptool_definerun.sql \
+	laptool_definerunbyrelease.sql \
 	laptool_exposures.sql \
 	laptool_inactiveexp.sql \
Index: trunk/ippTools/share/laptool_definerunbyrelease.sql
===================================================================
--- trunk/ippTools/share/laptool_definerunbyrelease.sql	(revision 35566)
+++ trunk/ippTools/share/laptool_definerunbyrelease.sql	(revision 35566)
@@ -0,0 +1,23 @@
+select
+	rel.exp_id AS exp_id,
+	rel.chip_id AS chip_id,
+	false AS private,
+	false AS active,
+	false AS pairwise
+FROM
+	(select exp_id,chip_id FROM ippRelease
+	 JOIN relExp USING(rel_id)
+	 JOIN rawExp USING(exp_id)
+	 WHERE
+	 @RELWHERE@
+	) AS rel
+LEFT JOIN
+	(select MIN(lap_id) AS lap_id,
+	 exp_id,chip_id FROM lapRun
+	 JOIN lapExp USING(lap_id)
+	 WHERE
+	 @LAPWHERE@
+	 GROUP BY exp_id,chip_id
+	) AS lap
+USING(exp_id)
+WHERE lap_id IS NULL
Index: trunk/ippTools/src/laptool.c
===================================================================
--- trunk/ippTools/src/laptool.c	(revision 35558)
+++ trunk/ippTools/src/laptool.c	(revision 35566)
@@ -22,6 +22,8 @@
 // Run level
 static bool definerunMode(pxConfig *config);
+static bool definerunbyreleaseMode(pxConfig *config);
 static bool pendingrunMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
+
 // Exposure level
 static bool pendingexpMode(pxConfig *config);
@@ -64,4 +66,5 @@
     
     MODECASE(LAPTOOL_MODE_DEFINERUN,     definerunMode);
+    MODECASE(LAPTOOL_MODE_DEFINERUNBYRELEASE, definerunbyreleaseMode);
     MODECASE(LAPTOOL_MODE_PENDINGRUN,    pendingrunMode);
     MODECASE(LAPTOOL_MODE_UPDATERUN,     updaterunMode);
@@ -401,4 +404,173 @@
 }
 
+static bool definerunbyreleaseMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  PXOPT_LOOKUP_S64(seq_id,          config->args, "-seq_id",          true, false);
+  PXOPT_LOOKUP_STR(projection_cell, config->args, "-projection_cell", true, false);
+  PXOPT_LOOKUP_STR(tess_id,         config->args, "-tess_id",         true, false);
+  PXOPT_LOOKUP_STR(filter,          config->args, "-filter",          true, false);
+  PXOPT_LOOKUP_STR(label,           config->args, "-label",           false, false);
+  PXOPT_LOOKUP_STR(dist_group,      config->args, "-dist_group",      false, false);
+  PXOPT_LOOKUP_S64(rel_id,          config->args, "-rel_id",          false, false);
+  PXOPT_LOOKUP_STR(rel_name,        config->args, "-release_name",    false, false);
+  PXOPT_LOOKUP_BOOL(simple,         config->args, "-simple",          false);
+
+  // Validate config
+  if ((!rel_name)&&(!rel_id)) {
+    // Die here.
+  }
+  
+  lapRunRow *run = lapRunRowAlloc(0, // lap_id
+				  seq_id,
+				  tess_id,
+				  projection_cell,
+				  filter,
+				  "new", // state
+				  label,
+				  dist_group,
+				  NULL, // registered
+				  0,    // fault
+				  INT64_MAX,    // quick_sass_id
+				  INT64_MAX     // final_sass_id
+				  );
+  if (!run) {
+    psError(PS_ERR_UNKNOWN, false, "failed to alloc lapRun object");
+    return(true);
+  }
+
+  if (!psDBTransaction(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return(false);
+  }
+    
+  if (!lapRunInsertObject(config->dbh, run)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    psFree(run);
+    return(false);
+  }
+
+  if (!lapRunPrintObject(stdout, run, !simple)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+    psError(PS_ERR_UNKNOWN, false, "failed to print object");
+    psFree(run);
+    return(false);
+  }
+
+  psS64 lap_id = psDBLastInsertID(config->dbh);
+
+  psString query = pxDataGet("laptool_definerunbyrelease.sql");
+  if (!query) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+    psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
+    return(false);
+  }
+  
+  // Add constraints
+  psMetadata *relWhere = psMetadataAlloc();
+  PXOPT_COPY_STR(config->args, relWhere, "-filter",       "rawExp.filter", "==");
+  PXOPT_COPY_STR(config->args, relWhere, "-release_name", "ippRelease.release_name", "==");
+  PXOPT_COPY_S64(config->args, relWhere, "-rel_id",       "ippRelease.rel_id", "==");
+
+  psMetadata *lapWhere = psMetadataAlloc();
+  PXOPT_COPY_S64(config->args, lapWhere, "-seq_id",       "lapRun.seq_id", "==");
+  PXOPT_COPY_STR(config->args, lapWhere, "-filter",       "lapRun.filter", "==");
+
+  psString relWhereClause = psDBGenerateWhereConditionSQL(relWhere,NULL);
+  psString lapWhereClause = psDBGenerateWhereConditionSQL(lapWhere,NULL);
+
+  if (relWhereClause) {
+    psStringSubstitute(&query,relWhereClause,"@RELWHERE@");
+  }
+  if (lapWhereClause) {
+    psStringSubstitute(&query,lapWhereClause,"@LAPWHERE@");
+  }
+  psFree(relWhere);
+  psFree(lapWhere);
+
+  // Fetch exposures
+  if (!p_psDBRunQuery(config->dbh, query)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    psFree(query);
+    return(false);
+  }
+  psFree(query);
+
+  psArray *output = p_psDBFetchResult(config->dbh);
+  if (!output) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return(false);
+  }
+  if (!psArrayLength(output)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
+    psTrace("laptool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return(true);
+  }
+
+  // Insert the exposure data
+  for (long i = 0; i < output->n; i++) {
+    psMetadata *row = output->data[i]; // Row from select
+    // Add default values from this run:
+    psMetadataAddS64(row, PS_LIST_TAIL, "lap_id", 0, "", lap_id);
+    psMetadataAddS64(row, PS_LIST_TAIL, "pair_id", 0, "", INT64_MAX);
+    psMetadataAddStr(row, PS_LIST_TAIL, "data_state", 0, "", "new");
+    lapExpRow *lapExp = lapExpObjectFromMetadata(row);
+    lapExp->lap_id = lap_id;
+
+    if (!lapExpInsertObject(config->dbh,lapExp)) {
+      if (!lapExpPrintObject(stdout, lapExp, !simple)) {
+	if (!psDBRollback(config->dbh)) {
+	  psError(PS_ERR_UNKNOWN, false, "database error");
+	}
+	psError(PS_ERR_UNKNOWN, false, "failed to print object");
+	psFree(run);
+	return false;
+      }
+      
+      if (!psDBRollback(config->dbh)) {
+	psError(PS_ERR_UNKNOWN, false, "database error");
+      }
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      psFree(output);
+      psFree(lapExp);
+      return(false);
+    }
+  }
+
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  
+  psFree(output);
+  return(true);  
+}
+
+
+  
+  
+		   
 static bool pendingrunMode(pxConfig *config)
 {
Index: trunk/ippTools/src/laptool.h
===================================================================
--- trunk/ippTools/src/laptool.h	(revision 35558)
+++ trunk/ippTools/src/laptool.h	(revision 35566)
@@ -13,4 +13,5 @@
   LAPTOOL_MODE_LISTSEQUENCE,
   LAPTOOL_MODE_DEFINERUN,
+  LAPTOOL_MODE_DEFINERUNBYRELEASE,
   LAPTOOL_MODE_PENDINGRUN,
   LAPTOOL_MODE_UPDATERUN,
Index: trunk/ippTools/src/laptoolConfig.c
===================================================================
--- trunk/ippTools/src/laptoolConfig.c	(revision 35558)
+++ trunk/ippTools/src/laptoolConfig.c	(revision 35566)
@@ -62,4 +62,17 @@
   
   ADD_OPT(Bool,definerunArgs, "-simple",                      "use the simple output format", false);
+
+  // -definerunbyrelease
+  psMetadata *definerunbyreleaseArgs = psMetadataAlloc();
+  ADD_OPT(S64, definerunbyreleaseArgs, "-seq_id",             "define the LAP sequence for this run (required)", 0);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-projection_cell",    "define the projection cell for this run (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-tess_id",            "define the tessellation used (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-filter",             "define the filter used (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-label",              "define the label used", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-dist_group",         "define the distribution group for this data", NULL);
+  ADD_OPT(S64, definerunbyreleaseArgs, "-rel_id",             "define the release to copy", 0);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-release_name",       "define the release to copy", NULL);
+  
+  ADD_OPT(Bool, definerunbyreleaseArgs, "-simple",            "use the simple output format", false);
   
   // -pendingrun
@@ -204,4 +217,5 @@
   PXOPT_ADD_MODE("-listsequence",            "", LAPTOOL_MODE_LISTSEQUENCE,     listsequenceArgs);
   PXOPT_ADD_MODE("-definerun",               "", LAPTOOL_MODE_DEFINERUN,        definerunArgs);
+  PXOPT_ADD_MODE("-definerunbyrelease",      "", LAPTOOL_MODE_DEFINERUNBYRELEASE, definerunbyreleaseArgs);
   PXOPT_ADD_MODE("-pendingrun",              "", LAPTOOL_MODE_PENDINGRUN,       pendingrunArgs);
   PXOPT_ADD_MODE("-listrun",                 "", LAPTOOL_MODE_PENDINGRUN,       listrunArgs);
