# include "ppSimSequence.h"

int main (int argc, char **argv) {

    int argNum;
    bool status;
    unsigned int nFail;

    psLibInit(NULL);

    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);
    }

    psArray *sequences = NULL;
    if (item->type == PS_DATA_METADATA) {
	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);
    }

    for (int i = 0; i < sequences->n; i++) {

	// 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);
	}

	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;
	}

	psLogMsg ("ppSimSequence", PS_LOG_WARN, "SEQUENCE %d has an unknown type: %s", i, type);
	exit (1);
    }
    exit (0);
}
