/*
 * pxwarp.c
 *
 * Copyright (C) 2007-2008  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 <stdlib.h>
#include <ippdb.h>
#include <string.h>

#include "pxtools.h"
#include "pxwarp.h"

bool pxwarpRunSetState(pxConfig *config, psS64 warp_id, const char *state)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    PS_ASSERT_PTR_NON_NULL(state, false);

    // check that state is a valid string value
    if (!(
            (strncmp(state, "run", 4) == 0)
            || (strncmp(state, "stop", 5) == 0)
            || (strncmp(state, "reg", 4) == 0)
        )
    ) {
        psError(PS_ERR_UNKNOWN, false,
                "invalid warpRun state: %s", state);
        return false;
    }

    char *query = "UPDATE warpRun SET state = '%s' WHERE warp_id = %" PRId64;
    if (!p_psDBRunQuery(config->dbh, query, state, warp_id)) {
        psError(PS_ERR_UNKNOWN, false,
                "failed to change state for warp_id %" PRId64, warp_id);
        return false;
    }

    return true;
}


bool pxwarpRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *state)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    PS_ASSERT_PTR_NON_NULL(state, false);

    // check that state is a valid string value
    if (!(
            (strncmp(state, "run", 4) == 0)
            || (strncmp(state, "stop", 5) == 0)
            || (strncmp(state, "reg", 4) == 0)
        )
    ) {
        psError(PS_ERR_UNKNOWN, false,
                "invalid chipRun state: %s", state);
        return false;
    }

    psString query = psStringCopy("UPDATE warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET warpRun.state = '%s'");

    if (where) {
        psString whereClause = psDBGenerateWhereSQL(where, NULL);
        if (whereClause && strlen(whereClause) > 0) {
            psStringAppend(&query, " %s", whereClause);
        }
        psFree(whereClause);
    }

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

    psFree(query);

    return true;
}


bool pxwarpRunSetLabel(pxConfig *config, psS64 warp_id, const char *label)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    // note label == NULL should be explicitly allowed

    char *query = "UPDATE warpRun SET warpRun.label = '%s' WHERE warp_id = %" PRId64;
    if (!p_psDBRunQuery(config->dbh, query, label, warp_id)) {
        psError(PS_ERR_UNKNOWN, false,
                "failed to change label for warp_id %" PRId64, warp_id);
        return false;
    }

    return true;
}


bool pxwarpRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *label)
{
    PS_ASSERT_PTR_NON_NULL(config, false);
    // note label == NULL should be explicitly allowed

    psString query = psStringCopy("UPDATE warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) SET warpRun.label = '%s'");

    if (where) {
        psString whereClause = psDBGenerateWhereSQL(where, NULL);
        psStringAppend(&query, " %s", whereClause);
        psFree(whereClause);
    }

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

    psFree(query);

    return true;
}


bool pxwarpQueueByFakeID(pxConfig *config,
                    psS64 fake_id,
                    char *workdir,
                    char *label,
                    char *dvodb,
                    char *tess_id,
                    char *end_stage)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    // depend on the f-keys to make sure we have a valid cam_id
    if (!warpRunInsert(config->dbh,
        0,          // ID
        fake_id,
        "warp",     // mode
        "run",      // state
        workdir,
        "dirty",    // workdir_state
        label,
        dvodb,
        tess_id,
        end_stage,
        NULL,       // registered
        false       // magiced
    )) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return true;
    }

    return true;
}
