Index: gsdl/trunk/common-src/src/lib/gdbmclass.cpp
===================================================================
--- gsdl/trunk/common-src/src/lib/gdbmclass.cpp	(revision 16892)
+++ gsdl/trunk/common-src/src/lib/gdbmclass.cpp	(revision 16895)
@@ -27,4 +27,7 @@
 #include "gsdltools.h"
 #include "gsdlunicode.h"
+#include "fileutil.h"
+
+
 
 
@@ -44,4 +47,5 @@
 			      )
 {
+
   text_t data_location;
   int block_size = 512;
@@ -63,4 +67,49 @@
   {
     gdbm_mode = GDBM_WRCREAT;
+  }
+
+  if (gdbm_mode == GDBM_READER) {
+    // Looking to read in the database
+    // => check to see if .ldb/.bdb file already there
+    // if not (first time) then generate using txt2db
+    if (!file_exists(filename)) {
+
+      // need to generate architecture native GDBM file using txt2db
+
+      // replace sought after gdbm filename ext with ".txt.gz"
+
+      text_t::const_iterator begin = filename.begin();
+      text_t::const_iterator end= filename.end();
+      text_t::const_iterator here = end;
+
+      bool found_ext = false;
+
+      while (here != begin) {
+	if (*here == '.') {
+	  found_ext = true;
+	  break;
+	}
+	here--;
+      }
+      
+      text_t filename_root;
+
+      if (found_ext) {
+	filename_root = substr(begin,here);
+      }
+      else {
+	filename_root = filename;
+      }
+
+      text_t txtgz_filename = filename_root + ".txt.gz";
+
+      text_t cmd = "gzip --decompress --to-stdout \"" + txtgz_filename + "\"";
+      cmd += " | txt2db \"" + filename + "\"";
+
+      int rv = gsdl_system(cmd, true, cerr);
+      if (rv != 0) {
+	cerr << "Tried to run command \""<<cmd<<"\", but it failed\n";
+      }
+    }
   }
 
Index: gsdl/trunk/common-src/src/lib/gsdltools.cpp
===================================================================
--- gsdl/trunk/common-src/src/lib/gsdltools.cpp	(revision 16892)
+++ gsdl/trunk/common-src/src/lib/gsdltools.cpp	(revision 16895)
@@ -25,4 +25,5 @@
 
 #include "gsdltools.h"
+#include "fileutil.h"
 
 #if defined(__WIN32__)
@@ -30,4 +31,10 @@
 #include <process.h>
 #endif
+
+#if !defined (__WIN32__)
+#include <sys/utsname.h>
+#include <unistd.h>
+#endif
+
 
 bool littleEndian() {
@@ -160,4 +167,68 @@
 }
 
+
+static bool gsdl_setenv_done = false;
+
+bool set_gsdl_env_vars (const text_t& gsdlhome) 
+{
+  if (gsdl_setenv_done) { return true; }
+
+  // set up GSDLOS, GSDLHOME and PATH environment variables
+
+  text_t gsdlos, path;
+  unsigned int path_separator = ':';
+
+#if defined (__WIN32__)
+  gsdlos = "windows";
+  path_separator = ';';
+
+  path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;");
+
+#else
+  struct utsname *buf = new struct utsname();
+  int i = uname (buf);
+  if (i == -1) gsdlos = "linux"; // uname failed, default to linux
+  else gsdlos.setcstr (buf->sysname);
+  delete buf;
+  lc (gsdlos);
+#endif
+
+  // according to getenv documentation (after a bit of digging), *getenv*
+  // is responsible for the char* pointer returned, so no need for us
+  // to free it (in fact that would be a mistake!)
+
+  char* orig_pathc = getenv ("PATH"); 
+  path += filename_cat (gsdlhome, "bin", gsdlos);
+  path.push_back (path_separator);
+  path += filename_cat (gsdlhome, "bin", "script");
+  if (orig_pathc != NULL) {
+    path.push_back (path_separator);
+    path += orig_pathc;
+  }  
+
+  // these will be cleaned up in the destructor
+  char* gsdlosc = gsdlos.getcstr();
+  char* gsdlhomec = gsdlhome.getcstr();
+  char* pathc = path.getcstr();
+  
+  if ((setenv("GSDLOS",gsdlosc,1)!=0) 
+      || (setenv("GSDLHOME",gsdlhomec,1)!=0) 
+      || (setenv("PATH",pathc,1)!=0))
+    {
+      perror("Setting Greenstone environment variables");
+      return false;
+    }
+
+  delete [] gsdlosc;
+  delete [] gsdlhomec;
+  delete [] pathc;
+
+  gsdl_setenv_done = true;
+
+  return true;
+}
+
+
+
 // attempts to work out if perl is functional
 bool perl_ok (ostream &logout) {
Index: gsdl/trunk/common-src/src/lib/gsdltools.h
===================================================================
--- gsdl/trunk/common-src/src/lib/gsdltools.h	(revision 16892)
+++ gsdl/trunk/common-src/src/lib/gsdltools.h	(revision 16895)
@@ -65,4 +65,6 @@
 int gsdl_system (const text_t &cmd, bool sync, ostream &logout);
 
+bool set_gsdl_env_vars (const text_t& gsdlhome) ;
+
 // attempts to work out if perl is functional
 bool perl_ok (ostream &logout);
