# include "getstar.h"

main (int argc, char **argv) {

  int i, Nstars, Nregions;
  Image *image, *gimages(), *gtimes(), *find_images();
  Catalog catalog;
  GSCRegion *region, *tregion, *gregions();
  Stars *stars;
  
  args (argc, argv);

  image = gimages (argv[1]);

  region = gregions (&image[0], &Nregions);

  stars = (Stars *) NULL;
  Nstars = 0;

  for (i = 0; i < Nregions; i++) {
    if (VERBOSE) fprintf (stderr, "extracting from %s\n", region[i].filename);
    catalog.filename = region[i].filename;  /* don't free region before catalog! */
    switch (lock_catalog (&catalog, LCK_SOFT)) {
    case 0:
      fprintf (stderr, "ERROR: can't lock file\n");
      exit (1);
    case 1:
      gcatalog (&catalog);
      break;
    case 2:
      fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
      catalog.Naverage = 0;
      catalog.Nmeasure = 0;
      break;
    default:
      fprintf (stderr, "weird lock_catalog exit state\n");
      exit (1);
    }
    unlock_catalog (&catalog);
    stars = find_matches (&catalog, &image[0], 0, 0, stars, &Nstars);
  }

  /* write out the selected stars */
  clean_stars (stars, &Nstars, image);
  wstars (argv[2], stars, Nstars, image);
  fprintf (stderr, "SUCCESS\n");
  exit (0);

}
