# include "addstar.h"

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

  FILE *f;
  int i, Nstars, Nimage, Nregions, Nmissed;
  Stars *stars, *gstars();
  Image image, *pimage, *gimages();
  Catalog catalog;
  GSCRegion *region, *gregions();
  struct timeval now, then;  
  
  gettimeofday (&then, (void *) NULL);
  ConfigInit (argc, argv);

  VERBOSE = FALSE;
  if ((i = get_argument (argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (i, &argc, argv);
  }
  if (argc < 2) {
    fprintf (stderr, "ERROR: Usage: addstar filename\n");
    exit (0);
  }

  /* if lockfile exists, program will complain and quit */
  check_lockfile ();

  stars = gstars (argv[1], &Nstars, &image);

  pimage = gimages (&image, &Nimage);
  region = gregions (&image, &Nregions);

  for (i = 0; i < Nregions; i++) {
    gcatalog (&region[i], &catalog);
    find_matches (&region[i], stars, Nstars, &catalog, &image, pimage, Nimage);
    wcatalog (&region[i], &catalog);
  }
  
  for (Nmissed = i = 0; i < Nstars; i++) {
    if (stars[i].found == -1) {
      fprintf (stderr, "%d %f %f %f %f\n", i, stars[i].R, stars[i].D, stars[i].M, stars[i].dM);
      Nmissed ++;
    }
  }
  if (Nmissed) fprintf (stderr, "WARNING: %d stars in image were missed!\n", Nmissed);

  wimage (argv[1], &image, Nstars);

  if (VERBOSE) {
    gettimeofday (&now, (void *) NULL);
    fprintf (stderr, "%s: elapsed time = %.2f sec\n", argv[1], 
	     (now.tv_sec - then.tv_sec) + 1e-6*(now.tv_usec - then.tv_usec));
  }
  clear_lockfile ();
  fprintf (stderr, "SUCCESS\n");
}


  /* stars within a file are not all within 1 region file:
     1) check if unfound stars are in current region 
     2) if not, keep unfound for next pass */


/* input star lists should be adjusted so 1 e/sec is mag 25.000 */ 


