# 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);
    }
  }

  NphotcodesGroupA = 0;
  photcodesGroupA = NULL;
  if (PHOTCODE_A_LIST != NULL) {
    NPHOTCODES = 10;
    ALLOCATE (photcodesGroupA, PhotCode *, NPHOTCODES);

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

  NphotcodesGroupB = 0;
  photcodesGroupB = NULL;
  if (PHOTCODE_B_LIST != NULL) {
    NPHOTCODES = 10;
    ALLOCATE (photcodesGroupB, PhotCode *, NPHOTCODES);

    /* parse the comma-separated list of photcodesGroupB */
    list = PHOTCODE_B_LIST;
    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
      list = NULL; // pass NULL on successive strtok_r calls
      fprintf (stderr, "PHOTCODE_B_LIST: %s\n", PHOTCODE_B_LIST);
      fprintf (stderr, "codename: %s\n", codename);
      if ((photcodesGroupB[NphotcodesGroupB] = GetPhotcodebyName (codename)) == NULL) {
        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
        exit (1);
      }
      NphotcodesGroupB ++;
      CHECK_REALLOCATE (photcodesGroupB, PhotCode *, NPHOTCODES, NphotcodesGroupB, 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);
  }
}

