/*
 * pxinjectConfig.c
 *
 * Copyright (C) 2006  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 <psmodules.h>

#include "pxtools.h"
#include "pxinject.h"

pxConfig *pxinjectConfig(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;
    }

    // -newExp
    psMetadata *newExpArgs = psMetadataAlloc();
    psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-tmp_exp_name",  0,
            "define the exp_name (required)", NULL);
    psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-tmp_inst",  0,
            "define the camera name (required)", NULL);
    psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-tmp_telescope",  0,
            "define the telescope name (required)", NULL);
    psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-workdir",  0,
            "define workdir (required)", 0);
    psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-reduction",  0,
            "define reduction class", NULL);
    psMetadataAddBool(newExpArgs, PS_LIST_TAIL, "-simple",  0,
            "use the simple output format", false);

    // -newImfile
    psMetadata *newImfileArgs = psMetadataAlloc();
    psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-exp_id",  0,
            "define the exp_id (required)", NULL);
    psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-tmp_class_id",  0,
            "define the class ID (required)", NULL);
    psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-uri",  0,
            "define the URI (required)", NULL);

    // -updatenewExp
    psMetadata *updatenewExpArgs = psMetadataAlloc();
    psMetadataAddStr(updatenewExpArgs, PS_LIST_TAIL, "-exp_id",  0,
            "define the exp_id (required)", NULL);
    psMetadataAddStr(updatenewExpArgs, PS_LIST_TAIL, "-state", 0,
            "set state (required)", NULL);


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

    PXTOOL_ADD_MODE("-newExp",      "", PXINJECT_MODE_NEWEXP,       newExpArgs);
    PXTOOL_ADD_MODE("-newImfile",   "", PXINJECT_MODE_NEWIMFILE,    newImfileArgs);
    PXTOOL_ADD_MODE("-updatenewExp",   "", PXINJECT_MODE_UPDATENEWEXP,    updatenewExpArgs);

    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(det_id);
    {
        int n = 0;
        bool status = false;
        if ((n = psMetadataLookupS32(&status, config->args, "-iteration"))) {
            if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "iteration", 0, "==", n)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item iteration");
                psFree(config);
                return NULL;
            }
        }
    }
    addWhereStr(det_type);
    addWhereStr(exp_id);
    addWhereStr(class_id);
    // convert '-inst' to 'camera'
    {
        psString str = NULL;
        bool status = false;
        if ((str = psMetadataLookupStr(&status, config->args, "-inst"))) {
            if (!psMetadataAddStr(config->where, PS_LIST_TAIL, "camera", 0, "==", str)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
                psFree(config);
                return NULL;
            }
        }
    }
    addWhereStr(telescope);
    {
        int n = 0;
        bool status = false;
        if ((n = psMetadataLookupS32(&status, config->args, "-imfiles"))) {
            if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "imfiles", 0, "==", n)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
                psFree(config);
                return NULL;
            }
        }
    }
    addWhereStr(filter);
    addWhereStr(stats);
    addWhereStr(recipe);
    {
        int n = 0;
        bool status = false;
        if ((n = psMetadataLookupS32(&status, config->args, "-guide_version"))) {
            if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "guide_version", 0, "==", n)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item guide_version");
                psFree(config);
                return NULL;
            }
        }
    }
    {
        bool boolean = false;
        bool status = false;

        if ((boolean = psMetadataLookupBool(&status, config->args, "-accept"))) {
            if (!psMetadataAddBool(config->where, PS_LIST_TAIL, "accept", 0, "==", boolean)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item accept");
                psFree(config);
                return NULL;
            }
        }
    }

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