# include "addstar.h"

Stars *LoadStars (char *filename, unsigned int *Nstars, Image **images, off_t *Nimages, AddstarClientOptions *options) {

  off_t *extsize, NheaderSets;
  int i, Nfile, NFILE, Nheaders, mode;
  char **file, line[1024];
  FILE *f;
  glob_t globList;
  Header **headers;
  Stars *stars;
  HeaderSet *headerSets;

  if (options[0].filelist) {
    // read the list of input files from the supplied file
    f = fopen (filename, "r");
    if (f == NULL) {
      fprintf (stderr, "can't read input list %s, giving up\n", filename);
      exit (1);
    }

    NFILE = 10;
    ALLOCATE (file, char *, NFILE);
    for (i = 0; (fscanf (f, "%s", line) != EOF); i++) {
      // filename limited to 1024 chars
      fprintf (stderr, "file: %s\n", line);
      file[i] = strcreate (line);
      if (i == NFILE - 1) {
	NFILE += 10;
	REALLOCATE (file, char *, NFILE);
      }
    }
    Nfile = i;
  } else {
    // parse the filename as a glob
    globList.gl_offs = 0;
    glob (filename, 0, NULL, &globList);

    // if the glob does not match, save the literal word:
    // otherwise save all glob matches
    if (globList.gl_pathc == 0) {
      Nfile = 1;
      ALLOCATE (file, char *, Nfile);
      file[0] = strcreate (filename);
    } else {
      Nfile = globList.gl_pathc;
      ALLOCATE (file, char *, Nfile);
      for (i = 0; i < Nfile; i++) {
	file[i] = strcreate (globList.gl_pathv[i]);
      }
    }
  }

  *Nimages = 0;
  *Nstars = 0;
  *images = NULL;
  stars = NULL;

  for (i = 0; i < Nfile; i++) {
    f = fopen (file[i], "r");
    if (f == NULL) {
      fprintf (stderr, "can't read file %s, skipping\n", file[i]);
      continue;
    }

    // load PMM data if specified (these are not stored as FITS-tables)
    if (PMM_CCD_TABLE != NULL) {
      LoadDataPMM (f, file[i], images, Nimages, &stars, Nstars);
      continue;
    }

    // otherwise, we have FITS-table files: parse their headers to determine the contents
    headers = LoadHeaders (f, &mode, &Nheaders);
    headerSets = MatchHeaders (&extsize, &NheaderSets, mode, headers, Nheaders);
    if (headerSets == NULL) {
      fprintf (stderr, "ERROR: can't read headers for %s\n", file[i]);
      continue;
    }
    if (NheaderSets == 0) {
      fprintf (stderr, "no object data in file %s, skipping\n", file[i]);
      continue;
    }
    if (VERBOSE) fprintf (stderr, "file %s has %d headers, including %lld images\n", file[i], Nheaders, (long long) NheaderSets);

    /* supplied photcode is incompatible with multi-chip images */
    if ((NheaderSets > 1) && options[0].photcode) {
      fprintf (stderr, "ERROR: photcode cannot be supplied to multi-chip images -- manually adjust the headers\n");
      exit (1);
    }

    // if these are SDSS data, load with SDSS-specific wrapper
    if (headerSets[0].exttype && !strcmp (headerSets[0].exttype, "SDSS_OBJ")) {
      LoadDataSDSS (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets);
      continue;
    }

    LoadData (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets, options);

    // XXX add a function to (optionally) load the extended source measurements
    # if (0)
    if (extSources) {
      // not sure how to link the measurements here to the psf measurements above (though there is an ID in the det list)
      LoadDataXSRC (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets);
    }
    if (extFits) {
      LoadDataXFIT (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets);
    }
    # endif

  }

  if (*Nimages == 0) {
    if (Nfile == 1) 
      fprintf (stderr, "no valid image data in any of these files, giving up\n");
    else 
      fprintf (stderr, "no valid image data in this file, giving up\n");
    exit (0);
  }

  return stars;
}

