# include "psdemo.h"

static void usage (void) ;

psMetadata *psdemoArgs (int *argc, char **argv) {

  int N;
  int mode = PS_DATA_STRING | PS_META_REPLACE;

  // basic pslib options
  fprintf (stderr, "starting... %s\n", psLibVersion());
  psLogSetFormat ("M");
  psArgumentVerbosity (argc, argv);

  // optional mask image - add to config
  char *mask = NULL;
  if ((N = psArgumentGet (*argc, argv, "-mask"))) {
    psArgumentRemove (N, argc, argv);
    mask = psStringCopy (argv[N]);
    psArgumentRemove (N, argc, argv);
  }

  // optional weight image - add to config
  char *weight = NULL;
  if ((N = psArgumentGet (*argc, argv, "-weight"))) {
    psArgumentRemove (N, argc, argv);
    weight = psStringCopy (argv[N]);
    psArgumentRemove (N, argc, argv);
  }

  // optional output residual image - add to config
  char *resid = NULL;
  if ((N = psArgumentGet (*argc, argv, "-resid"))) {
    psArgumentRemove (N, argc, argv);
    resid = psStringCopy (argv[N]);
    psArgumentRemove (N, argc, argv);
  }

  if (*argc != 4) usage ();

  // setup config information
  psMetadata *config = psMetadataAlloc ();

  // load config file 
  // psMetadataAdd (config, PS_LIST_HEAD, "PSF_MODEL", PS_META_MULTI, "folder for psf model entries", NULL);
  // config = psMetadataConfigRead(config, &Nfail, argv[3], FALSE);
  // fprintf (stderr, "loaded config...\n");

  // identify input image & optional weight & mask images
  // command-line entries override config-file entries
  psMetadataAdd (config, PS_LIST_HEAD, "IMAGE",       mode, "", argv[1]);
  psMetadataAdd (config, PS_LIST_HEAD, "OUTPUT_FILE", mode, "", argv[2]);

  if (mask != NULL) {
    psMetadataAdd (config, PS_LIST_HEAD, "MASK_IMAGE", mode, "", mask);
  }
  if (weight != NULL) {
    psMetadataAdd (config, PS_LIST_HEAD, "WEIGHT_IMAGE", mode, "", weight);
  }
  if (resid != NULL) {
    psMetadataAdd (config, PS_LIST_HEAD, "RESID_IMAGE", mode, "", resid);
  }
  return (config);
}

static void usage (void) {

    fprintf (stderr, "USAGE: psdemo (image) (output) (config)\n");
    fprintf (stderr, "options: \n");
    fprintf (stderr, "  -mask  (filename)\n");
    fprintf (stderr, "  -weight (filename)\n");
    fprintf (stderr, "  -resid (filename)\n");
    exit (2);
}
