# include "addstar.h"

int UpdateDatabase_Image (AddstarClientOptions *options, Image *images, int Nimages, Coords *mosaic, Stars *stars, int Nstars) {

  int i, status;
  Catalog catalog;
  SkyList *skylist, *newlist;

  if (options[0].mode != M_IMAGE) {
    fprintf (stderr, "error: expecting only IMAGE mode\n");
    return (FALSE);
  }

  catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
  if (options[0].update) catalog.catflags = LOAD_AVES | LOAD_MISS | LOAD_SECF;
  
  // XXX this is probably not needed anymore
  SetAirmassQuality (options[0].quality_airmass);

  /*** update catalog: average, measure, etc ***/

  /* find correpsonding regions for image */
  RegisterMosaic (mosaic);
  skylist = NULL;
  newlist = NULL;
  for (i = 0; i < Nimages; i++) {
      newlist = SkyListByImage (ServerSky, -1, &images[i]);
      SkyListMerge (&skylist, newlist);
      SkyListFree (newlist);
  }

  ImageOptions (options, images, Nimages);

  /* reduce regions to existing subset, if necessary */
  if (options[0].only_match || options[0].existing_regions) {
    SkyList *tmp;
    tmp = SkyListExistingSubset (skylist, CATDIR);
    SkyListFree (skylist);
    skylist = tmp;
  }
  if (VERBOSE) fprintf (stderr, "writing to %d regions\n", skylist[0].Nregions);

  for (i = 0; i < skylist[0].Nregions; i++) {

    // set the parameters which guide catalog open/load/create
    catalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
    catalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
    catalog.filename  = skylist[0].filename[i];
    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
    if (options[0].update) catalog.catflags = LOAD_AVES | LOAD_MISS | LOAD_SECF;

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

    // Naves_disk == 0 implies an empty catalog file
    // for only_match, skip empty catalogs 
    if ((catalog.Naves_disk == 0) && options[0].only_match) {
      dvo_catalog_unlock (&catalog);
      dvo_catalog_free (&catalog);
      continue;
    }

    if (options[0].closest) {
      find_matches_closest (skylist[0].regions[i], stars, Nstars, &catalog, options[0]);
    } else {
      find_matches (skylist[0].regions[i], stars, Nstars, &catalog, options[0]);
    }

    if (!options[0].only_images) {
      SetProtect (TRUE);
      if (options[0].update) {
	catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
	dvo_catalog_update (&catalog, VERBOSE);
      } else {
	dvo_catalog_save (&catalog, VERBOSE);
      }
      SetProtect (FALSE);
    }
    dvo_catalog_unlock (&catalog);
    dvo_catalog_free (&catalog);
  }

  if (options[0].calibrate) { FindCalibration (&images[0]); }
  
  /*** load image db, save new image ***/
  { 
    FITS_DB db;

    /*** update the image table ***/
    /* setup image table format and lock */
    db.mode   = dvo_catalog_catmode (CATMODE);
    db.format = dvo_catalog_catformat (CATFORMAT);
    status = dvo_image_lock (&db, ImageCat, 60.0, LCK_XCLD);
    if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);

    /* load or create the image table */
    if (db.dbstate == LCK_EMPTY) {
      if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
      dvo_image_create (&db, GetZeroPoint());
    } else {
      if (!dvo_image_load (&db, VERBOSE, FORCE_READ)) {
	Shutdown ("can't read image catalog %s", db.filename);
      }
    }

    /* write out new image */
    dvo_image_addrows (&db, images, Nimages);
    SetProtect (TRUE);
    dvo_image_update (&db, VERBOSE);
    SetProtect (FALSE);
    dvo_image_unlock (&db);
  }
  free (mosaic);
  free (images);
  free (stars);
  return (TRUE);
}
