Index: src/apache-2/mod_rivet.c
===================================================================
--- src/apache-2/mod_rivet.c	(revision 590002)
+++ src/apache-2/mod_rivet.c	(working copy)
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <string.h>
 
+/* as long as we need to emulate ap_chdir_file we need to include unistd.h */
+#include <unistd.h>
+
 /* Apache includes */
 #include <httpd.h>
 #include <http_config.h>
@@ -255,7 +258,37 @@
     Tcl_DecrRefCount(obj);
 }
 
+/*
+ * Experimental function that sets up an array with some of the request_rec fields
+ * as they are passed to the handler.
+ *
+ */
+
 static void
+Rivet_InitRequestVariables (Tcl_Interp *interp, request_rec *r)
+{	
+    Tcl_Obj *obj;
+
+    obj = Tcl_NewStringObj(r->filename,-1);
+    Tcl_IncrRefCount(obj);
+    Tcl_SetVar2Ex(interp,
+            "request",
+            "filename",
+            obj,
+            TCL_GLOBAL_ONLY);
+    Tcl_DecrRefCount(obj);
+    
+    obj = Tcl_NewStringObj(r->canonical_filename,-1);
+    Tcl_IncrRefCount(obj);
+    Tcl_SetVar2Ex(interp,
+            "request",
+            "canonical_filename",
+            obj,
+            TCL_GLOBAL_ONLY);
+    Tcl_DecrRefCount(obj);
+}
+
+static void
 Rivet_PropagateServerConfArray( Tcl_Interp *interp, rivet_server_conf *rsc )
 {
     apr_table_t *t;
@@ -412,8 +445,9 @@
             delEntry = Tcl_FindHashEntry(
                     rsc->objCache,
                     rsc->objCacheList[ct]);
-            if (delEntry != NULL)
+            if (delEntry != NULL) {
                 Tcl_DecrRefCount((Tcl_Obj *)Tcl_GetHashValue(delEntry));
+	    }
             Tcl_DeleteHashEntry(delEntry);
 
             free(rsc->objCacheList[ct]);
@@ -672,17 +706,14 @@
     }
 }
 
-
-
-
 /* Function to get a config and merge the directory/server options  */
 rivet_server_conf *
 Rivet_GetConf( request_rec *r )
 {
-    rivet_server_conf *newconfig = NULL;
     rivet_server_conf *rsc = RIVET_SERVER_CONF( r->server->module_config );
-    rivet_server_conf *rdc;
     void *dconf = r->per_dir_config;
+    //rivet_server_conf *newconfig = NULL;
+    //rivet_server_conf *rdc;
     //int tst;
     
     FILEDEBUGINFO;
@@ -691,9 +722,8 @@
     if (dconf == NULL) {
         return rsc;
     }
-
     return rsc;
-
+/*
     rdc = RIVET_SERVER_CONF( dconf );
     
     newconfig = RIVET_NEW_CONF( r->pool );
@@ -703,6 +733,7 @@
     Rivet_MergeDirConfigVars( r->pool, newconfig, rsc, rdc );
 
     return newconfig;
+ */
 }
 
 static void *
@@ -805,8 +836,6 @@
     Tcl_DecrRefCount(arrayName);
 }
 
-
-
 /*
  *-----------------------------------------------------------------------------
  *
@@ -849,6 +878,16 @@
 
     /* Eval Rivet's init.tcl file to load in the Tcl-level
        commands. */
+
+    /* We want to run the init.tcl specific to the installation
+     * that is being carried out
+     */
+
+    Tcl_EvalFile(interp,RIVETLIB_DESTDIR"/init.tcl");
+
+//  ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, s, "auto_path: %s",
+//            Tcl_GetVar(interp, "auto_path", TCL_GLOBAL_ONLY));
+
     if (Tcl_PkgRequire(interp, "RivetTcl", "1.1", 1) == NULL) {
         ap_log_error( APLOG_MARK, APLOG_ERR, APR_EGENERAL, s,
                 "init.tcl must be installed correctly for Apache Rivet to function: %s",
@@ -1048,7 +1087,11 @@
 Rivet_InitHandler(apr_pool_t *pPool, apr_pool_t *pLog, apr_pool_t *pTemp,
        server_rec *s)
 {
+#ifdef HIDE_RIVET_VERSION
     ap_add_version_component(pPool, "Rivet");
+#else
+    ap_add_version_component(pPool, "Rivet/"VERSION);
+#endif
     return OK;
 }
 
@@ -1248,13 +1291,22 @@
                 ( sr == s || rsc->separate_virtual_interps ||
                   function != parentfunction))
         {
-            if (Tcl_EvalObjEx(rsc->server_interp,
-                        function, 0) != TCL_OK) {
+            if (Tcl_EvalObjEx(rsc->server_interp,function, 0) != TCL_OK) {
                 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, s,
                         errmsg, Tcl_GetString(function));
                 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, s, "%s",
                         Tcl_GetVar(rsc->server_interp, "errorInfo", 0));
             }
+
+	/*
+	 * Upon child exit we delete each interpreter before the caller 
+	 * uses Tcl_Finalize 
+	 */
+
+	    if (!init) {
+	    	Tcl_DeleteInterp(rsc->server_interp);
+	    }
+
         }
         sr = sr->next;
     }
@@ -1434,7 +1486,7 @@
     Rivet_ChildHandlers(s, 1);
 
     //cleanup
-    //apr_pool_cleanup_register (pChild, s, Rivet_ChildExit, Rivet_ChildExit);
+    apr_pool_cleanup_register (pChild, s, Rivet_ChildExit, Rivet_ChildExit);
 }
 
 /* Set things up to execute a file, then execute */
@@ -1469,6 +1521,8 @@
     globals->r = r;
     globals->req = (TclWebRequest *)apr_pcalloc(r->pool, sizeof(TclWebRequest));
 
+    Rivet_InitRequestVariables(interp,r);
+
     if (r->per_dir_config != NULL)
         rdc = RIVET_SERVER_CONF( r->per_dir_config );
     else
@@ -1579,6 +1633,7 @@
         request_cleanup = Tcl_NewStringObj("::Rivet::cleanup_request\n", -1);
         Tcl_IncrRefCount(request_cleanup);
     }
+
     if(Tcl_EvalObjEx(interp, request_cleanup, 0) == TCL_ERROR) {
         ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r->server, "%s",
                 Tcl_GetVar(interp, "errorInfo", 0));
@@ -1600,7 +1655,7 @@
 {
     //static const char * const aszPre[] = {
     //    "http_core.c", "mod_mime.c", NULL };
-    static const char * const aszPreTranslate[] = {"mod_alias.c", NULL};
+    //static const char * const aszPreTranslate[] = {"mod_alias.c", NULL};
 
     ap_hook_post_config (Rivet_InitHandler, NULL, NULL, APR_HOOK_LAST);
     ap_hook_handler (Rivet_SendContent, NULL, NULL, APR_HOOK_LAST);
