Index: /trunk/Ohana/src/imregister/detrend/altpath.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/altpath.c	(revision 123)
+++ /trunk/Ohana/src/imregister/detrend/altpath.c	(revision 124)
@@ -7,49 +7,11 @@
   int Nimage, Nlist, Ncurrent, Nadd, Nremove;
   int *list, *current, *curfound, *add, *remove;
-  char *dBPath, input[256], output[256], line[1024];
+  char *dBPath, infile[256], outfile[256], line[1024];
   DetReg *image;
-  
+  struct stat statbuf;
+
   image = get_images (&Nimage);
   ALLOCATE (current, int, Nimage);
 
-  /* identify current altpath images */
-  Ncurrent = 0;
-  for (i = 0; i < Nimage; i++) {
-    if (image[i].altpath) {
-      current[Ncurrent] = i;
-      Ncurrent++; 
-    }
-  }
-
-  /* extract 'remove' and 'add' entries from current & match */
-  ALLOCATE (remove, int, Nimage);
-  ALLOCATE (add, int, Nimage);
-  
-  ALLOCATE (curfound, int, Ncurrent);
-  bzero (curfound, Ncurrent*sizeof(int));
-
-  Nadd = Nremove = 0;
-  for (i = 0; i < Nmatch; i++) {
-    found = FALSE;
-    for (j = 0; !found && (j < Ncurrent); j++) {
-      if (match[i].image == current[j]) {
-	found = TRUE;
-	curfound[j] = TRUE;
-      }
-    }
-    if (!found) {
-      add[Nadd] = match[i].image;
-      Nadd ++;
-    }
-  }
-  for (i = 0; i < Ncurrent; i++) {
-    if (!curfound[i]) {
-      remove[Nremove] = current[i];
-      Nremove ++;
-    }
-  }
-  free (current);
-  free (curfound);
-  
   ALLOCATE (list, int, Nimage);
   Nlist = 0;
@@ -58,51 +20,90 @@
 
   /* set altpath for 'add' images */
-  for (j = 0; j < Nadd; j++) {
-    i = add[j];
-    image[i].altpath = TRUE;
-    list[Nlist] = i;
-    Nlist ++;
-    for (n = 0; n < NDetrendAltDB; n++) {
-      sprintf (input, "%s/%s", dBPath, image[i].filename);
-      sprintf (output, "%s/%s", DetrendAltDB[n], image[i].filename);
-      status = ckpathname (output);
-      if (!status) {
-	remove[Nremove] = i;
-	Nremove ++;
-	break;
-      }
-      sprintf (line, "cp %s %s", input, output);
-      fprintf (stderr, "%s\n", line);
-      status = system (line);
-      if (status) {
-	remove[Nremove] = i;
-	Nremove ++;
-	break;
+  if (output.Altpath == ADD) {
+    /* find images to add */
+    for (i = 0; i < Nmatch; i++) {
+      if (image[match[i].image].altpath == FALSE) {
+	list[Nlist] = match[i].image;
+	Nlist ++;
       }
     }
+    /* copy the masters to the altpath locations */
+    for (j = 0; j < Nlist; j++) {
+      i = list[j];
+      for (n = 0; n < NDetrendAltDB; n++) {
+	sprintf (infile, "%s/%s", dBPath, image[i].filename);
+	sprintf (outfile, "%s/%s", DetrendAltDB[n], image[i].filename);
+	status = ckpathname (outfile);
+	if (!status) {
+	  fprintf (stderr, "warning: can't make outfile directory for %s\n", outfile);
+	  continue;
+	}
+	sprintf (line, "cp %s %s", infile, outfile);
+	fprintf (stderr, "%s\n", line);
+	status = system (line);
+	if (status) {
+	  fprintf (stderr, "warning: can't make outfile directory for %s\n", outfile);
+	  continue;
+	}
+      }
+      image[i].altpath = TRUE;
+    }
+    update_db (list, Nlist);
+    close_db ();
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
   }
 
-  /* unset altpath for 'remove' images */
-  for (j = 0; j < Nremove; j++) {
-    i = remove[j];
-    image[i].altpath = FALSE;
-    list[Nlist] = i;
-    Nlist ++;
-    for (n = 0; n < NDetrendAltDB; n++) {
-      sprintf (output, "%s/%s", DetrendAltDB[n], image[i].filename);
-      sprintf (line, "rm %s", output);
-      fprintf (stderr, "%s\n", line);
-      status = system (line);
-      if (status) {
-	fprintf (stderr, "warning: can't delete %s\n", output);
+  /* unset altpath for 'delete' images */
+  if (output.Altpath == DELETE) {
+    /* find images to delete */
+    for (i = 0; i < Nmatch; i++) {
+      if (image[match[i].image].altpath == TRUE) {
+	list[Nlist] = match[i].image;
+	Nlist ++;
       }
     }
+    /* remove the copies from the altpath locations */
+    for (j = 0; j < Nlist; j++) {
+      i = list[j];
+      image[i].altpath = FALSE;
+      for (n = 0; n < NDetrendAltDB; n++) {
+	sprintf (outfile, "%s/%s", DetrendAltDB[n], image[i].filename);
+	sprintf (line, "rm %s", outfile);
+	fprintf (stderr, "%s\n", line);
+	status = system (line);
+	if (status) {
+	  fprintf (stderr, "warning: can't delete %s\n", outfile);
+	}
+      }
+    }
+    update_db (list, Nlist);
+    close_db ();
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
   }
 
-  update_db (list, Nlist);
-  close_db ();
-  fprintf (stderr, "SUCCESS\n");
-  exit (0);
+  /* check for existence in altpath, set flag as appropriate */
+  if (output.Altpath == UPDATE) {
+    for (j = 0; j < Nmatch; j++) {
+      i = match[j].image;
+      list[Nlist] = i;
+      Nlist ++;
+      found = TRUE;
+      for (n = 0; found && (n < NDetrendAltDB); n++) {
+	sprintf (outfile, "%s/%s", DetrendAltDB[n], image[i].filename);
+	status = stat (outfile, &statbuf);
+	fprintf (stderr, "checking for %s, status is %d\n", outfile, status);
+	if (status == -1) found = FALSE;
+      }
+      image[i].altpath = found;
+    }
+    update_db (list, Nlist);
+    close_db ();
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+  }
 
+  fprintf (stderr, "unknown altpath mode: %d\n", output.Altpath);
 }
 
@@ -131,2 +132,3 @@
   return (TRUE);
 }
+
