# include "psphotDetect.h"

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

    int N;

    if (argc == 1) {
        psError(PSPHOT_ERR_ARGUMENTS, true, "Too few arguments: %d", argc);
        return NULL;
    }

    if ((N = psArgumentGet (argc, argv, "-version"))) {
        psString version;
        version = psphotVersionLong();    fprintf (stdout, "%s\n", version); psFree (version);
        version = psModulesVersionLong(); fprintf (stdout, "%s\n", version); psFree (version);
        version = psLibVersionLong();     fprintf (stdout, "%s\n", version); psFree (version);
        exit (0);
    }

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

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

    // XXX add psphot thread arguments?

    // all three of these input files are required: if not found, we will exit
    if (!pmConfigFileSetsMD (config->arguments, &argc, argv, "MASK",   "-mask",    "-masklist")) {
        psError(PSPHOT_ERR_ARGUMENTS, false, "mask image not supplied");
        return NULL;
    }
    if (!pmConfigFileSetsMD (config->arguments, &argc, argv, "PSPHOT.PSF", "-psf", "-psflist")) {
        psError(PSPHOT_ERR_ARGUMENTS, false, "mask image not supplied");
        return NULL;
    }
    if (!pmConfigFileSetsMD (config->arguments, &argc, argv, "POINTS", "-points",  "-pointslist")) {
        psError(PSPHOT_ERR_ARGUMENTS, false, "mask image not supplied");
        return NULL;
    }

    if (argc != 2) {
        psError(PSPHOT_ERR_ARGUMENTS, true, "Expected to see one more argument; saw %d", argc - 1);
        return NULL;
    }

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

    psTrace("psphot", 1, "Done with psphotDetectArguments...\n");
    return (config);
}
