/*
 * pxinject.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 <stdlib.h>
#include <ippdb.h>

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

static bool newExpMode(pxConfig *config);
static bool newImfileMode(pxConfig *config);

# define MODECASE(caseName, func) \
    case caseName: \
    if (!func(config)) { \
        goto FAIL; \
    } \
    break;

int main(int argc, char **argv)
{
    psLibInit(NULL);

    pxConfig *config = pxinjectConfig(NULL, argc, argv);
    if (!config) {
        psError(PXTOOLS_ERR_CONFIG, false, "failed to configure");
        goto FAIL;
    }

    switch (config->mode) {
        MODECASE(PXINJECT_MODE_NEWEXP, newExpMode);
        MODECASE(PXINJECT_MODE_NEWIMFILE, newImfileMode);
        default:
            psAbort("invalid option (this should not happen)");
    }

    psFree(config);
    pmConfigDone();
    psLibFinalize();

    exit(EXIT_SUCCESS);

FAIL:
    psErrorStackPrint (stderr, "failure\n");
    int exit_status = pxerrorGetExitStatus();

    psFree(config);
    pmConfigDone();
    psLibFinalize();

    exit(exit_status);
}

static bool newExpMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool status = false;
    psString exp_id = psMetadataLookupStr(&status, config->args, "-exp_id");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_id");
        return false;
    }
    if (!exp_id) {
        psError(PS_ERR_UNKNOWN, true, "-exp_id is required");
        return false;
    }
    psString camera = psMetadataLookupStr(&status, config->args, "-inst");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst");
        return false;
    }
    if (!camera) {
        psError(PS_ERR_UNKNOWN, true, "-inst is required");
        return false;
    }
    psString telescope = psMetadataLookupStr(&status, config->args, "-telescope");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope");
        return false;
    }
    if (!telescope) {
        psError(PS_ERR_UNKNOWN, true, "-telescope is required");
        return false;
    }
    psS32 imfiles = psMetadataLookupS32(&status, config->args, "-imfiles");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -imfiles");
        return false;
    }
    if (!imfiles) {
        psError(PS_ERR_UNKNOWN, true, "-imfiles is required");
        return false;
    }

    psString workdir = psMetadataLookupStr(&status, config->args, "-workdir");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -workdir");
        return false;
    }
    if (!workdir) {
        psError(PS_ERR_UNKNOWN, true, "-workdir is required");
        return false;
    }

    psString exp_tag = pxGenExpTag(config, exp_id);
    if (!exp_tag) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    if (!newExpInsert(config->dbh,
                exp_tag,
                exp_id,
                camera,
                telescope,
                imfiles,
                workdir
            )
        ) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        psFree(exp_tag);
        return false;
    }

    psMetadata *md = psMetadataAlloc();
    if (!psMetadataAddStr(md, PS_LIST_TAIL, "exp_tag", 0, NULL, exp_tag)) {
        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
        psFree(md);
        psFree(exp_tag);
        psFree(md);
    }
    psFree(exp_tag);

    bool simple = false;
    {
        bool status = false;
        simple = psMetadataLookupBool(&status, config->args, "-simple");
        if (!status) {
            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
            psFree(md);
            return false;
        }
    }

    // negate simple so the default is true
    if (!ippdbPrintMetadata(stdout, md, !simple)) {
        psError(PS_ERR_UNKNOWN, false, "failed to print array");
        psFree(md);
        return false;
    }

    psFree(md);

    return true;
}

static bool newImfileMode(pxConfig *config)
{
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool status = false;
    psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
        return false;
    }
    if (!exp_tag) {
        psError(PS_ERR_UNKNOWN, true, "-exp_tag is required");
        return false;
    }
    psString class = psMetadataLookupStr(&status, config->args, "-class");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class");
        return false;
    }
    if (!class) {
        psError(PS_ERR_UNKNOWN, true, "-class is required");
        return false;
    }
    psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
        return false;
    }
    if (!class_id) {
        psError(PS_ERR_UNKNOWN, true, "-class_id is required");
        return false;
    }
    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
    if (!status) {
        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
        return false;
    }
    if (!uri) {
        psError(PS_ERR_UNKNOWN, true, "-uri is required");
        return false;
    }

    // insert with error flag state set to 0 (no errors)
    if (!newImfileInsert(config->dbh, exp_tag, class, class_id, uri)) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        return false;
    }

    return true;
}
