root/main/trunk/greenstone2/gs2-web-server.bat @ 23987

Revision 23987, 9.7 KB (checked in by ak19, 9 years ago)

Bringing the windows scripts up to date with the linux scripts. Changes are still to do with moving resetting the server (and Greenstone) after moving Greenstone home. Now gs2-server can be launched as it is and will run from whichever folder GS is moved to, without having to explicitly call gsicontrol.bat's reset-gsdlhome target first.

Line 
1@echo off
2SETLOCAL
3
4if "%serverlang%" == "" set serverlang=en
5set java_min_version=1.5.0_00
6set PROGNAME=gs2-server
7if "%PROGABBR%" == "" set PROGABBR=GSI
8pushd "%CD%"
9CD /D "%~dp0"
10
11echo Greenstone 2 Server
12echo Copyright (C) 2009, New Zealand Digital Library Project, University Of Waikato
13echo This software comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.txt
14echo This is free software, and you are welcome to redistribute it
15
16::  -------- Run the Greenstone 2 Server --------
17
18::  ---- Determine GSDLHOME ----
19set gsdl2path=
20
21:: Some users may set the above line manually
22if "%gsdl2path%" == "" (
23   set GSDLHOME=%CD%
24   set gsdl2path=%CD%
25)
26
27:checkUserPermissions
28    echo.
29    echo Checking if the Greenstone log directory is writable ...
30    if not exist "%GSDLHOME%\etc\logs-gsi" goto missingLogDir
31    (echo This is a temporary file. It is safe to delete it. > "%GSDLHOME%\etc\logs-gsi\testing.tmp" ) 2>nul
32    if exist "%GSDLHOME%\etc\logs-gsi\testing.tmp" goto deleteTempFile 
33    if "%1" == "Elevated" goto printWarning
34    echo ... FAILED
35    echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi)
36    echo Requesting elevated status to become admin user to continue.
37    "%GSDLHOME%\bin\windows\gstart.exe" %0 Elevated %1 %2 %3 %4 %5 %6 %7 %8 %9
38    goto exit
39   
40:missingLogDir
41    echo ... FAILED
42    echo The Greenstone log directory does not exist (%GSDLHOME%\etc\logs-gsi). Please reinstall Greenstone.
43    pause
44    goto exit
45   
46:printWarning
47    echo ... FAILED
48    echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi).
49    echo Attempting to continue without permissions.
50    goto shiftElevated
51
52:deleteTempFile
53    echo ... OK
54    del "%GSDLHOME%\etc\logs-gsi\testing.tmp"
55
56:shiftElevated
57:: Shift "Elevated" (one of our own internal command words) out of the way if present
58:: so the command-line is as it was when the user initiated the command
59    if "%1" == "Elevated" shift
60
61echo GS2 Home: %GSDLHOME%
62
63:: Setup Greenstone2, unless it has already been done
64:: If either GSDLHOME or GSDLOS is not set, need to run setup.bat first
65:: OR operations in an IF stmt: http://fixunix.com/ms-dos/21057-how-implement-if-condition-batch-file.html
66for %%i in ("%GSDLOS%" "%GSDLHOME%") do if %%i == "" set OR=True
67if "%OR%" == "True" (
68   pushd "%gsdl2path%"
69   call setup.bat
70rem   echo **** GSDLOS: %GSDLOS% and GSDLHOME: %GSDLHOME%
71   popd
72)
73
74
75:: First test that there is actually something that can be run...
76:: Exit if the apache-httpd folder doesn't exist for some reason
77:: (The errors reported when the apache webserver does not exist
78:: in the correct location are not at all helpful).
79
80:: "You cannot use the if command to test directly for a directory, but
81:: the null (NUL) device does exist in every directory. As a result, you
82:: can test for the null device to determine whether a directory exists."
83rem echo "%GSDLHOME%\apache-httpd\nul"
84if not exist "%GSDLHOME%\apache-httpd\*" (
85    echo.
86    echo UNABLE TO CONTINUE: There is no apache-httpd directory.
87    echo It does not look like the local apache webserver has been installed.
88    echo Exiting...
89    echo.
90    goto exit
91)
92:: exit 1
93
94set PATH=%GSDLHOME%\apache-httpd\windows\lib;%PATH%
95
96:: If there's no llssite.cfg file, copy from the template
97if exist "%GSDLHOME%\llssite.cfg" goto cfgfile
98if exist "%GSDLHOME%\llssite.cfg.in" (
99   copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
100) else (
101   echo Warning: could not find llssite.cfg.in to create llssite.cfg from.
102)
103
104:cfgfile
105::  ---- Determine GSDLHOME ----
106:: JRE_HOME or JAVA_HOME must be set correctly to run this program
107bin\windows\search4j -m %java_min_version% > nul
108echo.
109:: In Java code, '...getResourceAsStream("build.properties")'
110:: needs up to be in the right directory when run
111if %ERRORLEVEL% equ 0 pushd %GSDL2PATH%
112
113:: http://ss64.com/nt/call.html (and leave in trailing slash)
114call :isinpath "%GSDLHOME%\lib\java"
115
116:: After the call, we come back here
117goto chkjava
118
119:isinpath
120:: http://ss64.com/nt/syntax-replace.html and http://ss64.com/nt/syntax-args.html
121:: (Does not work: section "Finding items within the PATH environment variable")
122:: Instead, we expand the filepath of parameter 1 to its full path and
123:: try to subtract it from the classpath.
124::call set test_cpath=%%CLASSPATH:%~f1=%%
125call set test_cpath=%%CLASSPATH:%~1=%%
126
127:: If the classpath was not empty to begin with and if there IS a difference in
128:: the classpath before and after, then the filepath was already on the classpath
129if not "%CLASSPATH%" == "" if not "%CLASSPATH%" == "%test_cpath%" (
130   echo   - CLASSPATH already correct:
131   echo %CLASSPATH%
132   goto :eof
133)
134
135:: If there was NO difference in the classpath before and after,
136:: then the filepath needs to be added to the classpath
137set CLASSPATH=%GSDLHOME%\lib\java;%CLASSPATH%
138
139:: http://ss64.com/nt/for_r.html and (for call) http://ss64.com/nt/for.html
140:: http://ss64.com/nt/syntax-args.html
141FOR /R "%GSDLHOME%\lib\java" %%G IN (*.jar) DO call :putinpath "%%G"
142echo   - Adjusted CLASSPATH
143echo.
144::echo CLASSPATH:& echo %CLASSPATH%
145goto :eof
146
147
148:putinpath
149set jarfile=%1
150::strip quotes around jarfile path, since we can't update classpath with quotes
151set jarfile=%jarfile:"=%
152echo jarfile: %jarfile%
153set CLASSPATH=%CLASSPATH%;%jarfile%
154goto :eof
155
156:: ---- Check Java ----
157:chkjava
158:: call the script with source, so that we have the variables JAVA_EXECUTABLE and GS_JAVA_HOME it sets
159set exit_status=0
160:: Need to find Java. If found, JAVA_EXECUTABLE will be set
161:: call findjava.bat %serverlang% %PROGNAME%
162if "%GSDL3SRCHOME%" == "" (set _VERSION=2) else (set _VERSION=3)
163call "%GSDLHOME%\findjava.bat"
164if "%JAVA_EXECUTABLE%" == "" echo **** No Java executable found& goto exit
165set PATH=%GS_JAVA_HOME%\bin;%PATH%
166
167
168:: ---- Run the Greenstone Server Interface ----
169:: Some informative messages to direct the users to the logs
170if "%serverlang%" == "en" (
171   echo ***************************************************************
172   echo Starting the Greenstone Server Interface ^(GSI^)...
173   echo.
174   echo Server log messages go to:
175   echo    "%GSDLHOME%\etc\logs-gsi\server.log"
176   echo.
177   echo Using Apache web server located at:
178   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\bin\httpd"
179   echo The Apache error log is at:
180   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\logs\error_log"
181   echo The Apache configuration file template is at:
182   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf.in"
183   echo This is used to generate:
184   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf"
185   echo    each time Enter Library is pressed or otherwise activated.
186   echo ***************************************************************
187   echo.
188)
189echo.
190
191:: GLI launches gs2-server.bat with:
192:: cmd /c start "window" "c:\path to\gs2-server.bat" --config=c:\path to\glisite.cfg [--quit=portnum]
193:: The configfile param could contain spaces, in which case its space-separated parts spread over
194:: multiple parameters . Need to handle things the following way, since using quotes to embed the
195:: parameters breaks the start command, see http://ss64.com/nt/start.html
196:: This script is launched by the start cmd as:
197:: "c:\path to\gs2-server.bat" --config=c:\path to\config.cfg [--quit=portnum].
198:: But if the parameter  following --config=<some string> doesn't start with a double hyphen, we
199:: assume it to be part of the config parameter, as the configfile path may have contained spaces.
200
201:: Store all parameters together in config file var: we may or may not have a portnumber
202:: Get everything after the "--quitport=", if anything, and store it in port var.  If no port, nothing to do.
203set conf=%*
204if "%conf%" == "" goto :runit
205
206set port=%conf:*--quitport=%
207if "%conf%" == "%port%" (
208set port=
209goto :runit
210) else (
211set port=%port:~1%
212)
213
214:: Because we got here, need to adjust conf by removing " --quitport=" from it and prefixing this to port
215call set conf=%%conf:%port%=%%
216set conf=%conf:~0,-12%
217set port=--quitport=%port%
218
219:: Remove trailing spaces from conf and port
220:: (We'll put the all-important quotes around conf and port when the actual java cmd is launched)
221set tmp=%conf%
222call :trim %tmp%
223set conf=%tmp%
224set tmp=%port%
225call :trim %port%
226set port=%tmp%
227set tmp=
228
229goto :runit
230
231::  http://www.computing.net/answers/programming/dos-batch-remove-trailing-space/9270.html
232:trim
233set tmp=%*
234goto :eof
235
236:runit
237:: whenever the server is started up, make sure gsdlhome is correct (in case the gs install was moved).
238:: In parallel with the linux equivalent script, redirect stdout into the void
239:: (If redirecting both stderr and stdout into the void, would need to use >nul 2>&1. See
240:: http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file-in-dos)
241call gsicontrol.bat reset-gsdlhome >nul
242
243::echo port: %port%& echo conf: %conf%& echo.& echo.
244:: "%JAVA_EXECUTABLE%" org.greenstone.server.Server2 "%GSDLHOME%" "%serverlang%" %*
245"%JAVA_EXECUTABLE%" org.greenstone.server.Server2 "%GSDLHOME%" "%serverlang%" "%conf%" "%port%"
246
247
248:: The 1st argument to gs2-server.bat (if any) would be --config, the 2nd is the config filename.
249:: Both are stored in %conf% now. If the config file is glisite.cfg, then this script was launched through
250:: GLI and would have opened a DOS console. Need to exit from this script then to close the console.
251:: Remove --config=prefix and do a case insensitive comparison, since filepaths can have uppercase chars
252if "%conf%" == "" goto exit
253if /i "%conf:~9%" == "%GSDLHOME%\glisite.cfg" goto quitcmd
254goto exit
255
256:: Exit the batch script (close the console)
257:quitcmd
258popd
259ENDLOCAL
260exit 0
261
262:: Just end the script without closing the console
263:exit
264popd
265ENDLOCAL
Note: See TracBrowser for help on using the browser.