Index: trunk/ppSim/src/ppSimSequence.c
===================================================================
--- trunk/ppSim/src/ppSimSequence.c	(revision 16393)
+++ trunk/ppSim/src/ppSimSequence.c	(revision 16499)
@@ -1,72 +1,133 @@
-# include "ppSim.h"
+# include "ppSimSequence.h"
 
-int main(int argc, char *argv[])
-{
+int main (int argc, char **argv) {
+
+    int argNum;
+    bool status;
+    unsigned int nFail;
+
     psLibInit(NULL);
 
-    if (argc != 2) {
-      fprintf (stderr, "USAGE: ppSimSequence (seq.config)\n");
-      fprintf (stderr, "generates a set of simulated data defined by the sequence file\n");
-      exit (2);
+    char *dbname = NULL;
+    if ((argNum = psArgumentGet (argc, argv, "-dbname"))) { 
+        psArgumentRemove(argNum, &argc, argv);
+	dbname = psStringCopy (argv[argNum]);
+        psArgumentRemove(argNum, &argc, argv);
+    }
+
+    char *path = NULL;
+    if (psArgumentGet (argc, argv, "-path")) { 
+        psArgumentRemove(argNum, &argc, argv);
+	path = psStringCopy (argv[argNum]);
+        psArgumentRemove(argNum, &argc, argv);
+    }
+
+    char *workdir = NULL;
+    if (psArgumentGet (argc, argv, "-workdir")) { 
+        psArgumentRemove(argNum, &argc, argv);
+	workdir = psStringCopy (argv[argNum]);
+        psArgumentRemove(argNum, &argc, argv);
+    }
+
+    char *basename = NULL;
+    if (psArgumentGet (argc, argv, "-basename")) { 
+        psArgumentRemove(argNum, &argc, argv);
+	basename = psStringCopy (argv[argNum]);
+        psArgumentRemove(argNum, &argc, argv);
+    } else {
+	basename = psStringCopy ("simtest");
+    }
+
+    if (argc != 4) {
+	fprintf (stderr, "USAGE: ppSimSequence (sequence) (simulate) (inject)\n");
+	fprintf (stderr, "generates a set of simulated data defined by the sequence file\n");
+	fprintf (stderr, " (sequence) : a mdc-file describing the desired image sequences\n");
+	fprintf (stderr, " (simulate) : an output file with commands to generate the images\n");
+	fprintf (stderr, " (inject)   : an output file with commands to inject the images into the pipeline\n");
+	fprintf (stderr, "\n");
+	exit (2);
     }
 
     // load the sequence description
     psMetadata *config = psMetadataConfigRead (NULL, &nFail, argv[1], false);
+    if (!config) {
+	psLogMsg ("ppSimSequence", PS_LOG_WARN, "unable to read sequence description from %s", argv[1]);
+	exit (1);
+    }
     
+    FILE *simfile = fopen (argv[2], "w");
+    if (!simfile) {
+	psLogMsg ("ppSimSequence", PS_LOG_WARN, "unable to open %s for output", argv[2]);
+	exit (1);
+    }
+	
+    FILE *inject = fopen (argv[3], "w");
+    if (!inject) {
+	psLogMsg ("ppSimSequence", PS_LOG_WARN, "unable to open %s for output", argv[3]);
+	exit (1);
+    }
+
+    // build the base injectCommand string
+    psString injectCommand = psStringCopy ("ipp_inject_fileset.pl --telescope SIMTEST");
+    if (dbname)  psStringAppend (&injectCommand, " --dbname %s",  dbname);
+    if (workdir) psStringAppend (&injectCommand, " --workdir %s", workdir);
+
+    unsigned long seed = psMetadataLookupS32 (&status, config, "RND_SEED");
+    if (!status) seed = 0;
+    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS, seed);
+
     psMetadataItem *item = psMetadataLookup (config, "SEQUENCE");
     if (item == NULL) {
-      psLogMsg ("ppSimSequence", PS_LOG_WARN, "missing SEQUENCE description");
-      exit (1);
+	psLogMsg ("ppSimSequence", PS_LOG_WARN, "missing SEQUENCE description");
+	exit (1);
     }
 
+    psArray *sequences = NULL;
     if (item->type == PS_DATA_METADATA) {
-      sequences = psArrayAlloc(1);
-      sequences->data[0] = psMemIncrRefCounter (item->data.V);
+	sequences = psArrayAlloc(1);
+	sequences->data[0] = psMemIncrRefCounter (item->data.V);
     } else {
-      if (item->type != PS_DATA_METADATA_MULTI)  {
-	psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE is not MULTI or METADATA");
-	exit (1);
-      }
-      sequences = psListToArray (item->data.list);
+	if (item->type != PS_DATA_METADATA_MULTI)  {
+	    psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE is not MULTI or METADATA");
+	    exit (1);
+	}
+	sequences = psListToArray (item->data.list);
     }
 
     for (int i = 0; i < sequences->n; i++) {
 
-      // a OBJECT sequence is described by a center position and a set of offsets at each
-      // offset, we perform a set of dithers for each pointing, we have a set of filters
-      // and exposure times
+	// XXX this is not obvious: the entry on the list is a metadata item 
+	// containing the psMetadata :: why is this not just a metadata?
+	psMetadataItem *item = sequences->data[i];
+	psMetadata *sequence = item->data.V;
+	// double check item type?
+      
+	// determine the sequence type
+	char *type = psMetadataLookupStr (&status, sequence, "OBSTYPE");
+	if (!status) {
+	    psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE %d is missing a type", i);
+	    exit (1);
+	}
 
-      // a non-OBJECT sequence consists of a number of a given type
+	if (!strcasecmp (type, "BIAS")) {
+	    ppSimSequenceBias (simfile, inject, sequence, i, rng, path, basename, injectCommand);
+	    continue;
+	}
+	if (!strcasecmp (type, "DARK")) {
+	    ppSimSequenceDark (simfile, inject, sequence, i, rng, path, basename, injectCommand);
+	    continue;
+	}
+	if (!strcasecmp (type, "FLAT")) {
+	    ppSimSequenceFlat (simfile, inject, sequence, i, rng, path, basename, injectCommand);
+	    continue;
+	}
+	if (!strcasecmp (type, "OBJECT")) {
+	    ppSimSequenceObject (simfile, inject, sequence, i, rng, path, basename, injectCommand);
+	    continue;
+	}
 
-      // create files with ppSim:
-      // ppSim -camera CAMERA -type TYPE filename
-      
-      // inject --camera 
-
-      psMetadata *sequence = sequences->data[i];
-      
-      // determine the sequence type
-      char *type = psMetadataLookupStr (&status, sequence, "TYPE");
-      if (!status) {
-	psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE %d is missing a type", i);
+	psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE %d has an unknown type: %s", i, type);
 	exit (1);
-      }
-
-      if (!strcasecmp (type, "BIAS")) {
-	ppSimSequenceBias (sequence);
-	continue;
-      }
-      if (!strcasecmp (type, "DARK")) {
-	ppSimSequenceDark (sequence);
-	continue;
-      }
-      if (!strcasecmp (type, "FLAT")) {
-	ppSimSequenceFlat (sequence);
-	continue;
-      }
-      if (!strcasecmp (type, "OBJECT")) {
-	ppSimSequenceObject (sequence);
-	continue;
-      }
     }
+    exit (0);
 }
