# include "delstar.h"

void delete_imagefile (FITS_DB *db, char *filename) {

  int i, Nimlist;
  int *imlist;
  double trange;
  time_t start, stop;
  Image *image;
  Catalog catalog;
  SkyList *skylist;
  SkyTable *sky;

  /* load sky from correct table */
  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
  SkyTableSetFilenames (sky, CATDIR, "cpt");

  /* load information about file - time/photcode */
  image = gimages (filename);
  
  /* need to define method to get the mosaic (look up from table) */
  if (VERBOSE) fprintf (stderr, "deleting %s\n", image[0].name);

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

    if (VERBOSE) fprintf (stderr, "deleting from %s\n", skylist[0].filename[i]);
    catalog.filename = skylist[0].filename[i];  /* don't free region before catalog! */
    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;

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

    /* pad exposure time by 1 sec to require a valid time */
    /* trate is in 0.1 msec / row  - stop is the latest exposure end time */
    trange = 1e-4*image[0].NY*image[0].trate + image[0].exptime + 1;  
    start = image[0].tzero;
    stop  = image[0].tzero + trange;
    find_matches (&catalog, image[0].source, start, stop);

    dvo_catalog_save (&catalog, VERBOSE);
    dvo_catalog_unlock (&catalog);
    dvo_catalog_free (&catalog);
  }

  /* find and delete matching images */
  // XXX EAM : load image data above, find mosaic?
  imlist = find_images_data (db, image, &Nimlist);
  if (!Nimlist) Shutdown ("image %s not found in db", filename);

  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, imlist, Nimlist);
  dvo_image_update (db, VERBOSE);
  dvo_image_unlock (db);

  fprintf (stderr, "SUCCESS\n");
  exit (0);
}
