Index: main/trunk/greenstone2/runtime-src/packages/apache-httpd/adjust-winmak-files.bat
===================================================================
--- main/trunk/greenstone2/runtime-src/packages/apache-httpd/adjust-winmak-files.bat	(revision 31422)
+++ main/trunk/greenstone2/runtime-src/packages/apache-httpd/adjust-winmak-files.bat	(revision 31425)
@@ -3,7 +3,8 @@
 :: (Also http://trac.greenstone.org/browser/main/trunk/ant-tasks/src/org/greenstone/anttasks/RegexSearchReplace.java
 :: http://trac.greenstone.org/browser/main/trunk/ant-tasks/src/org/greenstone/anttasks/InsertUniqueValue.java)
-:: This script performs 2 steps: 
-:: replace all occurrences of "tempfile" in .mak files within httpd-2.2.11 with "tempfile$counter"
-:: replace all occurrences of "/MD" in .mak files within httpd-2.2.11 with "/MT"
+:: This script makes use of mod_winmake_file.awk to perform the following 2 steps
+:: to prevent race conditions to any win32.mak file trying to create/access the same tempfile.bat: 
+:: - in each .mak within httpd-2.2.11, make each line that refers to "tempfile.bat" refer to a unique tempfile.bat instead
+:: - replace all occurrences of "/MD" in .mak files within httpd-2.2.11 with "/MT"
 
 :: http://www.theverge.com/2016/3/30/11331014/microsoft-windows-linux-ubuntu-bash
@@ -33,5 +34,5 @@
 :: http://stackoverflow.com/questions/2913231/how-do-i-increment-a-dos-variable-in-a-for-f-loop
 :: http://stackoverflow.com/questions/17605767/create-list-or-arrays-in-windows-batch
-set counter=0
+rem set counter=0
 
 :: http://stackoverflow.com/questions/2951063/windows-bat-file-how-to-recursively-list-all-files-of-type-mp3
@@ -41,6 +42,4 @@
 
 call :rsr-files %httpdfolder% *.mak
-::echo PATHS: %PATHS%
-::echo NAMES: %NAMES%
 
 popd
@@ -51,8 +50,13 @@
 :: ************************ SUBROUTINES **********************************************
 
-:: append counter to tempfile.bat references in the current .mak file of loop
-:: And change any occurrence of /MD to /MT in each .mak file too
-:: Digit wildcard doesn't work for me in sed regex here, and although the any char wildcard
-:: works it may be too greedy. Fortunately, character classes for digits works.
+:: A win mak file may make references to tempfile.bat in various lines of the makefile. The different
+:: references could end up clashing with each other when they're generated on the file system by
+:: that .mak file, leading to race conditions. To avoid this, within each win32.mak file, ensure
+:: unique tempfile .bat files are generated:
+:: For each win32.mak file, change each line of reference to tempfile.bat to a unique tempfile.bat
+:: by renaming the references in that makefile line to tempfile<number>.bat. Note: change each
+:: LINE to refer consistently to a unique tempfile .bat, don't make each reference in a line
+:: refer to a different tempfile.bat.
+:: Further, change any occurrence of /MD to /MT in each .mak file too.
 :: To check this script has worked, check httpd-2.2.11/modules/ssl/mod_ssl.mak to make sure
 :: occurrences of "tempfile.bat" have a number suffix before the extension and that there's
@@ -64,23 +68,26 @@
     set NAMES=
     for /r "%~1" %%P in ("%~2") do (
-		rem set PATHS=!PATHS! "%%~fP"
-		rem set NAMES=!NAMES! "%%~nP%%~xP"
 		
-		
-		set /a "counter +=1"
+		rem set /a "counter +=1"
 		rem echo counter: !counter!
         
 		set filename=%%~fP
+		echo file: Modifying win32.mak file !filename! to refer to unique tempfile .bat files to avoid race conditions.
 		
-		echo Writing tempfile!counter! into
-		echo file: !filename!		
+		rem echo Writing tempfile!counter! into
+		rem echo file: !filename!		
 		
+		rem copy "!filename!" "!filename!.tmp"		
+		rem sed "s@tempfile[0-9]*\.bat@tempfile!counter!.bat@g" "!filename!.tmp" > "!filename!"
+		rem del "!filename!.tmp"
+        
+		rem copy "!filename!" "!filename!.tmp"
+		rem sed "s@/MD@/MT@g" "!filename!.tmp" > "!filename!"
+		rem del "!filename!.tmp"
 		
+		rem http://stackoverflow.com/questions/4462954/sed-increment-number
+		rem https://www.gnu.org/software/gawk/manual/gawk.html see section An Example with Two Rules
 		copy "!filename!" "!filename!.tmp"
-		sed "s@tempfile[0-9]*\.bat@tempfile!counter!.bat@g" "!filename!.tmp" > "!filename!"
-		del "!filename!.tmp"
-        
-		copy "!filename!" "!filename!.tmp"
-		sed "s@/MD@/MT@g" "!filename!.tmp" > "!filename!"
+		awk -f mod_winmak_file.awk "!filename!.tmp" > "!filename!"
 		del "!filename!.tmp"
 		
Index: main/trunk/greenstone2/runtime-src/packages/apache-httpd/mod_winmak_file.awk
===================================================================
--- main/trunk/greenstone2/runtime-src/packages/apache-httpd/mod_winmak_file.awk	(revision 31425)
+++ main/trunk/greenstone2/runtime-src/packages/apache-httpd/mod_winmak_file.awk	(revision 31425)
@@ -0,0 +1,33 @@
+# This program replaces each occurrence of tempfile.bat in the current input line 
+# with tempfile-concatenated-to-current-count-.bat.
+
+# The current line is denoted by dollar-0. The global substitution regex, gsub,
+# doesn't return anything, but replaces what's in the 3rd argument. By default
+# gsub's 3rd argument is dollar-0 but we explicitly provide this as 3rd argument here.
+# The print command then outputs what we want for the current input line, in this
+# case dollar-0 which has been modified by the gsub.
+
+# https://www.gnu.org/software/gawk/manual/gawk.html#Two-Rules
+# For global substition function gsub, see https://www.tutorialspoint.com/awk/awk_string_functions.htm
+
+
+# The begin block is for initialisation such as declaration of vars
+BEGIN { count = 1 }
+
+# We can have as many regex rules here as we want. Each gets applied in
+# sequence to the current input line. In this case we have one regex rule
+# It matches tempfile\d*.bat and then defines what should happen when that
+# line is encountered.
+/tempfile\.bat/ { 
+                    uniq_temp = "tempfile" count ".bat"
+					gsub("tempfile.bat",uniq_temp,$0)					
+					count++
+				}
+/\/MD/ {
+	gsub("/MD", "/MT")	
+}
+
+# the current line is now correct, output it
+{
+	print $0
+}
