# include "sedstar.h"

int main (int argc, char **argv) {

  char *root, *ext, tmp;
  int i, Nbytes;
  SkyList *skylist;
  SkyTable *sky;
  AddstarClientOptions options;
  Catalog incatalog, outcatalog;
  SEDtable *sedtable;

  // need to construct these options with args_load2mass...
  options = ConfigInit (&argc, argv);
  options = args_sedstar (argc, argv, options);

  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
  SkyTableSetFilenames (sky, CATDIR, "cpt");
  
  // select regions of interest
  skylist = SkyListByPatch (sky, -1, &UserPatch);

  // load the SED data table
  sedtable = SEDtableLoad (argv[1]);

  for (i = 0; i < skylist[0].Nregions; i++) {
    incatalog.filename = skylist[0].filename[i];
    incatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;

    // an error exit status here is a significant error
    if (!dvo_catalog_open (&incatalog, skylist[0].regions[i], VERBOSE, "r")) {
      fprintf (stderr, "ERROR: failure to open/create catalog file %s\n", incatalog.filename);
      exit (2);
    }

    // Naves_disk == 0 implies an empty catalog file
    if ((incatalog.Naves_disk == 0) && options.only_match) {
      if (VERBOSE) fprintf (stderr, "skipping empty region\n");
      dvo_catalog_unlock (&incatalog);
      dvo_catalog_free (&incatalog);
      continue;
    }

    // create output catalog filename
    root = strstr (incatalog.filename, CATDIR);
    if (root == NULL) Shutdown ("error with input catalog name");
    ext = incatalog.filename + strlen(CATDIR);
    while (*ext == '/') ext++;
    Nbytes = snprintf (&tmp, 0, "%s/%s", argv[2], ext);
    ALLOCATE (outcatalog.filename, char, Nbytes + 1);
    snprintf (outcatalog.filename, Nbytes + 1, "%s/%s", argv[2], ext);

    outcatalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
    outcatalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
    outcatalog.Nsecfilt  = GetPhotcodeNsecfilt ();
    outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;

    // an error exit status here is a significant error
    if (!dvo_catalog_open (&outcatalog, skylist[0].regions[i], VERBOSE, "w")) {
      Shutdown ("ERROR: failure to open/create catalog file %s\n", outcatalog.filename);
    }

    SEDfitCatalog (&outcatalog, &incatalog, sedtable);
    
    dvo_catalog_save (&outcatalog, VERBOSE);
    dvo_catalog_unlock (&outcatalog);
    dvo_catalog_free (&outcatalog);

    dvo_catalog_unlock (&incatalog);
    dvo_catalog_free (&incatalog);
    // XXX free filename or not?
  }
  exit (0);
}  

/**  sedstar: 

* load in the SED data table
* load in the catalog file (by region)
* fit stars in the catalog file
* load output catalog file?
* construct output catalog file (optional)
* save output catalog file

**/
