#include "ppbgrestore.h"

void usage()
{
    fprintf(stderr, "USAGE: ppbgrestore [-file INPUT.fits] | [-list INPUT.txt] [-backmdl BACKMDL.fits] | [-backmdllist BACKMDL.txt] OUTPUT\n\n");
    exit(2);
}   
pmConfig *ppbgrestoreArguments(int argc, char *argv[])
{
    int argnum;
    if (argc == 1) {
        usage();
    }

    if (psArgumentGet(argc, argv, "-version")) {
        ppbgrestoreVersionPrint();
        exit(0);
    }

    pmConfig *config = pmConfigRead(&argc, argv, NULL);
    if (config == NULL) {
        psErrorStackPrint(stderr, "Can't find site configuration!\n");
        exit(EXIT_FAILURE);
    }

    // the input file is a required argument if not found we will exit
    if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "INPUT", "-file", "-list")) {
        fprintf(stderr, "input file is required\n");
        usage();
    }
    if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "BACKMDL", "-backmdl", "-backmdllist")) {
        fprintf(stderr, "backmdl file is required\n");
        usage();
    }

    if ((argnum = psArgumentGet(argc, argv, "-savebg"))) {
        psArgumentRemove(argnum, &argc, argv);
        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "SAVE_BACKGROUND", 0, "save background innmage", true);
    }

    if (argc != 2) usage ();

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

    return config;
}
