Index: /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c	(revision 14972)
+++ /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c	(revision 14973)
@@ -8,4 +8,5 @@
 #include <pslib.h>
 
+#include "pmErrorCodes.h"
 #include "pmConfig.h"
 #include "pmDetrendDB.h"
@@ -19,4 +20,77 @@
 #include "pmFPAConstruct.h"
 
+
+// Get the file rule of interest
+// Look up the name of the set of file rules to use, get that set from the site configuration, and return the
+// appropriate rule from the set.
+static psMetadata *getFileRule(const pmConfig *config, // Configuration
+                               const psMetadata *camera, // Camera configuration of interest
+                               const char *name // Name of rule to read
+    )
+{
+    assert(config);
+    assert(config->site);
+
+    const char *setName = psMetadataLookupStr(NULL, camera, "FILERULES"); // Name of file rules set
+    if (!setName) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FILERULES in the camera configuration.");
+        return NULL;
+    }
+
+    psMetadata *fileruleSets = psMetadataLookupMetadata(NULL, config->site,
+                                                        "FILERULES"); // Sets of defined file rules
+    if (!fileruleSets) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FILERULES in the site configuration.");
+        return NULL;
+    }
+
+    psMetadataItem *item = psMetadataLookup(fileruleSets, setName); // Item with the file rule of interest
+    if (!item) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FILERULES set %s in the SITE configuration.",
+                setName);
+        return NULL;
+    }
+
+    psMetadata *filerules = NULL;       // File rules from the site configuration
+    switch (item->type) {
+      case PS_DATA_METADATA:
+        // It's what we're after
+        filerules = item->data.md;
+        break;
+      case PS_DATA_STRING: {
+          // It's the name of a file --- read the file, and store it for future use
+          if (!pmConfigFileRead(&filerules, item->data.str, "filerules")) {
+              psError(PM_ERR_CONFIG, false, "Trouble reading reading file rules %s --- "
+                      "ignored.\n", setName);
+              psFree(filerules);
+              return NULL;
+          }
+
+          // Muck around under the hood to replace the filename with the metadata; don't try this at home,
+          // kids
+          item->type = PS_DATA_METADATA;
+          psFree(item->data.str);
+          item->data.md = filerules;
+          break;
+      }
+      default:
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                "Unexpected type for %s (%x) in FILERULES in SITE configuration.",
+                name, item->type);
+        return NULL;
+    }
+
+    // select the name from the FILERULES
+    // check for alias name (type == STR, name is aliased name)
+    bool mdok;                          // Status of MD lookup
+    const char *realname = psMetadataLookupStr(&mdok, filerules, name); // Name of file rule to look up
+    if (!realname || strlen(realname) == 0) {
+        realname = name;
+    }
+
+    return psMetadataLookupMetadata(NULL, filerules, realname);
+}
+
+
 // define an input-type pmFPAfile, bind to the optional fpa if supplied
 pmFPAfile *pmFPAfileDefineInput(const pmConfig *config, pmFPA *fpa, const char *name)
@@ -30,20 +104,7 @@
     char *type;
 
-    // select the FILERULES from config->camera
-    psMetadata *filerules = psMetadataLookupPtr (&status, config->camera, "FILERULES");
-    if (filerules == NULL) {
-        psError(PS_ERR_IO, true, "Can't find FILERULES in the CAMERA configuration!");
-        return NULL;
-    }
-
-    // select the name from the FILERULES
-    // check for alias name (type == STR, name is aliased name)
-    const char *realname = psMetadataLookupStr (&status, filerules, name);
-    if (!realname || strlen(realname) == 0) {
-        realname = name;
-    }
-
-    psMetadata *data = psMetadataLookupPtr (&status, filerules, realname);
-    if (data == NULL) {
+    const psMetadata *camera = (fpa ? fpa->camera : config->camera); // Camera configuration for this file
+    psMetadata *data = getFileRule(config, camera, name); // File rule
+    if (!data) {
         psError(PS_ERR_IO, true, "Can't find file rule %s!", name);
         return NULL;
@@ -76,7 +137,10 @@
     file->freeLevel = file->dataLevel;
 
-    if (fpa != NULL) {
+    if (fpa) {
         file->fpa = psMemIncrRefCounter(fpa);
         file->camera = psMemIncrRefCounter((psMetadata *)fpa->camera);
+        file->cameraName = psMemIncrRefCounter(config->cameraName); // XXX Is this the correct thing to do?
+    } else {
+        file->camera = psMemIncrRefCounter(config->camera);
         file->cameraName = psMemIncrRefCounter(config->cameraName);
     }
@@ -104,20 +168,34 @@
     bool status;
 
-    // select the FILERULES from the camera config
-    psMetadata *filerules = psMetadataLookupPtr(&status, config->camera, "FILERULES");
-    if (filerules == NULL) {
-        psError(PS_ERR_IO, true, "Can't find FILERULES in the CAMERA configuration!");
-        return NULL;
-    }
-
-    // select the name from the FILERULES
-    // check for alias name (type == STR, name is aliased name)
-    const char *realname = psMetadataLookupStr(&status, filerules, name);
-    if (!realname || strlen(realname) == 0) {
-        realname = name;
-    }
-
-    psMetadata *data = psMetadataLookupPtr (&status, filerules, realname);
-    if (data == NULL) {
+    // Use the camera we were told to, the camera of the provided FPA, or default to the default camera
+    psMetadata *camera;                 // Camera configuration
+    if (!cameraName || strlen(cameraName) == 0) {
+        if (fpa && fpa->camera) {
+            camera = (psMetadata*)fpa->camera; // Casting away const, so I can put it in the file
+        } else {
+            camera = config->camera;
+            cameraName = config->cameraName;
+        }
+    } else {
+        bool mdok;                      // Status of MD lookup
+        psMetadata *cameras = psMetadataLookupMetadata(&mdok, config->site, "CAMERAS"); // Known cameras
+        if (!mdok || !cameras) {
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find CAMERAS in the site configuration.\n");
+            return NULL;
+        }
+        camera = psMetadataLookupMetadata(&mdok, cameras, cameraName); // Camera configuration of interest
+        if (!mdok || !camera) {
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find automatically generated "
+                    "camera configuration %s in site configuration.\n", cameraName);
+            return NULL;
+        }
+
+        if (fpa && fpa->camera && fpa->camera != camera) {
+            psAbort("Camera of bound FPA is not the requested camera --- there is an inconsistency!");
+        }
+    }
+
+    psMetadata *data = getFileRule(config, camera, name); // File rule
+    if (!data) {
         psError(PS_ERR_IO, true, "Can't find file rule %s!", name);
         return NULL;
@@ -158,31 +236,4 @@
 # endif
 
-    // Use the camera we were told to, the camera of the provided FPA, or default to the default camera
-    psMetadata *camera;                 // Camera configuration
-    if (!cameraName || strlen(cameraName) == 0) {
-        if (fpa && fpa->camera) {
-            camera = (psMetadata*)fpa->camera; // Casting away const, so I can put it in the file
-        } else {
-            camera = config->camera;
-            cameraName = config->cameraName;
-        }
-    } else {
-        bool mdok;                      // Status of MD lookup
-        psMetadata *cameras = psMetadataLookupMetadata(&mdok, config->site, "CAMERAS"); // Known cameras
-        if (!mdok || !cameras) {
-            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find CAMERAS in the site configuration.\n");
-            return NULL;
-        }
-        camera = psMetadataLookupMetadata(&mdok, cameras, cameraName); // Camera configuration of interest
-        if (!mdok || !camera) {
-            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find automatically generated "
-                    "camera configuration %s in site configuration.\n", cameraName);
-            return NULL;
-        }
-
-        if (fpa && fpa->camera && fpa->camera != camera) {
-            psAbort("Camera of bound FPA is not the requested camera --- there is an inconsistency!");
-        }
-    }
     file->camera = psMemIncrRefCounter(camera);
     file->cameraName = psMemIncrRefCounter(cameraName);
