Index: trunk/psModules/src/config/pmConfig.c
===================================================================
--- trunk/psModules/src/config/pmConfig.c	(revision 15180)
+++ trunk/psModules/src/config/pmConfig.c	(revision 15217)
@@ -229,4 +229,38 @@
 }
 
+// Read a file into a metadataItem, if required
+static bool metadataItemReadFile(psMetadataItem *item, // Item into which to read file
+                                 const char *description // Description, for error messages
+    )
+{
+    assert(item);
+    assert(description);
+
+    if (item->type == PS_DATA_METADATA) {
+        return true;                    // We've already read it
+    }
+    if (item->type != PS_DATA_STRING) {
+        psTrace("config", 2, "Element %s in %s metadata is not of type STR.\n",
+                item->name, description);
+        return false;
+    }
+
+    psTrace("config", 2, "Reading %s %s: %s\n", description, item->name, item->data.str);
+    psMetadata *new = NULL;         // New metadata
+    if (!pmConfigFileRead(&new, item->data.str, item->name)) {
+        psError(PM_ERR_CONFIG, false, "Trouble reading reading %s %s.\n",
+                description, item->name);
+        psFree(new);
+        return false;
+    }
+
+    // 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 = new;
+
+    return true;
+}
+
 // Read metadata config files in a metadata
 // The metadata contains file names, which will be replaced with the metadata that are in the files.
@@ -239,27 +273,9 @@
     psMetadataItem *item;               // Item from iteration
     while ((item = psMetadataGetAndIncrement(iter))) {
-        if (item->type == PS_DATA_METADATA) {
-            continue;       // We've already read it
-        }
-        if (item->type != PS_DATA_STRING) {
-           psTrace("config", 2, "Element %s in %s metadata is not of type STR.\n",
-                   item->name, description);
-           continue;
-        }
-
-        psTrace("config", 2, "Reading %s %s: %s\n", description, item->name, item->data.str);
-        psMetadata *new = NULL;         // New metadata
-        if (!pmConfigFileRead(&new, item->data.str, item->name)) {
-            psError(PM_ERR_CONFIG, false, "Trouble reading reading %s %s --- "
-                    "ignored.\n", description, item->name);
-            psFree(new);
+        if (!metadataItemReadFile(item, description)) {
+            psError(PM_ERR_CONFIG, false, "Unable to read %s %s.", description, item->name);
             psFree(iter);
             return false;
         }
-
-        // 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 = new;
     }
     psFree(iter);
@@ -694,4 +710,12 @@
             }
             psMetadata *camera = cameraItem->data.md; // Camera configuration
+
+            psMetadata *newRule = pmConfigFileRule(config, camera, new); // The rule of interest
+            if (!newRule) {
+                psWarning("Unable to find filerule %s in camera %s --- ignored.", new, cameraItem->name);
+                continue;
+            }
+
+            // By calling pmConfigFileRule, we've assured that the FILERULES is now a metadata
             psMetadata *filerules = psMetadataLookupMetadata(NULL, camera, "FILERULES"); // File rules
             if (!filerules) {
@@ -700,24 +724,5 @@
                 continue;
             }
-            psMetadataItem *newItem = psMetadataLookup(filerules, new); // The rule of interest, or a redir.
-            if (!newItem) {
-                psWarning("Can't find file rule %s in camera %s --- ignored.", new, cameraItem->name);
-                continue;
-            }
-            psMetadata *newRule;        // New rule (replacement)
-            switch (newItem->type) {
-              case PS_DATA_STRING:
-                // A redirection
-                newRule = psMetadataLookupMetadata(NULL, filerules, newItem->data.str);
-                break;
-              case PS_DATA_METADATA:
-                // The rule itself
-                newRule = newItem->data.md;
-                break;
-              default:
-                psWarning("Unable to find filerule %s of the correct type in camera %s --- ignored.",
-                          new, cameraItem->name);
-                continue;
-            }
+
             psMetadataAddMetadata(filerules, PS_LIST_TAIL, old, PS_META_REPLACE,
                                   "Original replaced by -F option", newRule);
@@ -1414,2 +1419,34 @@
     return psStringCopy(filename);
 }
+
+psMetadata *pmConfigFileRule(const pmConfig *config, const psMetadata *camera, const char *name)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+    PS_ASSERT_METADATA_NON_NULL(camera, NULL);
+    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
+
+    psMetadataItem *item = psMetadataLookup(camera, "FILERULES"); // Item with the file rule of interest
+    if (!item) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FILERULES in the camera configuration.");
+        return NULL;
+    }
+
+    if (!metadataItemReadFile(item, "file rules ")) {
+        psError(PM_ERR_CONFIG, false, "Unable to read file rules for camera.");
+        return NULL;
+    }
+
+    assert(item->type == PS_DATA_METADATA);
+    psMetadata *filerules = item->data.md; // File rules from the camera configuration
+
+    // 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);
+}
+
