/*
 * magictoolConfig.c
 *
 * Copyright (C) 2007  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 <stdint.h>

#include <psmodules.h>

#include "pxtools.h"
#include "magictool.h"

pxConfig *magictoolConfig(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");
        psFree(config);
        return NULL;
    }

    psTime *now = psTimeGetNow(PS_TIME_TAI);

    // -definebyquery
    psMetadata *queueArgs = psMetadataAlloc();

    // -definerun
    psMetadata *definerunArgs = psMetadataAlloc();
    psMetadataAddStr(definerunArgs, PS_LIST_TAIL, "-workdir", 0,            "define workdir (required)", NULL);
    psMetadataAddStr(definerunArgs, PS_LIST_TAIL, "-label", 0,            "define label", NULL);
    psMetadataAddStr(definerunArgs, PS_LIST_TAIL, "-dvodb", 0,            "define dvodb", NULL);
    psMetadataAddTime(definerunArgs, PS_LIST_TAIL, "-registered",  0,            "time detrend run was registered", now);
    psMetadataAddBool(definerunArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -updaterun
    psMetadata *updaterunArgs = psMetadataAlloc();
    psMetadataAddStr(updaterunArgs, PS_LIST_TAIL, "-magic_id", 0,            "define warptool ID (required)", NULL);
    psMetadataAddStr(updaterunArgs, PS_LIST_TAIL, "-state", 0,            "set state (required)", NULL);

    // -addinputskyfile
    psMetadata *addinputskyfileArgs = psMetadataAlloc();
    psMetadataAddStr(addinputskyfileArgs, PS_LIST_TAIL, "-magic_id", 0,            "define magictool ID (required)", NULL);
    psMetadataAddStr(addinputskyfileArgs, PS_LIST_TAIL, "-diff_id", 0,            "define difftool ID (required)", NULL);
    psMetadataAddStr(addinputskyfileArgs, PS_LIST_TAIL, "-node",  0,            "define symbolic node name (required)", NULL);

    // -inputskyfile
    psMetadata *inputskyfileArgs = psMetadataAlloc();
    psMetadataAddStr(inputskyfileArgs, PS_LIST_TAIL, "-magic_id", 0,            "search by magictool ID", NULL);
    psMetadataAddStr(inputskyfileArgs, PS_LIST_TAIL, "-diff_id", 0,            "search by difftool ID", NULL);
    psMetadataAddStr(inputskyfileArgs, PS_LIST_TAIL, "-node",  0,            "search by symbolic node name", NULL);
    psMetadataAddU64(inputskyfileArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(inputskyfileArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -totree
    psMetadata *totreeArgs = psMetadataAlloc();
    psMetadataAddStr(totreeArgs, PS_LIST_TAIL, "-magic_id", 0, "search by magic ID", NULL);
    psMetadataAddU64(totreeArgs, PS_LIST_TAIL, "-limit", 0, "limit result set to N items", 0);
    psMetadataAddBool(totreeArgs, PS_LIST_TAIL, "-simple", 0, "use the simple output format", false);

    // -inputtree
    psMetadata *inputtreeArgs = psMetadataAlloc();
    psMetadataAddStr(inputtreeArgs, PS_LIST_TAIL, "-magic_id", 0,            "define magictool ID (required)", NULL);
    psMetadataAddStr(inputtreeArgs, PS_LIST_TAIL, "-dep_file", 0,            "order of operations dep. file (required)", NULL);
    psMetadataAddS16(inputtreeArgs, PS_LIST_TAIL, "-code",  0, "set fault code", 0);

    // -inputs
    psMetadata *inputsArgs = psMetadataAlloc();
    psMetadataAddStr(inputsArgs, PS_LIST_TAIL, "-magic_id", 0,            "search by magictool ID", NULL);
    psMetadataAddStr(inputsArgs, PS_LIST_TAIL, "-node",  0,            "search by symbolic node name", NULL);
    psMetadataAddU64(inputsArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(inputsArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -tooprocess
    psMetadata *toprocessArgs = psMetadataAlloc();
    psMetadataAddStr(toprocessArgs, PS_LIST_TAIL, "-magic_id", 0,            "search by magic ID", NULL);
    psMetadataAddU64(toprocessArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(toprocessArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -addresult
    psMetadata *addresultArgs = psMetadataAlloc();
    psMetadataAddStr(addresultArgs, PS_LIST_TAIL, "-magic_id", 0,            "define magictool ID (required)", NULL);
    psMetadataAddStr(addresultArgs, PS_LIST_TAIL, "-node",  0,            "define symbolic node name (required)", NULL);
    psMetadataAddStr(addresultArgs, PS_LIST_TAIL, "-uri",  0,            "define URI (required)", NULL);
    psMetadataAddS16(addresultArgs, PS_LIST_TAIL, "-code",  0, "set fault code", 0);

    // -tomask
    psMetadata *tomaskArgs = psMetadataAlloc();
    psMetadataAddU64(tomaskArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(tomaskArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -addmask
    psMetadata *addmaskArgs = psMetadataAlloc();
    psMetadataAddStr(addmaskArgs, PS_LIST_TAIL, "-magic_id", 0,          "define magictool ID (required)", NULL);
    psMetadataAddStr(addmaskArgs, PS_LIST_TAIL, "-uri",  0, "define URI", NULL);
    psMetadataAddS32(addmaskArgs, PS_LIST_TAIL, "-streaks",  0, "define number of streaks", 0);
    psMetadataAddS16(addmaskArgs, PS_LIST_TAIL, "-code",  0, "set fault code", 0);

# if (0)
    // -toskyfilemask
    psMetadata *toskyfilemaskArgs = psMetadataAlloc();
    psMetadataAddStr(toskyfilemaskArgs, PS_LIST_TAIL, "-magic_id", 0,            "search by magic ID", NULL);
    psMetadataAddU64(toskyfilemaskArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
    psMetadataAddBool(toskyfilemaskArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);

    // -addskyfilemask
    psMetadata *addskyfilemaskArgs = psMetadataAlloc();
    psMetadataAddStr(addskyfilemaskArgs, PS_LIST_TAIL, "-magic_id", 0,            "define magictool ID (required)", NULL);
    psMetadataAddStr(addskyfilemaskArgs, PS_LIST_TAIL, "-diff_id",  0,            "define difftool ID (required)", NULL);
    psMetadataAddStr(addskyfilemaskArgs, PS_LIST_TAIL, "-uri",  0,            "define URI (required)", NULL);
# endif

    // -mask
    psMetadata *maskArgs = psMetadataAlloc();
    psMetadataAddStr(maskArgs, PS_LIST_TAIL, "-magic_id", 0, "define magictool ID", NULL);
    psMetadataAddU64(maskArgs, PS_LIST_TAIL, "-limit", 0, "limit result set to N items", 0);
    psMetadataAddBool(maskArgs, PS_LIST_TAIL, "-simple", 0, "use the simple output format", false);

    psFree(now);

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

    PXOPT_ADD_MODE("-definebyquery",   "", MAGICTOOL_MODE_DEFINEBYQUERY,   queueArgs);
    PXOPT_ADD_MODE("-definerun",       "", MAGICTOOL_MODE_DEFINERUN,       definerunArgs);
    PXOPT_ADD_MODE("-updaterun",       "", MAGICTOOL_MODE_UPDATERUN,       updaterunArgs);
    PXOPT_ADD_MODE("-addinputskyfile", "", MAGICTOOL_MODE_ADDINPUTSKYFILE, addinputskyfileArgs);
    PXOPT_ADD_MODE("-inputskyfile",    "", MAGICTOOL_MODE_INPUTSKYFILE,    inputskyfileArgs);
    PXOPT_ADD_MODE("-totree",          "", MAGICTOOL_MODE_TOTREE,          totreeArgs);
    PXOPT_ADD_MODE("-inputtree",       "", MAGICTOOL_MODE_INPUTTREE,       inputtreeArgs);
    PXOPT_ADD_MODE("-toprocess",       "", MAGICTOOL_MODE_TOPROCESS,       toprocessArgs);
    PXOPT_ADD_MODE("-inputs",          "", MAGICTOOL_MODE_INPUTS,          inputsArgs);
    PXOPT_ADD_MODE("-addresult",       "", MAGICTOOL_MODE_ADDRESULT,       addresultArgs);
    PXOPT_ADD_MODE("-tomask",          "", MAGICTOOL_MODE_TOMASK,          tomaskArgs);
    PXOPT_ADD_MODE("-addmask",         "", MAGICTOOL_MODE_ADDMASK,         addmaskArgs);
    PXOPT_ADD_MODE("-mask",            "", MAGICTOOL_MODE_MASK,            maskArgs);

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