/*
 * pxtool.c
 *
 * Copyright (C) 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 <string.h>

#include "pxtools.h"

bool pxIsValidState(const char *state)
{
    PS_ASSERT_PTR_NON_NULL(state, false);

    if (!strcmp(state, "new")) return true;
    if (!strcmp(state, "reg")) return true;
    if (!strcmp(state, "full")) return true;
    if (!strcmp(state, "drop")) return true;
    if (!strcmp(state, "wait")) return true;
    if (!strcmp(state, "goto_cleaned")) return true;
    if (!strcmp(state, "error_cleaned")) return true;
    if (!strcmp(state, "goto_scrubbed")) return true;
    if (!strcmp(state, "error_scrubbed")) return true;
    if (!strcmp(state, "cleaned")) return true;
    if (!strcmp(state, "update")) return true;
    if (!strcmp(state, "purged")) return true;
    if (!strcmp(state, "goto_purged")) return true;
    if (!strcmp(state, "error_purged")) return true;

    return false;
}

// 'scrubbed' is a virtual state equivalent to cleaned, but allows files to be removed
// even if the config files is missing


// change the value for tableName.columName from 'full' to 'cleaned' if necessary
bool pxSetStateCleaned(const psString tableName, const psString columnName, psArray *rows)
{
    for (long i = 0; i < psArrayLength(rows); i++) {
        psMetadata *row = rows->data[i];
        psString state = psMetadataLookupStr(NULL, row, columnName);
        if (!state) {
            psError(PS_ERR_PROGRAMMING, false, "%s not found in row %ld of table %s",
		    columnName, i, tableName);
            return false;
        }
        if (!strcmp("full", state)) {
            // change full to cleaned
            psMetadataAddStr(row, PS_LIST_TAIL, columnName, PS_META_REPLACE, "", "cleaned");
        } else if (strcmp("cleaned", state)) {
            // if state isn't cleaned or full we can't set it to cleaned
            psError(PS_ERR_PROGRAMMING, true, "%s with state %s may not be exported cleaned",
		    tableName, state);
            return false;
        }
    }
    return true;
}

// XXX verify data type?
bool pxAddLabelSearchArgs (pxConfig *config, psMetadata *where, char *name, char *field, char *op) {

    psMetadataItem *item = psMetadataLookup(config->args, name);
    if (!item) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s", name);
        return false;
    }
    psAssert (item->type == PS_DATA_METADATA_MULTI, "%s should be a multi container", name);
    psAssert (item->data.list->n, "%s should at least have a place-holder", name);
    psMetadataItem *entry = (psMetadataItem *)item->data.list->head->data;
    psAssert (entry, "%s should at least have a place-holder", name);
    if (entry->data.str) {
	psListIterator *iter = psListIteratorAlloc (item->data.list, PS_LIST_HEAD, true);
	psMetadataItem *item = NULL;
	while ((item = psListGetAndIncrement(iter))) {
	    // need to change the name and comment
	    psFree (item->name);
	    item->name = psStringCopy (field);
	    psFree (item->comment);
	    item->comment = psStringCopy (op);
	    if (!psMetadataAddItem(where, item, PS_LIST_TAIL, PS_META_DUPLICATE_OK)) {
		psError(PS_ERR_UNKNOWN, false, "failed to add item %s", field);
		psFree(where);
		return false;
	    }
	}
	psFree (iter);
    }
    return true;
}
