# include "dvoshell.h"

Image *LoadImages (off_t *Nimage) {

  int status;
  char *catdir, filename[256];
  Image *image;
  FITS_DB db;
  
  /* VarConfig ("IMAGE_CATALOG", "%s", filename); */

  catdir = GetCATDIR ();
  sprintf (filename, "%s/Images.dat", catdir);

  gfits_db_init (&db);
  db.lockstate = LCK_SOFT;
  db.timeout   = 120.0;

  if (!gfits_db_lock (&db, filename)) {
    gprint (GP_ERR, "error opening image catalog %s (1)\n", filename);
    return (NULL);
  }

  if (db.dbstate == LCK_EMPTY) {
    gprint (GP_ERR, "note: image catalog is empty\n");
    ALLOCATE (image, Image, 1);
    *Nimage = 1;
    return (image);
  }

  status = dvo_image_load (&db, TRUE, FALSE);
  gfits_db_close (&db);

  if (!status) {
    gprint (GP_ERR, "problem loading image database table\n");
    return (NULL);
  }

  image = gfits_table_get_Image (&db.ftable, Nimage, &db.swapped);
  return (image);
}
