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

Revision 22576, 9.2 KB (checked in by sjm84, 10 years ago)

These files will now print an error and quit if the log directory is missing

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
94
95:: If there's no llssite.cfg file, copy from the template
96if exist "%GSDLHOME%\llssite.cfg" goto cfgfile
97if exist "%GSDLHOME%\llssite.cfg.in" (
98   copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
99) else (
100   echo Warning: could not find llssite.cfg.in to create llssite.cfg from.
101)
102
103:cfgfile
104::  ---- Determine GSDLHOME ----
105:: JRE_HOME or JAVA_HOME must be set correctly to run this program
106bin\windows\search4j -m %java_min_version% > nul
107echo.
108:: In Java code, '...getResourceAsStream("build.properties")'
109:: needs up to be in the right directory when run
110if %ERRORLEVEL% equ 0 pushd %GSDL2PATH%
111
112:: http://ss64.com/nt/call.html (and leave in trailing slash)
113call :isinpath "%GSDLHOME%\lib\java"
114
115:: After the call, we come back here
116goto chkjava
117
118:isinpath
119:: http://ss64.com/nt/syntax-replace.html and http://ss64.com/nt/syntax-args.html
120:: (Does not work: section "Finding items within the PATH environment variable")
121:: Instead, we expand the filepath of parameter 1 to its full path and
122:: try to subtract it from the classpath.
123::call set test_cpath=%%CLASSPATH:%~f1=%%
124call set test_cpath=%%CLASSPATH:%~1=%%
125
126:: If the classpath was not empty to begin with and if there IS a difference in
127:: the classpath before and after, then the filepath was already on the classpath
128if not "%CLASSPATH%" == "" if not "%CLASSPATH%" == "%test_cpath%" (
129   echo   - CLASSPATH already correct:
130   echo %CLASSPATH%
131   goto :eof
132)
133
134:: If there was NO difference in the classpath before and after,
135:: then the filepath needs to be added to the classpath
136set CLASSPATH=%GSDLHOME%\lib\java;%CLASSPATH%
137
138:: http://ss64.com/nt/for_r.html and (for call) http://ss64.com/nt/for.html
139:: http://ss64.com/nt/syntax-args.html
140FOR /R "%GSDLHOME%\lib\java" %%G IN (*.jar) DO call :putinpath "%%G"
141echo   - Adjusted CLASSPATH
142echo.
143::echo CLASSPATH:& echo %CLASSPATH%
144goto :eof
145
146
147:putinpath
148set jarfile=%1
149::strip quotes around jarfile path, since we can't update classpath with quotes
150set jarfile=%jarfile:"=%
151echo jarfile: %jarfile%
152set CLASSPATH=%CLASSPATH%;%jarfile%
153goto :eof
154
155:: ---- Check Java ----
156:chkjava
157:: call the script with source, so that we have the variables JAVA_EXECUTABLE and GS_JAVA_HOME it sets
158set exit_status=0
159:: Need to find Java. If found, JAVA_EXECUTABLE will be set
160:: call findjava.bat %serverlang% %PROGNAME%
161if "%GSDL3SRCHOME%" == "" (set _VERSION=2) else (set _VERSION=3)
162call "%GSDLHOME%\findjava.bat"
163if "%JAVA_EXECUTABLE%" == "" echo **** No Java executable found& goto exit
164set PATH=%GS_JAVA_HOME%\bin;%PATH%
165
166
167:: ---- Run the Greenstone Server Interface ----
168:: Some informative messages to direct the users to the logs
169if "%serverlang%" == "en" (
170   echo ***************************************************************
171   echo Starting the Greenstone Server Interface ^(GSI^)...
172   echo.
173   echo Server log messages go to:
174   echo    "%GSDLHOME%\etc\logs-gsi\server.log"
175   echo.
176   echo Using Apache web server located at:
177   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\bin\httpd"
178   echo The Apache error log is at:
179   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\logs\error_log"
180   echo The Apache configuration file template is at:
181   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf.in"
182   echo This is used to generate:
183   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf"
184   echo    each time Enter Library is pressed or otherwise activated.
185   echo ***************************************************************
186   echo.
187)
188echo.
189
190:: GLI launches gs2-server.bat with:
191:: cmd /c start "window" "c:\path to\gs2-server.bat" --config=c:\path to\glisite.cfg [--quit=portnum]
192:: The configfile param could contain spaces, in which case its space-separated parts spread over
193:: multiple parameters . Need to handle things the following way, since using quotes to embed the
194:: parameters breaks the start command, see http://ss64.com/nt/start.html
195:: This script is launched by the start cmd as:
196:: "c:\path to\gs2-server.bat" --config=c:\path to\config.cfg [--quit=portnum].
197:: But if the parameter  following --config=<some string> doesn't start with a double hyphen, we
198:: assume it to be part of the config parameter, as the configfile path may have contained spaces.
199
200:: Store all parameters together in config file var: we may or may not have a portnumber
201:: Get everything after the "--quitport=", if anything, and store it in port var.  If no port, nothing to do.
202set conf=%*
203if "%conf%" == "" goto :runit
204
205set port=%conf:*--quitport=%
206if "%conf%" == "%port%" (
207set port=
208goto :runit
209) else (
210set port=%port:~1%
211)
212
213:: Because we got here, need to adjust conf by removing " --quitport=" from it and prefixing this to port
214call set conf=%%conf:%port%=%%
215set conf=%conf:~0,-12%
216set port=--quitport=%port%
217
218:: Remove trailing spaces from conf and port
219:: (We'll put the all-important quotes around conf and port when the actual java cmd is launched)
220set tmp=%conf%
221call :trim %tmp%
222set conf=%tmp%
223set tmp=%port%
224call :trim %port%
225set port=%tmp%
226set tmp=
227
228goto :runit
229
230::  http://www.computing.net/answers/programming/dos-batch-remove-trailing-space/9270.html
231:trim
232set tmp=%*
233goto :eof
234
235:runit
236::echo port: %port%& echo conf: %conf%& echo.& echo.
237:: "%JAVA_EXECUTABLE%" org.greenstone.server.Server2 "%GSDLHOME%" "%serverlang%" %*
238"%JAVA_EXECUTABLE%" org.greenstone.server.Server2 "%GSDLHOME%" "%serverlang%" "%conf%" "%port%"
239
240
241:: The 1st argument to gs2-server.bat (if any) would be --config, the 2nd is the config filename.
242:: Both are stored in %conf% now. If the config file is glisite.cfg, then this script was launched through
243:: GLI and would have opened a DOS console. Need to exit from this script then to close the console.
244:: Remove --config=prefix and do a case insensitive comparison, since filepaths can have uppercase chars
245if "%conf%" == "" goto exit
246if /i "%conf:~9%" == "%GSDLHOME%\glisite.cfg" goto quitcmd
247goto exit
248
249:: Exit the batch script (close the console)
250:quitcmd
251popd
252ENDLOCAL
253exit 0
254
255:: Just end the script without closing the console
256:exit
257popd
258ENDLOCAL
Note: See TracBrowser for help on using the browser.