/*
 * 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 (required)", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-out_product",  0, "define output product name (required)", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-last_fileset", 0, "define last fileset seen", NULL);
    psMetadataAddStr(adddatastoreArgs, PS_LIST_TAIL, "-state",        0, "define datastore state", "enabled");
    
    // -datastore
    psMetadata *datastoreArgs = psMetadataAlloc();
    psMetadataAddStr(datastoreArgs, PS_LIST_TAIL, "-ds_id", 0,            "define ds_id", NULL); 
    psMetadataAddBool(datastoreArgs, PS_LIST_TAIL, "-simple", 0,            "use the simple output format", false);

    // -moddatastore
    psMetadata *moddatastoreArgs = psMetadataAlloc();
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-ds_id", 0,            "define ds_id", "0"); 
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-last_fileset", 0,            "define storage uri", NULL);
    psMetadataAddStr(moddatastoreArgs, PS_LIST_TAIL, "-state", 0,            "define storage uri", 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);

    // -completedreq
    psMetadata *completedreqArgs = psMetadataAlloc();
    psMetadataAddU64(completedreqArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(completedreqArgs, 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); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-outProduct", 0,            "define request outProduct", NULL); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-fault", 0,            "define request fault code", NULL); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-uri", 0,            "define the uri", NULL); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-name", 0,            "define the name", NULL); 
    psMetadataAddStr(processedreqArgs, PS_LIST_TAIL, "-reqType", 0,            "define the reqType", NULL); 

    // -revertreq
    psMetadata *revertreqArgs = psMetadataAlloc();
    psMetadataAddS64(revertreqArgs, PS_LIST_TAIL, "-req_id", 0,            "req_id for which to revert", 0); 

    // -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, "-rownum", 0,            "define job rownum", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-job_type", 0,            "define job job_type", "stamp"); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-outputBase", 0,            "define job outputBase", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-args", 0,            "define job args", NULL); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-state", 0,            "new state", "run"); 
    // psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-result", 0,            "new result", "0"); 
    psMetadataAddStr(addjobArgs, PS_LIST_TAIL, "-fault", 0,            "new result", 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();
    psMetadataAddS64(pendingjobArgs, PS_LIST_TAIL, "-job_id", 0,            "define job", 0); 
    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); 
    psMetadataAddStr(processedjobArgs, PS_LIST_TAIL, "-fault", 0,            "new result", NULL); 

    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("-completedreq",    "", PSTAMPTOOL_MODE_COMPLETEDREQ, completedreqArgs);
    PXOPT_ADD_MODE("-revertreq",       "", PSTAMPTOOL_MODE_REVERTREQ,    revertreqArgs);

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

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

    return config;
}
