/*
 * chiptool.c
 *
 * Copyright (C) 2006  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>

#include "pxtools.h"
#include "pxdata.h"
#include "chiptool.h"
#include "camtool.h"

static bool queueMode(pxConfig *config);
static bool pendingimfileMode(pxConfig *config);
static bool addprocessedimfileMode(pxConfig *config);
static bool processedimfileMode(pxConfig *config);
static bool updateprocessedimfileMode(pxConfig *config);
static bool blockMode(pxConfig *config);
static bool maskedMode(pxConfig *config);
static bool unblockMode(pxConfig *config);

static chipProcessedImfileRow *chipPendingToProcessedImfile(pxConfig *config, chipPendingImfileRow *imfile);
static chipProcessedExpRow *chipPendingToProcessedExp(pxConfig *config, chipPendingExpRow *pendingExp);
static bool chipProcessedCompleteExp(pxConfig *config);

# define MODECASE(caseName, func) \
    case caseName: \
    if (!func(config)) { \
        goto FAIL; \
    } \
    break;

int main(int argc, char **argv) {
    psLibInit(NULL);

    pxConfig *config = chiptoolConfig(NULL, argc, argv);
    if (!config) {
        psError(PXTOOLS_ERR_CONFIG, false, "failed to configure");
        goto FAIL;
    }

    switch (config->mode) {
        MODECASE(CHIPTOOL_MODE_QUEUE,                   queueMode);
        MODECASE(CHIPTOOL_MODE_PENDINGIMFILE,           pendingimfileMode);
        MODECASE(CHIPTOOL_MODE_ADDPROCESSEDIMFILE,      addprocessedimfileMode);
        MODECASE(CHIPTOOL_MODE_PROCESSEDIMFILE,         processedimfileMode);
        MODECASE(CHIPTOOL_MODE_UPDATEPROCESSEDIMFILE,updateprocessedimfileMode);
        MODECASE(CHIPTOOL_MODE_BLOCK,                   blockMode);
        MODECASE(CHIPTOOL_MODE_MASKED,                  maskedMode);
        MODECASE(CHIPTOOL_MODE_UNBLOCK,                 unblockMode);
        default:
            psAbort("invalid option (this should not happen)");
    }

    psFree(config);
    pmConfigDone();
    psLibFinalize();

    exit(EXIT_SUCCESS);

FAIL:
    psErrorStackPrint(stderr, "\n");
    int exit_status = pxerrorGetExitStatus();

    psFree(config);
    pmConfigDone();
    psLibFinalize();

    exit(exit_status);
}

#define ADDRENAMEPARAMSTR(from, to, oldname, newname) \
{ \
    bool status = false; \
    psString str = psMetadataLookupStr(&status, from, "-" oldname); \
    if (!status) { \
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -" oldname); \
        return false; \
    } \
    if (str) { \
        if (!psMetadataAddStr(to, PS_LIST_TAIL, newname, 0, "==", str)) { \
            psError(PS_ERR_UNKNOWN, false, "failed to add item " newname); \
            psFree(to); \
            return false; \
        } \
    } \
}

#define ADDPARAMSTR(from, to, name) ADDRENAMEPARAMSTR(from, to, name, name)

#define ADDPARAMF(from, to, type, name) \
{ \
    bool status = false; \
    ps##type var = psMetadataLookup##type(&status, from, "-" name); \
    if (!status) { \
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -" name); \
        return false; \
    } \
    if (!isnan(var)) { \
        if (!psMetadataAdd##type(to, PS_LIST_TAIL, #name, 0, "==", var)) { \
            psError(PS_ERR_UNKNOWN, false, "failed to add item " name); \
            psFree(to); \
            return false; \
        } \
    } \
}

static bool queueMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);

    psMetadata *where = psMetadataAlloc();
    ADDPARAMSTR(config->args, where, "exp_tag");
    ADDPARAMSTR(config->args, where, "exp_id");
    ADDRENAMEPARAMSTR(config->args, where, "inst", "camera");
    ADDPARAMSTR(config->args, where, "telescope");
    ADDPARAMSTR(config->args, where, "dateobs");
    ADDPARAMSTR(config->args, where, "exp_type");
    ADDPARAMSTR(config->args, where, "imfiles");
    ADDPARAMSTR(config->args, where, "workdir");
    ADDPARAMSTR(config->args, where, "filter");
    ADDPARAMF(config->args, where, F32, "airmass");
    ADDPARAMF(config->args, where, F32, "ra");
    ADDPARAMF(config->args, where, F32, "decl");
    ADDPARAMF(config->args, where, F32, "exp_time");
    ADDPARAMF(config->args, where, F32, "bg");
    ADDPARAMF(config->args, where, F32, "bg_stdev");
    ADDPARAMF(config->args, where, F32, "bg_mean_stdev");
    ADDPARAMF(config->args, where, F64, "alt");
    ADDPARAMF(config->args, where, F64, "az");
    ADDPARAMF(config->args, where, F32, "ccd_temp");
    ADDPARAMF(config->args, where, F64, "posang");
    ADDPARAMSTR(config->args, where, "object");

    if (where->list->n < 1) {
        psFree(where);
        where = NULL;
    }

    bool status = false;
    psString workdir = psMetadataLookupStr(&status, config->args, "-set_workdir");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_workdir");
        return false;
    }

    psString label = psMetadataLookupStr(&status, config->args, "-set_label");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_label");
        return false;
    }

    psString recipe = psMetadataLookupStr(&status, config->args, "-set_recipe");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_recipe");
        return false;
    }

    psString expgroup = psMetadataLookupStr(&status, config->args, "-set_expgroup");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_expgroup");
        return false;
    }

    psString dvodb = psMetadataLookupStr(&status, config->args, "-set_dvodb");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_dvodb");
        return false;
    }

    // find the exp_tag of all the exposures that we want to queue up.
    psString query = pxDataGet("chiptool_find_rawexp.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (where) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawExp");
        psFree(where);
        psStringAppend(&query, " AND %s", whereClause);
        psFree(whereClause);
    }

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("chiptool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    // start a transaction so we don't end up with an exp without any associted
    // imfiles
    if (!psDBTransaction(config->dbh)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(output);
        return false;
    }

    // loop over our list of exp_tags
    for (long i = 0; i < psArrayLength(output); i++) {
        psMetadata *md = output->data[i];

        psString exp_tag = psMetadataLookupStr(&status, md, "exp_tag");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for exp_tag");
            psFree(output);
            return false;
        }

        // queue the exp
        if (!chipQueueExpTag(config, exp_tag, workdir, label, recipe, expgroup, dvodb)) {
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            psError(PS_ERR_UNKNOWN, false,
                    "failed to trying to queue exp_tag: %s", exp_tag);
            psFree(output);
            return false;
        }
    }
    psFree(output);

    if (!psDBCommit(config->dbh)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}

static bool pendingimfileMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);

    bool status = false;
    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
        return false;
    }

    // XXX does this need to be constrained so that it won't return any results
    // if a match chipPendingExp hasn't been registered?
    psString query = pxDataGet("chiptool_pendingimfile.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (config->where) {
        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "chipPendingImfile");
        psStringAppend(&query, " AND %s", whereClause);
        psFree(whereClause);
    }

    // treat limit == 0 as "no limit"
    if (limit) {
        psString limitString = psDBGenerateLimitSQL(limit);
        psStringAppend(&query, " %s", limitString);
        psFree(limitString);
    }

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("chiptool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    bool simple = false;
    {
        bool status = false;
        simple = psMetadataLookupBool(&status, config->args, "-simple");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
            return false;
        }
    }

    if (!convertIdToStr(output)) {
        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
        psFree(output);
        return false;
    }

    // negative simple so the default is true
    if (!ippdbPrintMetadatas(stdout, output, "chipPendingImfile", !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print array");
        psFree(output);
        return false;
    }

    psFree(output);

    return true;
}


static bool addprocessedimfileMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    // select * from chipProcessedImfiles
    // where
    // exp_tag & class_id are not in chipPendingImfile

    // add the completed imfile to
    // the chipProcessedeImfile tables
    // remove corresponding entries from the
    // chipPendingImfile table
    // check to see if any chipPendingExps have no
    // associated chipPendingImfiles
    // if so move the chipPendingExp(s) to chipProcessedExp

    psString query = pxDataGet("chiptool_find_unprocessed_imfile.pl");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    {
        psMetadata *where = psMetadataAlloc();
        bool status = false;
        psString chip_id = psMetadataLookupStr(&status, config->args, "-chip_id");        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -chip_id");
            psFree(query);
            return false;
        }
        if (chip_id) {
            if (!psMetadataAddStr(where, PS_LIST_TAIL, "chip_id", 0, "==", chip_id)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item chip_id");
                psFree(where);
                psFree(query);
                return false;
            }
        }

        psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
            psFree(query);
            return false;
        }
        if (class_id) {
            if (!psMetadataAddStr(where, PS_LIST_TAIL, "class_id", 0, "==", class_id)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
                psFree(where);
                psFree(query);
                return false;
            }
        }

        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "chipPendingImfile");
        psFree(where);
        if (whereClause) {
            psStringAppend(&query, " AND %s", whereClause);
            psFree(whereClause);
        }
    }

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        // XXX check psError here
        psError(PS_ERR_UNKNOWN, false, "no chipPendingImfile rows found");
        psFree(output);
        return false;
    }

    // start a transaction so we don't end up with an incremented iteration
    // count but no detInputExps
    if (!psDBTransaction(config->dbh)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(output);
        return false;
    }

    // insert into chipProcessedImfile
    // remove chipPendingImfile entry
    for (long i = 0; i < psArrayLength(output); i++) {
        psMetadata *row = output->data[i];
        // convert metadata into a chipPendingImfile object
        chipPendingImfileRow *object = chipPendingImfileObjectFromMetadata(row);
        // convert chipPendingImfile object into a chipProcessedImfile object
        chipProcessedImfileRow *imfile = chipPendingToProcessedImfile(config, object);
        if (!imfile) {
            // rollback
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            psError(PS_ERR_UNKNOWN, false, "failed to convert chipPendingImfile to chipProcessedImfile");
            psFree(object);
            psFree(output);
            return false;
        }
        // insert chipProccessedImfile object into the database
        if (!chipProcessedImfileInsertObject(config->dbh, imfile)) {
            // rollback
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            psError(PS_ERR_UNKNOWN, false, "database error");
            psFree(imfile);
            psFree(object);
            psFree(output);
            return false;
        }
        psFree(imfile);
        // delete the chipPendingImfile object from the database
        if (!chipPendingImfileDeleteObject(config->dbh, object)) {
            // there must be atleast 1 Imfile to get this far
            // rollback
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            psError(PS_ERR_UNKNOWN, false, "database error");
            psFree(object);
            psFree(output);
            return false;
        }
        psFree(object);
    }

    psFree(output);

    // XXX I've decided to make the transaction cover the Exp migration as
    // well.  Otherwise, if the last imfile in an exp is moved and the exp
    // migration fails then the data base is left in a satuation where the exp
    // migration can't happen.

    if (!chipProcessedCompleteExp(config)) {
        // rollback
        if (!psDBRollback(config->dbh)) {
            psError(PS_ERR_UNKNOWN, false, "database error");
        }
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    // point of no return for chipPendingImfile -> chipProcessedImfile
    // point of no return for chipPendingExp -> chipProcessedExp
    if (!psDBCommit(config->dbh)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}


static bool processedimfileMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);

    bool status = false;
    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
        return false;
    }

    bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -faulted");
        return false;
    }

    // XXX does this need to be constrained so that it won't return any results
    // if a match chipPendingExp hasn't been registered?
    psString query = pxDataGet("chiptool_processedimfile.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (config->where) {
        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "chipProcessedImfile");
        psStringAppend(&query, " AND %s", whereClause);
        psFree(whereClause);
    }

    if (faulted) {
        // list only faulted rows
        psStringAppend(&query, " %s", "AND chipProcessedImfile.fault != 0");
    } else {
        // don't list faulted rows
        psStringAppend(&query, " %s", "AND chipProcessedImfile.fault = 0");
    }

    // treat limit == 0 as "no limit"
    if (limit) {
        psString limitString = psDBGenerateLimitSQL(limit);
        psStringAppend(&query, " %s", limitString);
        psFree(limitString);
    }

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("chiptool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    bool simple = false;
    {
        bool status = false;
        simple = psMetadataLookupBool(&status, config->args, "-simple");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
            return false;
        }
    }

    if (!convertIdToStr(output)) {
        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
        psFree(output);
        return false;
    }

    // negative simple so the default is true
    if (!ippdbPrintMetadatas(stdout, output, "chipProcessedImfile", !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print array");
        psFree(output);
        return false;
    }

    psFree(output);

    return true;
}


static bool updateprocessedimfileMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool status = false;
    psS16 code = psMetadataLookupS16(&status, config->args, "-code");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
        return false;
    }
    if (code == INT16_MAX) {
        psError(PS_ERR_UNKNOWN, true, "-code is required");
        return false;
    }

    if (!pxSetFaultCode(config->dbh, "chipProcessedImfile", config->where, code)) {
        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
        return false;
    }

    return true;
}


static bool blockMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool status = false;
    psString label = psMetadataLookupStr(&status, config->args, "-label");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -label");
        return false;
    }
    if (!label) {
        psError(PS_ERR_UNKNOWN, true, "-label is required");
        return false;
    }

    if (!chipMaskInsert(config->dbh, label)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}


static bool maskedMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    psString query = psStringCopy("SELECT * FROM chipMask");

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("chiptool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    bool simple = false;
    {
        bool status = false;
        simple = psMetadataLookupBool(&status, config->args, "-simple");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
            return false;
        }
    }

    if (!convertIdToStr(output)) {
        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
        psFree(output);
        return false;
    }

    // negative simple so the default is true
    if (!ippdbPrintMetadatas(stdout, output, "chipMask", !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print array");
        psFree(output);
        return false;
    }

    psFree(output);

    return true;
}


static bool unblockMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool status = false;
    psString label = psMetadataLookupStr(&status, config->args, "-label");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -label");
        return false;
    }
    if (!label) {
        psError(PS_ERR_UNKNOWN, true, "-label is required");
        return false;
    }

    char *query = "DELETE FROM chipMask WHERE label = '%s'";

    if (!p_psDBRunQuery(config->dbh, query, label)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }

    return true;
}


static bool chipProcessedCompleteExp(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    // look for completed chipPendingExp
    // migrate them to chipProccessedExp & camPendingExp
    psString query = pxDataGet("chiptool_completely_processed_exp.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (!p_psDBRunQuery(config->dbh, query)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("chiptool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    // insert into chipProcessedExp
    // insert into camPendingExp
    // remove chipPendingExp entry
    for (long i = 0; i < psArrayLength(output); i++) {
        psMetadata *row = output->data[i];

        // convert metadata into a chipPendingExp object
        chipPendingExpRow *pendingExp = chipPendingExpObjectFromMetadata(row);
        if (!pendingExp) {
            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipPendingExp");
            psFree(output);
            return false;
        }

        // convert chipPendingExp object into a chipProcesseExp object
        chipProcessedExpRow *processedExp = chipPendingToProcessedExp(config, pendingExp);
        if (!processedExp) {
            psError(PS_ERR_UNKNOWN, false, "failed to convert chipPendingExp to chipProcessedExp");
            psFree(pendingExp);
            psFree(output);
            return false;
        }

        // delete the chipPendingExp object from the database
        if (!chipPendingExpDeleteObject(config->dbh, pendingExp)) {
            // there must be atleast 1 Imfile to get this far
            psError(PS_ERR_UNKNOWN, false, "database error");
            psFree(processedExp);
            psFree(pendingExp);
            psFree(output);
            return false;
        }
        psFree(pendingExp);

        // insert chipProccessedExp object into the database
        if (!chipProcessedExpInsertObject(config->dbh, processedExp)) {
            psError(PS_ERR_UNKNOWN, false, "database error");
            psFree(processedExp);
            psFree(output);
            return false;
        }

        // camQueueChipID() can only be run after the chipProcessedExp entry
        // has been inserted.
        // queue the chip_id in the camPendingExp table
        if (!camQueueChipID(config,
                    processedExp->chip_id,
                    processedExp->workdir,
                    processedExp->label,
                    processedExp->recipe,
                    processedExp->expgroup,
                    processedExp->dvodb
        )) {
            // rollback
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            psError(PS_ERR_UNKNOWN, false, "failed to queue camPendingExp");
            psFree(processedExp);
            psFree(output);
            return false;
        }

        psFree(processedExp);
    }

    psFree(output);

    return true;
}


static chipProcessedImfileRow *chipPendingToProcessedImfile(pxConfig *config, chipPendingImfileRow *imfile)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);
    PS_ASSERT_PTR_NON_NULL(imfile, NULL);

    bool status = false;
    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
        return false;
    }

    psF32 bg = psMetadataLookupF32(&status, config->args, "-bg");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg");
        return false;
    }

    psF32 bg_stdev = psMetadataLookupF32(&status, config->args, "-bg_stdev");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_stdev");
        return false;
    }

    psF32 bg_mean_stdev = psMetadataLookupF32(&status, config->args, "-bg_mean_stdev");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_mean_stdev");
        return false;
    }

    psF32 bias = psMetadataLookupF32(&status, config->args, "-bias");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bias");
        return false;
    }

    psF32 bias_stdev = psMetadataLookupF32(&status, config->args, "-bias_stdev");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bias_stdev");
        return false;
    }

    psF32 fringe_0 = psMetadataLookupF32(&status, config->args, "-fringe_0");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fringe_0");
        return false;
    }

    psF32 fringe_1 = psMetadataLookupF32(&status, config->args, "-fringe_1");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fringe_1");
        return false;
    }

    psF32 fringe_2 = psMetadataLookupF32(&status, config->args, "-fringe_2");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fringe_2");
        return false;
    }

    psF32 sigma_ra = psMetadataLookupF32(&status, config->args, "-sigma_ra");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -sigma_ra");
        return false;
    }

    psF32 sigma_dec = psMetadataLookupF32(&status, config->args, "-sigma_dec");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -sigma_dec");
        return false;
    }

    psF32 ap_resid = psMetadataLookupF32(&status, config->args, "-ap_resid");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ap_resid");
        return false;
    }

    psF32 ap_resid_stdev = psMetadataLookupF32(&status, config->args, "-ap_resid_stdev");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ap_resid_stdev");
        return false;
    }

    psF32 fwhm = psMetadataLookupF32(&status, config->args, "-fwhm");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fwhm");
        return false;
    }

    psF32 fwhm_range = psMetadataLookupF32(&status, config->args, "-fwhm_range");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fwhm_range");
        return false;
    }

    psS32 n_stars = psMetadataLookupS32(&status, config->args, "-n_stars");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -n_stars");
        return false;
    }

    psS32 n_extended = psMetadataLookupS32(&status, config->args, "-n_extended");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -n_extended");
        return false;
    }

    psS32 n_cr = psMetadataLookupS32(&status, config->args, "-n_cr");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -n_cr");
        return false;
    }

    psS32 n_astrom = psMetadataLookupS32(&status, config->args, "-n_astrom");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -n_astrom");
        return false;
    }

    // optional
    psString path_base = psMetadataLookupStr(&status, config->args, "-path_base");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -path_base");
        return false;
    }

    // default values
    psS16 code = psMetadataLookupS16(&status, config->args, "-code");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
        return false;
    }

    return chipProcessedImfileRowAlloc(
        imfile->chip_id,
        imfile->class_id,
        uri,
        bg,
        bg_stdev,
        bg_mean_stdev,
        bias,
        bias_stdev,
        fringe_0,
        fringe_1,
        fringe_2,
        sigma_ra,
        sigma_dec,
        ap_resid,
        ap_resid_stdev,
        fwhm,
        fwhm_range,
        n_stars,
        n_extended,
        n_cr,
        n_astrom,
        path_base,
        code
        );
}


static chipProcessedExpRow *chipPendingToProcessedExp(pxConfig *config, chipPendingExpRow *pendingExp)
{
    PS_ASSERT_PTR_NON_NULL(pendingExp, NULL);

    return chipProcessedExpRowAlloc(
        pendingExp->chip_id,
        pendingExp->exp_tag,
        pendingExp->guide_id,
        pendingExp->workdir,
        pendingExp->label,
        pendingExp->recipe,
        pendingExp->expgroup,
        pendingExp->dvodb
    );
}
