# include "setphot.h"

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

  off_t Nimage;
  int status, Nzpts;
  FITS_DB db;
  ZptTable *zpts;
  Image *image;
  FlatCorrectionTable flatcorrTable;

  /* get configuration info, args, lockfile */
  initialize_setphot (argc, argv);

  set_db (&db);
  status = dvo_image_lock (&db, ImageCat, 60.0, (UPDATE ? LCK_XCLD : LCK_SOFT));
  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
  if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);

  if (UBERCAL) {
    zpts = load_zpt_ubercal (argv[1], &Nzpts, &flatcorrTable);

    char flatcorrfile[DVO_MAX_PATH];
    int size = snprintf (flatcorrfile, DVO_MAX_PATH, "%s/flatcorr.fits", CATDIR);
    assert (size < DVO_MAX_PATH);
    FlatCorrectionSave(&flatcorrTable, flatcorrfile);
    // XXX should this program save any old copy of this file?
  } else {
    zpts = load_zpt_table (argv[1], &Nzpts);
  }

  if (!zpts) Shutdown ("failed to load zero points, or empty table");

  // load images 
  image  = load_images_setphot (&db, &Nimage);
  if (!UPDATE) dvo_image_unlock (&db); 
  
  match_zpts_to_images (image, Nimage, zpts, Nzpts);

  if (UBERCAL) {
    match_flatcorr_to_images (image, Nimage, &flatcorrTable);
  } 

  if (!IMAGES_ONLY) {
    status = update_dvo_setphot (image, Nimage, &flatcorrTable);
  }

  // write image table (even if some remote clients failed)
  if (UPDATE) {
    dvo_image_save (&db, VERBOSE);
  }

  if (!status) exit (1);
  exit (0);
}
  

/* setphot : set the zero points for images in the db (perhaps based on external information)
   setphot (zpt_table)

   setphot has 2 modes : basic & ubercal

   * outline of basic mode:

   ** load text table of zpts, time (photcode?)
   ** load images
   ** match images to zpts
   ** set zpts
   ** load catalogs
   ** update detection (& averages?)

   * outline of ubercal mode:

   ** load fits table of zpts & time + table of flat-field corrections
   ** load images
   ** match images to zpts
   ** set zpts
   ** load catalogs
   ** update detection (& averages?)

 */

