# include "relastro.h"

void initialize (int argc, char **argv) {

  int NPHOTCODES;
  char *codename, *ptr, *list;

  ptr = NULL;

  ConfigInit (&argc, argv);
  args (argc, argv);

  /* build a list of accepted photcodes. these will be used by bcatalog to accept or
     reject loaded data */

  NphotcodesKeep = 0;
  photcodesKeep = NULL;
  if (PHOTCODE_KEEP_LIST != NULL) {
    NPHOTCODES = 10;
    ALLOCATE (photcodesKeep, PhotCode *, NPHOTCODES);

    /* parse the comma-separated list of photcodesKeep */
    list = PHOTCODE_KEEP_LIST;
    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
      list = NULL; // pass NULL on successive strtok_r calls
      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_KEEP_LIST);
      fprintf (stderr, "codename: %s\n", codename);
      if ((photcodesKeep[NphotcodesKeep] = GetPhotcodebyName (codename)) == NULL) {
        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
        exit (1);
      }
      NphotcodesKeep ++;
      CHECK_REALLOCATE (photcodesKeep, PhotCode *, NPHOTCODES, NphotcodesKeep, 10);
    }
  }

  NphotcodesSkip = 0;
  photcodesSkip = NULL;
  if (PHOTCODE_SKIP_LIST != NULL) {
    NPHOTCODES = 10;
    ALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES);

    /* parse the comma-separated list of photcodesSkip */
    list = PHOTCODE_SKIP_LIST;
    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
      list = NULL; // pass NULL on successive strtok_r calls
      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_SKIP_LIST);
      fprintf (stderr, "codename: %s\n", codename);
      if ((photcodesSkip[NphotcodesSkip] = GetPhotcodebyName (codename)) == NULL) {
        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
        exit (1);
      }
      NphotcodesSkip ++;
      CHECK_REALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES, NphotcodesSkip, 10);
    }
  }

  initstats (STATMODE);

  // IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
  // STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
  // MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_ASTROM | ID_MEAS_SKIP_ASTROM | ID_MEAS_AREA;

  /* XXX drop irrelevant entries */
  if (SHOW_PARAMS) {
    fprintf (stderr, "current parameter settings:\n");
    if (TimeSelect) {
      fprintf (stderr, "TimeSelect: TRUE (%s - %s)\n", ohana_sec_to_date (TSTART), ohana_sec_to_date (TSTOP));
    } else {
      fprintf (stderr, "TimeSelect: FALSE\n");
    }
    fprintf (stderr, "VERBOSE: %d, PLOTSTUFF: %d\n", VERBOSE, PLOTSTUFF);

    fprintf (stderr, "IMAGE_CATALOG          %s\n",  ImageCat);
    fprintf (stderr, "GSCFILE                %s\n",  GSCFILE);
    fprintf (stderr, "CATDIR                 %s\n",  CATDIR);
    exit (0);
  }
}

