/*
 * pztoolConfig.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 "pztool.h"

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

    // -seen
    psMetadata *seenArgs = psMetadataAlloc();
    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_name", 0,
            "define exposure ID", NULL); 
    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-inst", 0,
            "define camera ID", NULL); 
    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-telescope", 0,
            "define telescope ID", NULL); 
    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_type", 0,
            "define exposure type", NULL); 
    psMetadataAddBool(seenArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);
    
    // -pendingexp
    psMetadata *pendingexpArgs = psMetadataAlloc();
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-exp_name", 0,
            "define exposure ID", NULL); 
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-inst", 0,
            "define camera ID", NULL); 
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-telescope", 0,
            "define telescope ID", NULL); 
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-exp_type", 0,
            "define exposure type", NULL); 
    psMetadataAddBool(pendingexpArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -pendingimfile
    psMetadata *pendingimfileArgs = psMetadataAlloc();
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-exp_name", 0,
            "define exposure ID", NULL); 
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-inst", 0,
            "define camera ID", NULL); 
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-telescope", 0,
            "define telescope ID", NULL); 
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-exp_type", 0,
            "define exposure type", NULL); 
    psMetadataAddBool(pendingimfileArgs, PS_LIST_TAIL, "-simple", 0,
            "use the simple output format", false);

    // -copydone
    psMetadata *copydoneArgs = psMetadataAlloc();
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-exp_name", 0,
            "define exposure ID", NULL); 
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-inst", 0,
            "define camera ID", NULL); 
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-telescope", 0,
            "define telescope ID", NULL); 
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class", 0,
            "define class", NULL);
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class_id", 0,
            "define class_id", NULL);
    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-uri", 0,
            "define storage uri", 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();

    PXTOOL_ADD_MODE("-seen",            "", PZTOOL_MODE_SEEN,         seenArgs);
    PXTOOL_ADD_MODE("-pendingexp",      "", PZTOOL_MODE_PENDINGEXP,   pendingexpArgs);
    PXTOOL_ADD_MODE("-pendingimfile",   "", PZTOOL_MODE_PENDINGIMFILE,pendingimfileArgs);
    PXTOOL_ADD_MODE("-copydone",        "", PZTOOL_MODE_COPYDONE,     copydoneArgs);

    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(exp_name); 
    addWhereStr(telescope); 
    // 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(exp_type); 
    addWhereStr(class); 
    addWhereStr(class_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;
}
