/*
 * disttool.c
 *
 * Copyright (C) 2008
 *
 * 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 <stdlib.h>
#include <inttypes.h>

#include "pxtools.h"
#include "pxdata.h"
#include "disttool.h"

static bool definebyqueryMode(pxConfig *config);
static bool definerunMode(pxConfig *config);
static bool updaterunMode(pxConfig *config);
static bool revertrunMode(pxConfig *config);
static bool pendingcomponentMode(pxConfig *config);
static bool addprocessedcomponentMode(pxConfig *config);
static bool processedcomponentMode(pxConfig *config);
static bool toadvanceMode(pxConfig *config);
static bool pendingfilesetMode(pxConfig *config);
static bool addfilesetMode(pxConfig *config);
static bool revertfilesetMode(pxConfig *config);
static bool queuercrunMode(pxConfig *config);
static bool updatercrunMode(pxConfig *config);
static bool revertrcrunMode(pxConfig *config);
static bool pendingdestMode(pxConfig *config);

static bool definetargetMode(pxConfig *config);
static bool updatetargetMode(pxConfig *config);
static bool listtargetMode(pxConfig *config);

static bool definedsproductMode(pxConfig *config);
static bool updatedsproductMode(pxConfig *config);

static bool definedestinationMode(pxConfig *config);
static bool updatedestinationMode(pxConfig *config);

static bool defineinterestMode(pxConfig *config);
static bool updateinterestMode(pxConfig *config);

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


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

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

    switch (config->mode) {
        MODECASE(DISTTOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
        MODECASE(DISTTOOL_MODE_DEFINERUN, definerunMode);
        MODECASE(DISTTOOL_MODE_UPDATERUN, updaterunMode);
        MODECASE(DISTTOOL_MODE_REVERTRUN, revertrunMode);
        MODECASE(DISTTOOL_MODE_PENDINGCOMPONENT, pendingcomponentMode);
        MODECASE(DISTTOOL_MODE_ADDPROCESSEDCOMPONENT, addprocessedcomponentMode);
        MODECASE(DISTTOOL_MODE_PROCESSEDCOMPONENT, processedcomponentMode);
        MODECASE(DISTTOOL_MODE_TOADVANCE, toadvanceMode);
        MODECASE(DISTTOOL_MODE_PENDINGFILESET, pendingfilesetMode);
        MODECASE(DISTTOOL_MODE_ADDFILESET, addfilesetMode);
        MODECASE(DISTTOOL_MODE_REVERTFILESET, revertfilesetMode);
        MODECASE(DISTTOOL_MODE_QUEUERCRUN, queuercrunMode);
        MODECASE(DISTTOOL_MODE_UPDATERCRUN, updatercrunMode);
        MODECASE(DISTTOOL_MODE_REVERTRCRUN, revertrcrunMode);
        MODECASE(DISTTOOL_MODE_PENDINGDEST, pendingdestMode);
        MODECASE(DISTTOOL_MODE_DEFINETARGET, definetargetMode);
        MODECASE(DISTTOOL_MODE_UPDATETARGET, updatetargetMode);
        MODECASE(DISTTOOL_MODE_LISTTARGET, listtargetMode);
        MODECASE(DISTTOOL_MODE_DEFINEDSPRODUCT, definedsproductMode);
        MODECASE(DISTTOOL_MODE_UPDATEDSPRODUCT, updatedsproductMode);
        MODECASE(DISTTOOL_MODE_DEFINEDESTINATION, definedestinationMode);
        MODECASE(DISTTOOL_MODE_UPDATEDESTINATION, updatedestinationMode);
        MODECASE(DISTTOOL_MODE_DEFINEINTEREST, defineinterestMode);
        MODECASE(DISTTOOL_MODE_UPDATEINTEREST, updateinterestMode);
        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);
}

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

    // required
    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
    PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id",  true, false);
    PXOPT_LOOKUP_STR(outroot, config->args, "-outroot", true, false);
    PXOPT_LOOKUP_S64(target_id, config->args, "-target_id",  true, false);

    // optional
    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);

    // TODO: check that stage has an expected value
    // XXX: all of the following concerns will be managed properly by definebyquery

    // TODO: should we check that stage_id actually exists for stage
    // in magicdstool we queue off of a magic_id so the stage_id, exp_id, and cam_id get looked up
    // when the run is queued

    if (!distRunInsert(config->dbh,
            0,          // dist_id
            target_id,
            stage,
            stage_id,
            set_label,
            outroot,
            clean,
            no_magic,
            "new",
            NULL,       // time_stamp
            0           // fault
            )) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}

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

    // required
    PXOPT_LOOKUP_STR(stage, config->args,     "-stage", true, false);
    PXOPT_LOOKUP_STR(workdir, config->args,   "-workdir", true, false);

    // optional
    PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id",  false, false);
    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    
    PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);

    // select arguments
    psMetadata *where = psMetadataAlloc();

    PXOPT_COPY_S64(config->args, where, "-target_id", "distTarget.target_id",    "==");
    PXOPT_COPY_S64(config->args, where, "-exp_id",    "rawExp.exp_id",           "==");
    PXOPT_COPY_S64(config->args, where, "-chip_id",   "chipRun.chip_id",         "==");

    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);

    psString query = NULL;
    psString magicRunType = NULL;
    psString runJoinStr = NULL;
    if (!strcmp(stage, "raw")) {
        magicRunType = "rawExp";
        runJoinStr = "rawExp.exp_id";
        query = pxDataGet("disttool_definebyquery_raw.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        // for raw stage we select by chipRun.label because raw labels typically aren't set
        if (label) {
            psStringAppend(&query, " AND (chipRun.label = '%s')", label);
        }
    } else if (!strcmp(stage, "chip")) {
        magicRunType = "chipRun";
        runJoinStr = "chipRun.chip_id";
        query = pxDataGet("disttool_definebyquery_chip.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (chipRun.label = '%s')", label);
        }
    } else if (!strcmp(stage, "camera")) {
        magicRunType = "chipRun";    // This is used below to set the magicked business
        query = pxDataGet("disttool_definebyquery_camera.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (camRun.label = '%s')", label);
        }
    } else if (!strcmp(stage, "fake")) {
        magicRunType = "fakeRun";
        query = pxDataGet("disttool_definebyquery_fake.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (fakeRun.label = '%s')", label);
        }
        // fake stage doesn't require magic
        no_magic = true;
    } else if (!strcmp(stage, "warp")) {
        magicRunType = "warpRun";
        runJoinStr = "warpRun.warp_id";
        query = pxDataGet("disttool_definebyquery_warp.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (warpRun.label = '%s')", label);
        }

    } else if (!strcmp(stage, "diff")) {
        magicRunType = "diffRun";
        runJoinStr = "diffRun.diff_id";
        query = pxDataGet("disttool_definebyquery_diff.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (diffRun.label = '%s')", label);
        }

    } else if (!strcmp(stage, "stack")) {
        magicRunType = "stackRun";
        query = pxDataGet("disttool_definebyquery_stack.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            psFree(where);
            return false;
        }

        if (label) {
            psStringAppend(&query, " AND (stackRun.label = '%s')", label);
        }
        // stack stage doesn't require magic (perhaps let the script do this?
        no_magic = true;
    } else {
        psError(PS_ERR_UNKNOWN, true, "unknown value for stage: %s", stage);
        psFree(where);
        return false;
    }

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

    psString joinHook = NULL;

    if (!no_magic) {
        psStringAppend(&query, " AND (distTarget.clean OR %s.magicked)", magicRunType);

        // is selecting by magic_ds_id really interesting?
        if (magic_ds_id) {
            if (strcmp(stage, "camera")) {
                // stage other than camera
                if (!runJoinStr) {
                    psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
                    psFree(query);
                    return false;
                }
                psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
                                              " AND magicDSRun.stage_id = %s", runJoinStr);
            } else {
                // camera masks are magicked when the chipRun is magicked
                // XXX: This is confusing. Is it dangerous?
                // Maybe I should add a magicked bit to camRun. Note this isn't
                psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"
                                              " AND magicDSRun.stage_id = chipRun.chip_id");
            }
            psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id);
        }
    }

    if (limit) {
        psString limitString = psDBGenerateLimitSQL(limit);
        psStringAppend(&query, " %s", limitString);
        psFree(limitString);
    }

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

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


    if (dry_run) {
        if (!ippdbPrintMetadatas(stdout, output, "newdistRuns", true)) {
            psError(PS_ERR_UNKNOWN, false, "failed to print array");
            psFree(output);
            return false;
        }
        psFree(output);
        return true;
    }

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

    psArray *list = psArrayAllocEmpty(limit);
    for (long i=0; i < psArrayLength(output); i++) {
        psMetadata *md = output->data[i];
        psString stage = psMetadataLookupStr(NULL, md, "stage");
        psString run_tag = psMetadataLookupStr(NULL, md, "run_tag");
        psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
        psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
        psString target_label = psMetadataLookupStr(NULL, md, "label");
        bool clean = psMetadataLookupBool(NULL, md, "clean");

        psString outroot = NULL;
        psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage); 

        psString new_label;
        if (set_label != NULL) {
            new_label = set_label;
        } else {
            new_label = target_label;
        }
        distRunRow *row = distRunRowAlloc(
                0,      // dist_id
                target_id,
                stage,
                stage_id,
                new_label,
                outroot,
                clean,
                no_magic,
                "new",
                NULL,   // time_stamp
                0       // fault
                );

        if (!row) {
            psError(PS_ERR_UNKNOWN, false, "failed to allocate distRunRow");
            psFree(outroot);
            psFree(output);
            return false;
        }
        if (!distRunInsertObject(config->dbh, row)) {
            psError(PS_ERR_UNKNOWN, false, "database error");
            psFree(outroot);
            psFree(output);
            return false;
        }
        psFree(outroot);
        psS64 dist_id = psDBLastInsertID(config->dbh);
        row->dist_id = dist_id;
        psArrayAdd(list, list->n, row);
        psFree(row);
    }

    if (!psDBCommit(config->dbh)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }
    if (!distRunPrintObjects(stdout, list, !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print object");
        psFree(list);
        return false;
    }

    psFree(list);
    psFree(output);

    return true;
}

static bool updaterunMode(pxConfig *config)
{
    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");

    if (!psListLength(where->list)) {
        psFree(where);
        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
        return false;
    }

    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);

    if ((!state) && (!label) && (!fault)) {
        psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state or -set_label) are required");
        psFree(where);
        return false;
    }

    psString query = psStringCopy("UPDATE distRun SET time_stamp = UTC_TIMESTAMP()");

    if (state) {
        psStringAppend(&query, " , state = '%s'", state);
    }

    if (label) {
        psStringAppend(&query, " , label = '%s'", label);
    }

    if (fault) {
        psStringAppend(&query, " , fault = %d", fault);
    }

    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    psStringAppend(&query, " WHERE %s", whereClause);
    psFree(whereClause);
    psFree(where);

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

    return true;
}

static bool revertrunMode(pxConfig *config)
{
    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");

    // we need to disambiguate fault so make a copy of the where list before adding fault
    psMetadata *whereComponent = psMetadataCopy(NULL, where);
    PXOPT_COPY_S16(config->args, where,  "-fault", "distRun.fault", "==");
    PXOPT_COPY_S16(config->args, whereComponent, "-fault", "distComponent.fault", "==");

    // It might be useful to be able to query by the parameters of the underlying runs

    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
        psFree(where);
        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
        return false;
    }

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

    // Update state to 'new'
    int numUpdated;                     // Number updated
    {
        psString query = pxDataGet("disttool_revertrun_update.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            return false;
        }

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

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

        numUpdated = psDBAffectedRows(config->dbh);
    }

    psLogMsg("disttool", PS_LOG_INFO, "Updated %d dist runs", numUpdated);

    // Delete product
    int numDeleted;                     // Number deleted
    {
        psString query = pxDataGet("disttool_revertrun_delete.sql");
        if (!query) {
            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
            if (!psDBRollback(config->dbh)) {
                psError(PS_ERR_UNKNOWN, false, "database error");
            }
            return false;
        }

        if (psListLength(whereComponent->list)) {
            psString whereClause = psDBGenerateWhereConditionSQL(whereComponent, NULL);
            psStringAppend(&query, " AND %s", whereClause);
            psFree(whereClause);
        }

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

        numDeleted = psDBAffectedRows(config->dbh);
    }

    psLogMsg("disttool", PS_LOG_INFO, "Deleted %d distComponents", numDeleted);

    psFree(where);
    psFree(whereComponent);

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

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    pxAddLabelSearchArgs (config, where, "-label", "label", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    // look for "inputs" that need to processed
    psString query = pxDataGet("disttool_pendingcomponent.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    }
    psFree(where);

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

    // the query has where hooks for each stage.
    // right now we aren't using them.
    // XXX: I think that I want to change the query from a union of selects on the various
    // stages to separate queries. As it is pending data at the later stages of the pipline
    // will get blocked by pending earlier stages
    psString    raw_where = "";
    psString    raw_clean_where = "";
    psString    chip_where = "";
    psString    camera_where = "";
    psString    fake_where = "";
    psString    warp_where = "";
    psString    diff_where = "";
    psString    stack_where = "";

    if (!p_psDBRunQueryF(config->dbh,
            query,
            raw_where,
            raw_clean_where,
            chip_where,
            camera_where,
            fake_where,
            warp_where,
            diff_where,
            stack_where)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(query);
        return false;
    }
    psFree(query);

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psErrorCode err = psErrorCodeLast();
        switch (err) {
            case PS_ERR_DB_CLIENT:
                psError(PXTOOLS_ERR_SYS, false, "database error");
            case PS_ERR_DB_SERVER:
                psError(PXTOOLS_ERR_PROG, false, "database error");
            default:
                psError(PXTOOLS_ERR_PROG, false, "unknown error");
        }

        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

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

    psFree(output);

    return true;
}

static bool addprocessedcomponentMode(pxConfig *config)
{

    // required values
    PXOPT_LOOKUP_S64(dist_id, config->args, "-dist_id", true, false);
    PXOPT_LOOKUP_STR(component, config->args, "-component", true, false);

    // unless fault code is set require filename, bytes, and md5sum
    bool require_fileinfo = false;
    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
    if (!fault) {
        require_fileinfo = true;
    }
    PXOPT_LOOKUP_S32(bytes, config->args, "-bytes", require_fileinfo, false);
    PXOPT_LOOKUP_STR(md5sum, config->args, "-md5sum", require_fileinfo, false);
    PXOPT_LOOKUP_STR(name, config->args, "-name", require_fileinfo, false);

    if (!distComponentInsert(config->dbh, dist_id, component, bytes, md5sum, "full", name, fault)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    pxAddLabelSearchArgs (config, where, "-label", "label", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    // look for "inputs" that need to processed
    psString query = pxDataGet("disttool_toadvance.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    }
    psFree(where);

    // 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) {
        psErrorCode err = psErrorCodeLast();
        switch (err) {
            case PS_ERR_DB_CLIENT:
                psError(PXTOOLS_ERR_SYS, false, "database error");
            case PS_ERR_DB_SERVER:
                psError(PXTOOLS_ERR_PROG, false, "database error");
            default:
                psError(PXTOOLS_ERR_PROG, false, "unknown error");
        }

        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

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

    psFree(output);

    return true;
}
static bool processedcomponentMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    // look for "inputs" that need to processed
    psString query = pxDataGet("disttool_processedcomponent.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    }
    psFree(where);

    // 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) {
        psErrorCode err = psErrorCodeLast();
        switch (err) {
            case PS_ERR_DB_CLIENT:
                psError(PXTOOLS_ERR_SYS, false, "database error");
            case PS_ERR_DB_SERVER:
                psError(PXTOOLS_ERR_PROG, false, "database error");
            default:
                psError(PXTOOLS_ERR_PROG, false, "unknown error");
        }

        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

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

    psFree(output);

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    // look for "inputs" that need to processed
    psString query = pxDataGet("disttool_pendingfileset.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

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

    // 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) {
        psErrorCode err = psErrorCodeLast();
        switch (err) {
            case PS_ERR_DB_CLIENT:
                psError(PXTOOLS_ERR_SYS, false, "database error");
            case PS_ERR_DB_SERVER:
                psError(PXTOOLS_ERR_PROG, false, "database error");
            default:
                psError(PXTOOLS_ERR_PROG, false, "unknown error");
        }

        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

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

    psFree(output);

    return true;
}
static bool addfilesetMode(pxConfig *config)
{

    // required values
    PXOPT_LOOKUP_S64(dist_id, config->args, "-dist_id", true, false);
    PXOPT_LOOKUP_S64(prod_id, config->args, "-prod_id", true, false);

    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);

    // unless fault code is set require name
    PXOPT_LOOKUP_STR(name, config->args, "-name", fault == 0, false);

    if (!rcDSFilesetInsert(config->dbh, 
            0,          // fs_id
            dist_id,
            prod_id,
            name,
            "full",
            fault)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}
static bool revertfilesetMode(pxConfig *config)
{
    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
    PXOPT_COPY_S64(config->args, where, "-dist_id", "rcDSFileset.dist_id", "==");
    PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    PXOPT_COPY_S16(config->args, where, "-fault", "rcDSFileset.fault", "==");

    // It might be useful to be able to query by the parameters of the underlying runs

    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
        psFree(where);
        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
        return false;
    }

    psString query = pxDataGet("disttool_revertfileset.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        psFree(where);
        return false;
    }

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

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

    int numUpdated = psDBAffectedRows(config->dbh);

    psLogMsg("disttool", PS_LOG_INFO, "deleted %d rcDSFilesets", numUpdated);

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dest_id", "dist_id", "==");
//     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    // look for "inputs" that need to processed
    psString query = pxDataGet("disttool_pendingdest.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        return false;
    }

    psString whereString = NULL;
    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        // where string gets added by hook, not at the end of the query
        psStringAppend(&whereString, "\n AND %s", whereClause);
        psFree(whereClause);
    }
    psFree(where);

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

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

    psArray *output = p_psDBFetchResult(config->dbh);
    if (!output) {
        psErrorCode err = psErrorCodeLast();
        switch (err) {
            case PS_ERR_DB_CLIENT:
                psError(PXTOOLS_ERR_SYS, false, "database error");
            case PS_ERR_DB_SERVER:
                psError(PXTOOLS_ERR_PROG, false, "database error");
            default:
                psError(PXTOOLS_ERR_PROG, false, "unknown error");
        }

        return false;
    }
    if (!psArrayLength(output)) {
        psTrace("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

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

    psFree(output);

    return true;
}

static bool queuercrunMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    psMetadata *where = psMetadataAlloc();

    PXOPT_COPY_S64(config->args, where, "-dist_id",  "dist_id", "==");
    PXOPT_COPY_S64(config->args, where, "-dest_id",  "dest_id", "==");
    PXOPT_COPY_S64(config->args, where, "-prod_id",  "prod_id", "==");
    PXOPT_COPY_S64(config->args, where, "-target_id","target_id", "==");
    PXOPT_COPY_S64(config->args, where, "-fs_id",    "fs_id", "==");
    PXOPT_COPY_STR(config->args, where, "-stage",    "stage", "==");;
    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
    PXOPT_COPY_STR(config->args, where, "-label",    "label", "==");

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);

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

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

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

    long numUpdated = psDBAffectedRows(config->dbh);

    psLogMsg("disttool", PS_LOG_INFO, "Inserted %ld rcRuns", numUpdated);

    return true;
}


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

    psMetadata *where = psMetadataAlloc();

    PXOPT_LOOKUP_S64(rc_id, config->args, "-rc_id", false, false);
    PXOPT_LOOKUP_STR(fs_name, config->args, "-fs_name", false, false);
    PXOPT_LOOKUP_STR(status_fs_name, config->args, "-status_fs_name", false, false);
    PXOPT_LOOKUP_S64(dest_id, config->args, "-dest_id", false, false);

    // We either need rc_id or (dest_id and fs_name) to identifiy the rcRun
    if ((!rc_id) && !(dest_id && fs_name)) {
        psError(PXTOOLS_ERR_DATA, true, "either -rc_id or (-fs_name and -dest_id) are required");
        return false;
    }

    // now that we have done the argument checking
    PXOPT_COPY_S64(config->args, where, "-rc_id", "rc_id", "==");
    PXOPT_COPY_STR(config->args, where, "-fs_name", "rcDSFileset.name", "==");
    PXOPT_COPY_S64(config->args, where, "-dest_id", "rcRun.dest_id", "==");

    if (!psListLength(where->list)) {
        // this can't happen because we checked above
        psFree(where);
        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
        return false;
    }


    PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);

    if (!state && (fault < 0)) {
        psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state) are required");
        psFree(where);
        return false;
    }

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

    psString setString = psStringCopy("\n");
    psString separator = "";
    if (state) {
        psStringAppend(&setString, " rcRun.state = '%s'", state);
        separator = ",";
    }

    // default for fault is -1, so set it if it's zero or higher. This allows clearing fault
    // without forcing revert first
    if (fault >= 0) {
        psStringAppend(&setString, "%s rcRun.fault = %d", separator, fault);
    }

    if (status_fs_name) {
        psStringAppend(&setString, ", rcRun.status_fs_name = '%s'", status_fs_name);
    }

    if (last_fileset) {
        psStringAppend(&setString, ", rcDestination.last_fileset = '%s'", last_fileset);
    }

    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    psStringAppend(&query, " AND %s", whereClause);
    psFree(whereClause);
    psFree(where);

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

    long numUpdated = psDBAffectedRows(config->dbh);

    psLogMsg("disttool", PS_LOG_INFO, "Updated %ld rows", numUpdated);

    return true;
}

static bool revertrcrunMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-rc_id", "rc_id", "==");
    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    PXOPT_COPY_S64(config->args, where, "-fault", "fault", "==");

    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
        psFree(where);
        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
        return false;
    }

    psString query = pxDataGet("disttool_revertrcrun.sql");
    if (!query) {
        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
        if (!psDBRollback(config->dbh)) {
            psError(PS_ERR_UNKNOWN, false, "database error");
        }
        return false;
    }

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

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

    long numUpdated = psDBAffectedRows(config->dbh);

    psLogMsg("disttool", PS_LOG_INFO, "Updated %ld rcRuns", numUpdated);

    return true;
}

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

    // required
    PXOPT_LOOKUP_STR(label, config->args, "-label", true, false);
    PXOPT_LOOKUP_STR(filter, config->args, "-filter", true, false);
    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);

    // optional
    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    PXOPT_LOOKUP_STR(comment, config->args, "-comment", false, false);

    distTargetRow *row = distTargetRowAlloc(
            0,          // target_id
            label,
            filter,
            stage,
            clean,
            state ? state : "enabled",
            comment
            );
            
    if (!row) {
        psError(PS_ERR_UNKNOWN, false, "failed to allocate distTarget object");
        return false;
    }
   if (!distTargetInsertObject(config->dbh, row)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(row);
        return false;
    }

    // get the assigned target_id
    row->target_id = psDBLastInsertID(config->dbh);

    if (!distTargetPrintObject(stdout, row, true)) {
            psError(PS_ERR_UNKNOWN, false, "failed to print object");
            psFree(row);
            return false;
    }

    psFree(row);

    return true;
}
static bool updatetargetMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");

    PXOPT_LOOKUP_STR(state, config->args, "-set_state", true, false);

    psString query = psStringCopy("UPDATE distTarget SET state = '%s'");

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    } else {
        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
        psFree(where);
        psFree(query);
        return false;
    }
    psFree(where);

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

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");

    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);

    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);

    if (clean && full) {
        psError(PS_ERR_UNKNOWN, false, "can't select both -clean and -full");
        return false;
    }

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

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
        if (clean) {
            psStringAppend(&query, " AND (clean)");
        } else if (full) {
            psStringAppend(&query, " AND (!clean)");
        }
    } else if (clean) {
        psStringAppend(&query, " WHERE clean");
    } else if (full) {
        psStringAppend(&query, " WHERE !clean");
    }
    psFree(where);

    // 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);
        if (!psDBRollback(config->dbh)) {
            psError(PS_ERR_UNKNOWN, false, "database error");
        }
        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("disttool", PS_LOG_INFO, "no rows found");
        psFree(output);
        return true;
    }

    if (!ippdbPrintMetadatas(stdout, output, "distTarget", !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print array");
        psFree(output);
        return false;
    }

    psFree(output);

    return true;
}

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

    // required
    PXOPT_LOOKUP_STR(name, config->args,   "-name", true, false);
    PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", true, false);
    PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", true, false);

    // XXX: should we insure that these names do not contatin any whitespace?

    rcDSProductRow *row = rcDSProductRowAlloc(
            0,          // prod_id
            name,
            dbname,
            dbhost
            );
            
    if (!row) {
        psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDSProduct object");
        return false;
    }
   if (!rcDSProductInsertObject(config->dbh, row)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(row);
        return false;
    }

    // get the assigned target_id
    row->prod_id = psDBLastInsertID(config->dbh);

    if (!rcDSProductPrintObject(stdout, row, true)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print object");
        psFree(row);
        return false;
    }

    psFree(row);

    return true;
}
static bool updatedsproductMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");

    PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", false, false);
    PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", false, false);

    if (!(dbname || dbhost)) {
        psError(PS_ERR_UNKNOWN, true, "one or more of dbname or dbhost is required");
        psFree(where);
        return false;
    }
    psString query = psStringCopy("UPDATE rcDSProduct SET");
    psString sep = "";
    if (dbname) {
        psStringAppend(&query, " dbname = '%s'", dbname);
        sep = ",";
    }
    if (dbhost) {
        psStringAppend(&query, " %s dbhost = '%s'", sep, dbhost);
    }

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    } else {
        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
        psFree(where);
        psFree(query);
        return false;
    }
    psFree(where);

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

    return true;
}

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

    // required
    PXOPT_LOOKUP_S64(prod_id, config->args,      "-prod_id", true, false);
    PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);

    // optional
    PXOPT_LOOKUP_STR(status_uri, config->args,   "-status_uri", false, false);
    PXOPT_LOOKUP_STR(comment, config->args,      "-comment", false, false);
    PXOPT_LOOKUP_STR(last_fileset, config->args, "-last_fileset", false, false);
    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);

    // XXX: should we insure that these names do not contatin any whitespace?

    rcDestinationRow *row = rcDestinationRowAlloc(
            0,          // dest_id
            prod_id,
            name,
            status_uri,
            comment,
            last_fileset,
            state ? state : "enabled"
            );
            
    if (!row) {
        psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDestination object");
        return false;
    }
   if (!rcDestinationInsertObject(config->dbh, row)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(row);
        return false;
    }

    // get the assigned target_id
    row->dest_id = psDBLastInsertID(config->dbh);

    if (!rcDestinationPrintObject(stdout, row, true)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print object");
        psFree(row);
        return false;
    }

    psFree(row);

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");

    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
#ifdef ALLOW_UPDATE_LAST_FILESET
    PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
    if (!(state || last_fileset)) {
        psError(PS_ERR_UNKNOWN, true, "one or more of -set_state or -set_last_fileset is required");
# else 
    if (!state) {
#endif
        psFree(where);
        return false;
    }
    psString query = psStringCopy("UPDATE rcDestination SET");
    psString sep = "";
    if (state) {
        psStringAppend(&query, " state = '%s'", state);
        sep = ",";
    }
#ifdef ALLOW_UPDATE_LAST_FILESET
    // last_fileset normally gets set by updatercrunMode
    // Allowing it to be set here might cause problems
    // especially since we are allowing selection by prod_id
    if (last_fileset) {
        psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
    }
#endif

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    } else {
        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
        psFree(where);
        psFree(query);
        return false;
    }
    psFree(where);

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

    return true;
}

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

    // required
    PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", true, false);
    PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", true, false);

    // optional
    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);

    // XXX: should we insure that these names do not contatin any whitespace?

    rcInterestRow *row = rcInterestRowAlloc(
            0,          // int_id
            dest_id,
            target_id,
            state ? state : "enabled"
            );
            
    if (!row) {
        psError(PS_ERR_UNKNOWN, false, "failed to allocate rcInterest object");
        return false;
    }
   if (!rcInterestInsertObject(config->dbh, row)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(row);
        return false;
    }

    // get the assigned target_id
    row->int_id = psDBLastInsertID(config->dbh);

    if (!rcInterestPrintObject(stdout, row, true)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print object");
        psFree(row);
        return false;
    }

    psFree(row);

    return true;
}

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

    psMetadata *where = psMetadataAlloc();
    PXOPT_COPY_S64(config->args, where, "-int_id",    "int_id", "==");
    PXOPT_COPY_S64(config->args, where, "-dest_id",   "dest_id", "==");
    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");

    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    if (!state) {
        psError(PS_ERR_UNKNOWN, true, "-set_state is required");
        psFree(where);
        return false;
    }
    psString query = NULL;
    psStringAppend(&query, "UPDATE rcInterest SET state = '%s'", state);

    if (psListLength(where->list)) {
        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
        psStringAppend(&query, " WHERE %s", whereClause);
        psFree(whereClause);
    } else {
        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
        psFree(where);
        psFree(query);
        return false;
    }
    psFree(where);

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

    return true;
}

