Index: /trunk/Ohana/src/imregister/src/imstatreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3645)
+++ /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3646)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imstatreg $Revision: 3.9 $";
+static char *version = "imstatreg $Revision: 3.10 $";
 
 int main (int argc, char **argv) {
@@ -8,5 +8,5 @@
   int *match, Nmatch, status, Nentry;
   FILE *f;
-  FITS_DB db;
+  FITS_DB image_db, temp_db;
 
   get_version (argc, argv, version);
@@ -19,13 +19,15 @@
 
   /* fork in background */
-  if (fork () == -1) {
-      fprintf (stderr, "error forking imstatreg -daemon \n");
-      exit (1);
-    }
+  child = fork ();
+  if (child == -1) {
+    fprintf (stderr, "error forking imstatreg -daemon \n");
+    exit (1);
+  } 
+  if (child !=  0) {
     fprintf (stderr, "starting imstatreg, logging to %s\n", LogFile);
     exit (0);
   }
 
-  /* child process, check for process */
+  /* child process, check for previous process */
   ConfigPID (PIDFILE);
 
@@ -39,7 +41,11 @@
   }
 
-  db.lockstate = LCK_HARD;
-  db.timeout   = 300.0;
-  fits_db_init (&db);
+  image_db.lockstate = LCK_HARD;
+  image_db.timeout   = 300.0;
+  fits_db_init (&image_db);
+
+  temp_db.lockstate = LCK_HARD;
+  temp_db.timeout   = 300.0;
+  fits_db_init (&temp_db);
 
   /* start loop */
@@ -47,42 +53,65 @@
 
     /* check / load / delete temporary database */
-    if (!fits_db_lock (&db, TempDB)) {
-      fits_db_close (&db);
-      fprintf (stderr, "error locking db (path missing? access permission?)\n");
+    if (!fits_db_lock (&temp_db, TempDB)) {
+      fits_db_close (&temp_db);
+      fprintf (stderr, "error locking temp db (path missing? access permission?)\n");
       exit (1);
     }
-    if (db.dbstate == LCK_EMPTY) {
+    if (temp_db.dbstate == LCK_EMPTY) {
       fprintf (stderr, "temporary database empty\n");
       goto next;
     } 
-    if (!fits_db_load (&db)) {
+    if (!fits_db_load (&temp_db)) {
       fprintf (stderr, "error reading temp db\n");
       goto next;
     }
-    image = fits_table_get_RegImage (&db.ftable, &Nimage);
+    image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
     fprintf (stderr, "temporary database read\n");
 
-    /* need to zero out this file */
+    /* delete and unlock temporary database */
+    fits_db_close (&temp_db);
     fprintf (stderr, "temporary database closed\n");
  
-    /******* need to finish this section up *****/
     /* check / load main database */
-    set_db (ImageDB);
-    status = load_db ();
-    if (!status) {
-      print_db_status ("main database empty");
-      close_db (); 
+    if (!fits_db_lock (&image_db, ImageDB)) {
+      fits_db_close (&image_db);
+      fprintf (stderr, "error locking image db (path missing? access permission?)\n");
+      exit (1);
+    }
+    if (temp_db.dbstate == LCK_EMPTY) {
+      fprintf (stderr, "main database empty\n");
+      fits_db_close (&image_db);
+
       /* add temp data back to temp database */
-      print_db_status ("resave temporary data");
-      set_db (TempDB);
-      status = load_db ();
-      if (!status) {
-	print_db_status ("create new temporary database");
-	create_db ();
+      fprintf (stderr, "resave temporary data\n");
+      if (!fits_db_lock (&temp_db, TempDB)) {
+	fits_db_close (&temp_db);
+	fprintf (stderr, "error locking temp db (path missing? access permission?)\n");
+	exit (1);
       }
-      append_db (image, Nentry);
+      if (temp_db.dbstate == LCK_EMPTY) {
+	fprintf (stderr, "temporary database empty\n");
+	goto next;
+      } 
+      if (!fits_db_load (&temp_db)) {
+	fprintf (stderr, "error reading temp db\n");
+	goto next;
+      }
+      image = fits_table_get_RegImage (&temp_db.ftable, &Nimage);
+      fprintf (stderr, "temporary database read\n");
+
+      fits_convert_Spectrum (spectrum, sizeof (Spectrum), 1);
+      fits_table_to_vtable (&db.ftable, &vtable, 0, 0);
+      fits_vadd_rows (&vtable, (char *) spectrum, 1, sizeof(Spectrum));
+
+      fits_db_update (&db, &vtable);
+      fits_db_close (&db);
+      fits_db_free (&db);
+
       print_db_status ("temporary database resaved");
       goto next;
     }
+
+    /************* this is kind of convoluted, need to simplify a bit ****/
 
     /* match temp image with main images / update main DB */
@@ -104,2 +133,21 @@
   }
 }
+
+/* basic outline:
+
+   - lock temp db
+     - if empty, continue
+   - load temp db
+   - clear temp db
+
+   - lock image db
+     - if empty, resave temp db
+
+   - load image db
+   - match temp and main 
+
+   - update image db
+
+   - unlock temp db
+*/
+
