/*
 * pstamptoolConfig.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 <psmodules.h>

#include "pxtools.h"
#include "pstamptool.h"

pxConfig *pstamptoolConfig(pxConfig *config, int argc, char **argv)
{
    if (!config) {
        config = pxConfigAlloc();
    }

    pmConfigReadParamsSet(false);

    // setup site config
    config->modules = pmConfigRead(&argc, argv, NULL);
    if (! config->modules) {
        psError(PS_ERR_UNKNOWN, false, "Can't find site configuration!\n");
        psFree(config);
        return NULL;
    }

    // -adddatastore
    psMetadata *adddatastoreArgs = psMetadataAlloc();
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-uri", 0,
            "define storage uri", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-last_fileset", 0,
            "define last fileset seen", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-state", 0,
            "define datastore state", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-out_product", 0,
            "define output product name", NULL);
    
    // -datastore
    psMetadata *datastoreArgs = psMetadataAlloc();
    psMetadataAddBool(datastoreArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);
    psMetadataAddStr(datastoreArgs, PS_LIST_TAIL, "-ds_id", 0,
            "define ds_id", NULL); 

    // -moddatastore
    psMetadata *moddatastoreArgs = psMetadataAlloc();
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-ds_id", 0,
            "define ds_id", NULL); 
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-last_fileset", 0,
            "define storage uri", NULL);
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-state", 0,
            "define storage uri", NULL);
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-out_product", 0,
            "define output product name", NULL);


    // -addreq
    psMetadata *addreqArgs = psMetadataAlloc();
    psMetadataAddStr(addreqArgs, PS_LIST_TAIL, "-uri", 0,
            "define request file uri", NULL); 
    psMetadataAddStr(addreqArgs, PS_LIST_TAIL, "-ds_id", 0,
            "define request ds_id", NULL); 
    psMetadataAddStr(addreqArgs, PS_LIST_TAIL, "-out_fileset", 0,
            "define request output_fileset", NULL); 

    // -pendingreq
    psMetadata *pendingreqArgs = psMetadataAlloc();
    psMetadataAddStr(pendingreqArgs, PS_LIST_TAIL, "-req_id", 0,
            "define req_id", NULL); 
    psMetadataAddU64(pendingreqArgs, PS_LIST_TAIL, "-limit",  0,
            "limit result set to N items", 0);
    psMetadataAddBool(pendingreqArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -listreq
    psMetadata *listreqArgs = psMetadataAlloc();
    psMetadataAddStr(listreqArgs, PS_LIST_TAIL, "-req_id", 0,
            "define req_id", NULL); 
    psMetadataAddU64(listreqArgs, PS_LIST_TAIL, "-limit",  0,
            "limit result set to N items", 0);
    psMetadataAddBool(listreqArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -processedreq
    psMetadata *processedreqArgs = psMetadataAlloc();
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-req_id", 0,
            "req_id for which to change state", NULL); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-state", 0,
            "new state", NULL); 

    // -addjob
    psMetadata *addjobArgs = psMetadataAlloc();
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-uri", 0,
            "define job file uri", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-req_id", 0,
            "define job req_id", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-job_type", 0,
            "define job job_type", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-outputBase", 0,
            "define job outputBase", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-args", 0,
            "define job args", NULL); 

    // -listjob
    psMetadata *listjobArgs = psMetadataAlloc();
    psMetadataAddStr(listjobArgs, PS_LIST_TAIL, "-req_id", 0,
            "define request", NULL); 
    psMetadataAddStr(listjobArgs, PS_LIST_TAIL, "-job_id", 0,
            "define job", NULL); 
    psMetadataAddU64(listjobArgs, PS_LIST_TAIL, "-limit",  0,
            "limit result set to N items", 0);
    psMetadataAddBool(listjobArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -pendingjob
    psMetadata *pendingjobArgs = psMetadataAlloc();
    psMetadataAddStr(pendingjobArgs, PS_LIST_TAIL, "-job_id", 0,
            "define job", NULL); 
    psMetadataAddU64(pendingjobArgs, PS_LIST_TAIL, "-limit",  0,
            "limit result set to N items", 0);
    psMetadataAddBool(pendingjobArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -processedjob
    psMetadata *processedjobArgs = psMetadataAlloc();
    psMetadataAddStr(processedjobArgs, PS_LIST_TAIL, "-job_id", 0,
            "req_id for which to change state", NULL); 
    psMetadataAddStr(processedjobArgs, PS_LIST_TAIL, "-state", 0,
            "new state", NULL); 
    psMetadataAddStr(processedjobArgs, PS_LIST_TAIL, "-result", 0,
            "new result", NULL); 

#define PXTOOL_MODE(option, modeval, argset) \
{ \
    int N = 0; \
    if ((N = psArgumentGet (argc, argv, option))) { \
        psArgumentRemove (N, &argc, argv); \
        if (config->mode) { \
            psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed"); \
            psFree(config); \
            return NULL; \
        } \
        config->mode = modeval; \
        config->args = psMemIncrRefCounter(argset); \
    } \
    if (!psMetadataAddMetadata(argSets, PS_LIST_TAIL, option, 0, NULL, argset)) {;\
        psError(PS_ERR_UNKNOWN, false, "failed to add argset for %s", option); \
    } \
    psFree(argset); \
}

    psMetadata *argSets = psMetadataAlloc();
    psMetadata *modes = psMetadataAlloc();

    PXOPT_ADD_MODE("-addreq",          "", PSTAMPTOOL_MODE_ADDREQ,       addreqArgs);
    PXOPT_ADD_MODE("-pendingreq",      "", PSTAMPTOOL_MODE_PENDINGREQ,   pendingreqArgs);
    PXOPT_ADD_MODE("-processedreq",    "", PSTAMPTOOL_MODE_PROCESSEDREQ, processedreqArgs);
    PXOPT_ADD_MODE("-listreq",         "", PSTAMPTOOL_MODE_LISTREQ,      listreqArgs);

    PXOPT_ADD_MODE("-addjob",          "", PSTAMPTOOL_MODE_ADDJOB,       addjobArgs);
    PXOPT_ADD_MODE("-listjob",         "", PSTAMPTOOL_MODE_LISTJOB,      listjobArgs);
    PXOPT_ADD_MODE("-pendingjob",      "", PSTAMPTOOL_MODE_PENDINGJOB,   pendingjobArgs);
    PXOPT_ADD_MODE("-processedjob",    "", PSTAMPTOOL_MODE_PROCESSEDJOB, processedjobArgs);

    PXOPT_ADD_MODE("-adddatastore",    "", PSTAMPTOOL_MODE_ADDDATASTORE, adddatastoreArgs);
    PXOPT_ADD_MODE("-datastore",       "", PSTAMPTOOL_MODE_DATASTORE,    datastoreArgs);
    PXOPT_ADD_MODE("-moddatastore",    "", PSTAMPTOOL_MODE_MODDATASTORE, moddatastoreArgs);

    if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
        psError(PS_ERR_UNKNOWN, true, "option parsing failed");
        psFree(argSets);
        psFree(modes);
        psFree(config);
        return NULL;
    }

    psFree(argSets);
    psFree(modes);

    // setup search criterion
#define addWhereStr(name) \
{ \
    psString str = NULL; \
    bool status = false; \
    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
        if (!psMetadataAddStr(config->where, PS_LIST_TAIL, #name, 0, "==", str)) {\
            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
    } \
}

    // generate SQL where clause
    config->where = psMetadataAlloc();

    addWhereStr(req_id); 
    addWhereStr(job_id); 

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

    // define Database handle, if used
    // do this last so we don't setup a connection before CLI options are
    // validated
    config->dbh = pmConfigDB(config->modules);
    if (!config->dbh) {
        psError(PS_ERR_UNKNOWN, false, "Can't configure database");
        psFree(config);
        return NULL;
    }

    return config;
}
