# include "psastroStandAlone.h"

pmConfig *psastroArguments (int argc, char **argv) {

    bool status;
    int N;

    if (argc == 1) {
        psError(PSASTRO_ERR_ARGUMENTS, true, "No arguments supplied");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    // load config data from default locations
    pmConfig *config = pmConfigRead(&argc, argv, PSASTRO_RECIPE);
    if (config == NULL) {
        psError(PSASTRO_ERR_CONFIG, false, "Can't read site configuration");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    // save the following additional recipe values based on command-line options
    // these options override the PSASTRO recipe values loaded from recipe files
    psMetadata *options = pmConfigRecipeOptions (config, PSASTRO_RECIPE);

    // photcode : used in output to supplement header data (argument or recipe?)
    if ((N = psArgumentGet (argc, argv, "-photcode"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddStr (options, PS_LIST_TAIL, "PHOTCODE", PS_META_REPLACE, "", argv[N]);
        psArgumentRemove (N, &argc, argv);
    }

    // chip selection is used to limit chips to be processed
    if ((N = psArgumentGet (argc, argv, "-chip"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddStr (config->arguments, PS_LIST_TAIL, "CHIP_SELECTIONS", PS_META_REPLACE, "", argv[N]);
        psArgumentRemove (N, &argc, argv);
    }

    // apply mosastro mode?
    if ((N = psArgumentGet (argc, argv, "-mosastro"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.MOSAIC.MODE", PS_META_REPLACE, "", false);
    }
    if ((N = psArgumentGet (argc, argv, "+mosastro"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.MOSAIC.MODE", PS_META_REPLACE, "", true);
    }

    // apply chipastro mode?
    if ((N = psArgumentGet (argc, argv, "-chipastro"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.CHIP.MODE", PS_META_REPLACE, "", false);
    }
    if ((N = psArgumentGet (argc, argv, "+chipastro"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.CHIP.MODE", PS_META_REPLACE, "", true);
    }

    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
    if (!status) {
        psError(PSASTRO_ERR_ARGUMENTS, true, "Missing -file (input) or -list (input)");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    if (argc != 2) {
        psError(PSASTRO_ERR_ARGUMENTS, true, "Incorrect arguments supplied");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    // output positions is fixed
    psMetadataAddStr (config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "", argv[1]);

    psTrace("psastro", 1, "Done with psastroArguments...\n");
    return (config);
}
