#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pslib.h>
#include "ppstamp.h"
#include "ppstampOptions.h"
#include <ctype.h>

static void usage (void)
{
    fprintf(stderr, "\n");

    fprintf(stderr, "USAGE: ppstamp -skycenter RA DEC -pixrange dx dy    [-file INPUT.fits] [-list INPUT.txt] OUTPUT\n");
    fprintf(stderr, "       ppstamp -skycenter RA DEC -arcrange dRA dDEC [-file INPUT.fits] [-list INPUT.txt] OUTPUT\n");
    fprintf(stderr, "       ppstamp -pixcenter x y    -pixrange dx dy    [-class_id class_id] [-file INPUT.fits] [-list INPUT.txt] OUTPUT\n");
    fprintf(stderr, "       ppstamp -pixcenter x y    -arcrange dRA dDEC [-class_id class_id] [-file INPUT.fits] [-list INPUT.txt] OUTPUT\n");
    fprintf(stderr, "\n");

    fprintf(stderr, "-skycenter is specified as HH:MM:SS DD:MM:SS or decimal degrees\n");
    fprintf(stderr, "-arcrange values are seconds of arc\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Optional arguments:\n");
    fprintf(stderr, "   [-class_id class_id]     selects class_id (only used with -pixcenter)\n");
    fprintf(stderr, "   [-astrom astrom.cmp] :   provide an alternative astrometry calibration\n");
    fprintf(stderr, "   [-mask   mk_image] :     mask image\n");
    fprintf(stderr, "   [-variance var_image] :  variance image\n");
    fprintf(stderr, "\n");

    exit (2);
}

pmConfig *ppstampArguments(int argc, char **argv, ppstampOptions **pOptions)
{
    int argnum;                         // Argument number of interest
    bool gotCenter = false;
    bool gotRange = false;
    ppstampOptions *options;

    if (argc == 1) {
        usage();
    }

    if (psArgumentGet (argc, argv, "-version")) {
        psString version;
        version = ppstampVersionLong();   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 the site-wide configuration information
    pmConfig *config = pmConfigRead(&argc, argv, NULL);
    if (config == NULL) {
        psError(PSTAMP_ERR_CONFIG, false, "Can't read site configuration!\n");
        return(NULL);
    }

    options = ppstampOptionsAlloc();
    *pOptions = options;

    if (!pstampGetROI(&options->roip, &argc, argv, &gotCenter, &gotRange)) {
        usage();
    }

    if (!gotCenter) {
        psError(PSTAMP_ERR_ARGUMENTS, true, "must specify center of region of interest\n");
        usage();
    }

    if (!gotRange) {
        psError(PSTAMP_ERR_ARGUMENTS, true, "must specify extent of region of interest\n");
        usage();
    }

    if ((argnum = psArgumentGet(argc, argv, "-class_id"))) {
        psArgumentRemove(argnum, &argc, argv);
        options->chipName = psStringCopy(argv[argnum]);
        psArgumentRemove(argnum, &argc, argv);
    }

    pmConfigFileSetsMD(config->arguments, &argc, argv, "ASTROM", "-astrom", "-astromlist");
    pmConfigFileSetsMD(config->arguments, &argc, argv, "MASK",   "-mask", "-masklist");
    pmConfigFileSetsMD(config->arguments, &argc, argv, "VARIANCE", "-variance", "-variancelist");

    // the input file is a required argument; if not found, we will exit
    bool status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
    if (!status) {
        psError(PSTAMP_ERR_ARGUMENTS, true, "must specify INPUT\n");
        usage();
    }
    

    // finally the output file
    if (argc < 2) {
        psError(PSTAMP_ERR_ARGUMENTS, true, "must specify OUTPUT\n");
        usage();
    } else if (argc > 2) {
        psError(PSTAMP_ERR_ARGUMENTS, true, "unknown argument(s)\n");
        usage();
    }

    // Add the output image (which remains on the command-line) to the arguments list
    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "Name of the output image", argv[1]);

    // psMetadataPrint(stdout, config->arguments,0);

    return config;
}
