Index: main/trunk/gli/gems.bat
===================================================================
--- main/trunk/gli/gems.bat	(revision 30567)
+++ main/trunk/gli/gems.bat	(revision 30568)
@@ -44,4 +44,12 @@
 
 :checkUserPermissions
+
+rem In a distributed GS3 setup like in the labs, we don't require the Greenstone directory to be writable.
+rem If GS3, get the using.user.web property from build.properties and if set to true, we can skip to shiftElevated
+:: http://ss64.com/nt/syntax-substring.html
+	if "%_VERSION%" == "3" if exist "%GSDL3SRCHOME%\build.properties" for /F "tokens=1,2 delims==" %%G in (%GSDL3SRCHOME%\build.properties) do ( 
+		if "%%G" == "using.user.web" if "%%H" == "true" goto :shiftElevated
+	)
+
 	echo.
 	echo Checking if the Greenstone collection directory is writable ...
Index: main/trunk/gli/gli.bat
===================================================================
--- main/trunk/gli/gli.bat	(revision 30567)
+++ main/trunk/gli/gli.bat	(revision 30568)
@@ -48,4 +48,11 @@
 
 :checkUserPermissions
+
+rem In a distributed GS3 setup like in the labs, we don't require the Greenstone directory to be writable.
+rem If GS3, get the using.user.web property from build.properties and if set to true, we can skip to shiftElevated
+:: http://ss64.com/nt/syntax-substring.html	
+	if "%_VERSION%" == "3" if exist "%GSDL3SRCHOME%\build.properties" for /F "tokens=1,2 delims==" %%G in (%GSDL3SRCHOME%\build.properties) do ( 
+		if "%%G" == "using.user.web" if "%%H" == "true" goto :shiftElevated
+	)
 	echo.
 	echo Checking if the Greenstone collection directory is writable ...
@@ -158,4 +165,20 @@
 :: Run GS3 if version = 3
 :rungs3
+
+	rem In a distributed GS3 set up like in the labs, gsdl3home.isreadonly would be true and
+	rem we need to run the web server in read-only mode. This section of code borrowed from gs3-server.bat.
+	set USE_TMPDIR_FOR_TOMCAT=
+	for /F "tokens=1,2 delims==" %%G in (%GSDL3SRCHOME%\build.properties) do ( 
+		if "%%G"=="gsdl3home.isreadonly" set USE_TMPDIR_FOR_TOMCAT=%%H
+	)	
+	if "%USE_TMPDIR_FOR_TOMCAT%" == "true" (
+		set gsdl3_writablehome=%TMP%\greenstone\web
+		set opt_properties="-Dgsdl3home.isreadonly=true" -Dgsdl3.writablehome="%gsdl3_writablehome%"
+		echo Setting Greenstone3 web home writable area to be: %gsdl3_writablehome%
+		pushd %GSDL3SRCHOME%
+		cmd /c ant.bat %opt_properties% configure-web
+		popd
+	)
+	
 	if "%_VERSION%" == "3" "%JAVA_EXECUTABLE%" -cp classes/;GLI.jar;lib/apache.jar;lib/qfslib.jar;lib/rsyntaxtextarea.jar org.greenstone.gatherer.GathererProg -gsdl "%GSDLHOME%" -gsdlos %GSDLOS% -gsdl3 "%GSDL3HOME%" -gsdl3src "%GSDL3SRCHOME%" -perl "%PERLPATH%" %1 %2 %3 %4 %5 %6 %7 %8 %9
     if ERRORLEVEL 2 (
Index: main/trunk/greenstone3/build.properties.in
===================================================================
--- main/trunk/greenstone3/build.properties.in	(revision 30567)
+++ main/trunk/greenstone3/build.properties.in	(revision 30568)
@@ -15,4 +15,14 @@
 # uncomment the following if you have moved the greenstone3 web directory to tomcats webapps dir 
 #web.home=${tomcat.installed.path}/webapps/greenstone3
+
+## If creating a distributed GS3 set up, with GS3 in a read-only location, then it will need a
+## distinct user web folder for content (sites and collections), and need to run tomcat from TMP.
+## You need to configure 4 properties to get a distributed GS3 to work on windows:
+## - set web.home above to the writable location where collections and sites should be created (use forward slashes)
+## - uncomment using.user.web below and set to true, 
+## To make tomcat run from TMP folder (a writable location):
+## - set gsdl3home.isreadonly to true and 
+## - set gsdl3.writablehome to ${java.io.tmpdir}/greenstone/web (use forward slashes)
+# using.user.web=true
 
 # Default derby networked server portnumber is 1527, can change this here in case of conflict
Index: main/trunk/greenstone3/build.xml
===================================================================
--- main/trunk/greenstone3/build.xml	(revision 30567)
+++ main/trunk/greenstone3/build.xml	(revision 30568)
@@ -169,8 +169,17 @@
   <property name="web.home" value="${basedir}/web"/>
   <property name="web.writablehome" value="${gsdl3.writablehome}"/>
-
+  <!-- If using a distributed GS3, then the user web would not contain everything in the default GS3 web 
+  (it won't contain a CGI or lib folder inside WEB-INF, for instance) -->
+  <if>
+    <bool><available file="${web.home}/WEB-INF/cgi" type="dir"/></bool>
+    <property name="full.web.dir" value="${web.home}"/>
+	<else>
+		<property name="full.web.dir" value="${basedir}/web"/>
+	</else>
+  </if>  
+  
   <!-- jar files needed by applets go here -->
   <property name="web.applet" value="${web.home}/applet"/>
-
+  
   <!-- jar files needed by the servlet (and extra ones) go here -->
   <property name="web.lib" value="${web.home}/WEB-INF/lib"/>
@@ -235,5 +244,5 @@
 	    preservelastmodified="true"
 	    failonerror="true" >  
-	<fileset dir="${web.home}/WEB-INF" includes="**"/>  
+	<fileset dir="${full.web.dir}/WEB-INF" includes="**"/>  
       </copy>
 
@@ -631,6 +640,6 @@
   
   <path id="derby.server.classpath">
-    <pathelement location="${web.lib}/derbynet.jar"/>
-    <pathelement location="${web.lib}/derby.jar"/>
+    <pathelement location="${web.writablelib}/derbynet.jar"/>
+    <pathelement location="${web.writablelib}/derby.jar"/>
   </path>
 
@@ -1272,7 +1281,7 @@
 	<bool><not><equals arg1="${web.home}" arg2="${web.writablehome}"></equals></not></bool>
 	<mkdir dir="${web.writablehome}"/>
-	<copy file="${web.home}/WEB-INF/cgi/gliserver.pl"       tofile="${web.writablehome}/WEB-INF/cgi/gliserver.pl"       overwrite="true"/>
-	<copy file="${web.home}/WEB-INF/cgi/metadata-server.pl" tofile="${web.writablehome}/WEB-INF/cgi/metadata-server.pl" overwrite="true"/>
-	<copy file="${web.home}/WEB-INF/cgi/checksum.pl"        tofile="${web.writablehome}/WEB-INF/cgi/checksum.pl"        overwrite="true"/>
+	<copy file="${full.web.dir}/WEB-INF/cgi/gliserver.pl"       tofile="${web.writablehome}/WEB-INF/cgi/gliserver.pl"       overwrite="true"/>
+	<copy file="${full.web.dir}/WEB-INF/cgi/metadata-server.pl" tofile="${web.writablehome}/WEB-INF/cgi/metadata-server.pl" overwrite="true"/>
+	<copy file="${full.web.dir}/WEB-INF/cgi/checksum.pl"        tofile="${web.writablehome}/WEB-INF/cgi/checksum.pl"        overwrite="true"/>
       </if>
 
Index: main/trunk/greenstone3/gs3-server.bat
===================================================================
--- main/trunk/greenstone3/gs3-server.bat	(revision 30567)
+++ main/trunk/greenstone3/gs3-server.bat	(revision 30568)
@@ -16,15 +16,22 @@
 if "%GSDL3SRCHOME%" == "" call "%GSDL3PATH%\gs3-setup.bat" SetEnv > nul
 
+set USE_TMPDIR_FOR_TOMCAT=
+for /F "tokens=1,2 delims==" %%G in (%GSDL3SRCHOME%\build.properties) do ( 
+	if "%%G"=="gsdl3home.isreadonly" set USE_TMPDIR_FOR_TOMCAT=%%H
+)
 
+:: If the gsdl3.home readonly property is not already set to true, then
 :: See if Greenstone3 web folder is writable
+if "%USE_TMPDIR_FOR_TOMCAT%" == "false" (
   echo.
   echo Checking if the Greenstone3 web directory is writable ...
   (echo This is a temporary file. It is safe to delete it. > "%GSDL3HOME%\testing.tmp" ) 2>nul
   if exist "%GSDL3HOME%\testing.tmp" goto isWritable
+)
 
 :: Is read-only
   set gsdl3_writablehome=%TMP%\greenstone\web
   set opt_properties="-Dgsdl3home.isreadonly=true" -Dgsdl3.writablehome="%gsdl3_writablehome%"
-  echo ... no.
+  if "%USE_TMPDIR_FOR_TOMCAT%" == "false" echo ... no.
   echo Setting Greenstone3 web home writable area to be: %gsdl3_writablehome%
 
Index: main/trunk/greenstone3/gs3-setup.bat
===================================================================
--- main/trunk/greenstone3/gs3-setup.bat	(revision 30567)
+++ main/trunk/greenstone3/gs3-setup.bat	(revision 30568)
@@ -36,4 +36,21 @@
 :: set GSDL3HOME to the 'web' subdirectory
 set GSDL3HOME=!GSDL3SRCHOME!\web
+set TOMCATWEB=!GSDL3SRCHOME!\web
+:: set GSDL3HOME to any web.home property provided, and create that folder if it doesn't exist
+:: Replace forward slashes in web.home with back slashes 
+:: http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
+if exist "!GSDL3SRCHOME!\build.properties" for /F "tokens=1,2 delims==" %%G in (!GSDL3SRCHOME!\build.properties) do ( 
+	if "%%G"=="web.home" (
+		set GSDL3HOME=%%H
+		set GSDL3HOME=!GSDL3HOME:/=\!
+		set TOMCATWEB=!GSDL3HOME!
+		if not exist "!GSDL3HOME!" cmd /c "!GSDL3SRCHOME!\userweb.cmd"
+	)
+)
+
+:: Whatever the web directory is, it should contain the WEB-INF\classes folder, else go back to using default for this
+:: The WEB-INF\classes folder will be absent in a userweb folder, but will be present if GSDL3HOME=GSDL3SRCHOME\web
+:: or if web.home points to GS3 as a webapp inside tomcat
+if not exist "!GSDL3HOME!\WEB-INF\classes" set TOMCATWEB=!GSDL3SRCHOME!\web
 
 :: change if using external tomcat or ant
@@ -46,5 +63,5 @@
 :: ---- Set the CLASSPATH and PATH environment variables ----
 if "!GS_CP_SET!" == "yes" goto skipSetCp
-set CLASSPATH=!GSDL3HOME!\WEB-INF\classes;!GSDL3SRCHOME!\resources\java;!GSDL3SRCHOME!\cp.jar;!CLASSPATH!;
+set CLASSPATH=!TOMCATWEB!\WEB-INF\classes;!GSDL3SRCHOME!\resources\java;!GSDL3SRCHOME!\cp.jar;!CLASSPATH!;
 set PATH=!GSDL3SRCHOME!\bin;!GSDL3SRCHOME!\bin\script;!GSDL3SRCHOME!\lib\jni;!ANT_HOME!\bin;!PATH!
 
Index: main/trunk/greenstone3/userweb.cmd
===================================================================
--- main/trunk/greenstone3/userweb.cmd	(revision 30568)
+++ main/trunk/greenstone3/userweb.cmd	(revision 30568)
@@ -0,0 +1,39 @@
+@echo off
+setlocal enabledelayedexpansion
+
+:: AFFECTED FILES:
+:: gli\gems.bat
+:: gli\gli.bat
+:: build.properties.in
+:: build.properties
+:: build.xml
+:: gs3-server.bat
+:: gs3-setup.bat
+:: userweb.cmd
+
+set INSTALLDIR=!GSDL3SRCHOME!
+set USERWEB=
+set IS_TMP_TOMCAT=
+
+:: Loosely based on http://stackoverflow.com/questions/7708681/how-to-read-from-a-properties-file-using-batch-script 
+:: Replace forward slashes in web.home with back slashes 
+:: http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
+for /F "tokens=1,2 delims==" %%G in (!GSDL3SRCHOME!\build.properties) do ( 
+	if "%%G"=="web.home" set USERWEB=%%H& set USERWEB=!USERWEB:/=\!
+	if "%%G"=="gsdl3home.isreadonly" set IS_TMP_TOMCAT=%%H
+)
+echo USERWEB: !USERWEB!
+echo IS_TMP_TOMCAT: !IS_TMP_TOMCAT!
+
+if "!USERWEB!" == "" echo No userweb directory provided & echo. & goto done
+
+:: add exclusion of log folder
+robocopy /e "!INSTALLDIR!\web" "!USERWEB!" /xd "!INSTALLDIR!\web\sites\localsite\collect" "!INSTALLDIR!\web\WEB-INF" > CON
+robocopy "!INSTALLDIR!\web\WEB-INF" "!USERWEB!\WEB-INF" > CON
+md !USERWEB!\sites\localsite\collect
+
+
+:done
+endlocal & set USERWEB=%USERWEB%
+
+:end
