source: main/trunk/greenstone2/gsicontrol.bat@ 39002

Last change on this file since 39002 was 39002, checked in by kjdon, 13 days ago

changed the usage message slightly

File size: 14.6 KB
Line 
1@echo off
2
3setlocal enabledelayedexpansion
4
5::set testdone=0
6set startdir=%CD%
7cd /d "%~dp0"
8::we're now in the "%GSDLHOME%" folder
9call setup.bat
10
11::set _sed="%GSDLHOME%\bin\windows\sed.exe"
12:: NOTE: no quotes allowed around the following, since it's used inside
13:: a FOR statement executing a command already embedded in quotes
14set _sed=bin\windows\sed.exe
15
16set target=%1
17set configfile=%2
18:: remove any quotes around configfile, if we were given parameter configfile
19:: To test for the existence of a cmdline parameter: http://ss64.com/nt/if.html
20if not [%2] == [] set configfile=%configfile:"=%
21
22:: Construct the signal using the unique GS2 installation
23:: directory (after replacing spaces, colons and backslashes)
24set signal=%GSDLHOME: =_%
25for /F "delims=*" %%T in ('"echo !signal!| !_sed! s@[\\:]@_@g"') do set signal=%%T
26set GS2_APACHE_HTTPD_SIGNAL=GS2_APACHE_HTTPD_%signal%
27set signal=
28rem echo Signal is: %GS2_APACHE_HTTPD_SIGNAL%
29
30:: Need to get greenstone installation directory
31:: set cwd=%CD%
32
33set MONITOR_SUCCESS=MAKE SUCCESSFUL
34set MONITOR_FAILED=MAKE FAILED
35set MONITOR_FINISHED=MAKE DONE
36
37:: check that we have >=1 and <=2 arguments
38:chkargs
39if "%target%" == "" goto usage
40if not "%3" == "" goto usage
41 goto begincmd
42
43
44:usage
45echo.
46echo Usage: %0 command
47echo where command is any of the following:
48echo web-start
49echo web-stop
50echo web-restart
51::echo web-status
52::echo web-graceful
53echo configure-admin
54echo configure-web [config-filename]
55echo configure-apache [config-filename]
56echo configure-cgi
57echo reset-gsdlhome [config-filename]
58echo set-port
59echo test-gsdlhome
60echo web-stop-tested
61echo.
62echo NOTES: if config-filename not specified, $GSDLHOME/llssite.cfg will be used
63goto exit
64
65
66:begincmd
67:: web-stop-tested command assumes GSDLHOME/greenstone environment is already set
68if "%target%" == "web-stop-tested" goto stoptest
69
70:: For all other commands, the greenstone environment needs to be set first before they can be run
71:testgsdl
72if NOT "%GSDLHOME%" == "" if NOT "%GSDLOS%" == "" goto commands
73echo.
74echo Environment variable GSDLHOME (or GSDLOS) not set.
75echo This needs to be set to run the gsicontrol command %target%.
76echo Have you run setup.bat?
77echo.
78goto exit
79
80
81::MAIN MENU
82:commands
83echo.
84echo Using:
85echo GSDLHOME = %GSDLHOME%
86echo GSDLOS = %GSDLOS%
87echo.
88
89:: If %PROCESSOR_ARCHITECTURE% is x86, default the architecture to 32 bits, else 64.
90:: (Can't test for x64, since the 64 bit Win 7 here returns "AMD64" instead of "x64".)
91:: Then, if the svn version already uses just %GSDLOS% not %GSDLOS%%GSDLARCH%,
92:: set GSDLARCH to the empty string. Same if %PROCESSOR_ARCHITECTURE% is empty.
93if "%PROCESSOR_ARCHITECTURE%" == "x86" (set GSDLARCH=32) else (set GSDLARCH=64)
94if "%PROCESSOR_ARCHITECTURE%" == "" set GSDLARCH=
95if exist "bin\windows" set GSDLARCH=
96set cgibin=cgi-bin\%GSDLOS%%GSDLARCH%
97::set cgibin=cgi-bin ::set cgibin=cgi-bin\windows
98
99if "%target%" == "web-start" goto start
100if "%target%" == "web-stop" goto stop
101if "%target%" == "web-restart" goto restart
102if "%target%" == "configure-admin" goto cfgadmin
103if "%target%" == "configure-web" goto cfgweb
104if "%target%" == "configure-apache" goto cfgapach
105if "%target%" == "configure-cgi" goto cfgcgi
106if "%target%" == "reset-gsdlhome" goto resethome
107:: already tested gsdlhome (and web-stop-tested) above, don't want to keep looping on it
108if "%target%" == "test-gsdlhome" goto exit
109rem if "%target%" == "web-stop-tested" goto exit
110rem if "%target%" == "web-status" goto status
111rem if "%target%" == "web-graceful" goto graceful
112if "%target%" == "set-port" goto setport
113:: unknown command
114echo Command unrecognised: %target%
115goto usage
116
117
118:start
119:: START starts the app in a new console named by the string following immediately thereafter
120:: then we start up apache-httpd and pass the signal that the stop command will respond to
121START "%GSDLHOME%\apachectl" "%GSDLHOME%\bin\windows\starthttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% "%GSDLHOME%\apache-httpd\windows\bin\httpd.exe"
122:: if the return value is >= 0, it's succeeded:
123if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
124echo %MONITOR_FINISHED%
125goto exit
126
127:stop
128:stoptest
129if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
130if "%target%" == "web-stop-tested" goto exit
131:: if the return value is >= 0, it's succeeded:
132if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
133echo %MONITOR_FINISHED%
134goto exit
135
136
137:restart
138:: Need to stop server, wait and start it again.
139:: We're using Ping to implement batch file Wait
140if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL%
141
142:: Wait 5 seconds and then start. See http://ss64.com/nt/sleep.html (and http://malektips.com/dos0017.html)
143:: if loopback IP address (127.0.0.1) does not exist, we ask them to manually start it up again
144ping -n 1 -w 1000 127.0.0.1 |find "TTL=">nul || goto failmsg
145echo Waiting for re-start....
146ping -n 5 -w 1000 127.0.0.1> nul
147goto start
148
149:failmsg
150echo Unable to wait for restart. Manually run %0 web-start
151goto exit
152
153
154::status
155::graceful
156::echo Command %target% is not operational on this operating system
157::goto exit
158
159
160::configure-admin
161:cfgadmin
162echo.
163echo Configuring admin user password:
164for /F %%T in ('getpw') do set encrypted_password=%%T
165
166:: Have to create an intermediate file in the following, because echoing
167:: lines straight into a pipe adds spaces before the end of each line.
168:: When piping, need to double-escape the angle brackets with three hat signs,
169:: but when redirecting to a file, need to escape only once (one hat sign).
170if ERRORLEVEL 0 (
171 (
172 echo [admin]
173 echo ^<enabled^>true
174 echo ^<groups^>administrator,colbuilder,all-collections-editor
175 echo ^<password^>!encrypted_password!
176 echo ^<username^>admin
177 ) > "!GSDLHOME!\etc\users.txt"
178 type "!GSDLHOME!\etc\users.txt" | txt2db -append "!GSDLHOME!\etc\users.gdb"
179 del "!GSDLHOME!\etc\users.txt"
180) else (
181 echo Did not set password
182)
183echo.
184goto exit
185
186
187:: reset-gsdlhome forces configure-cgi by renaming any
188:: existing gsdlsite.cfg and by deleting Mac .app files
189:: However, we only relocate if there is a gsdlsite.cfg file with its gsdlhome
190:: property the same as the current (greenstone installation) directory
191:resethome
192echo.
193if not exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto relocate
194
195set gshome=
196FOR /F "tokens=*" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
197
198:: The following doesn't work if there are spaces in the gsdlhome filepath
199::FOR /F "tokens=2" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set gshome=%%G
200:: before string comparison, remove any quotes around gsdlhome value defined in config file
201:: if not [%gshome%] == [] set gshome=%gshome:"=%
202
203if "%gshome%" == "%GSDLHOME%" set gshome=& goto exit
204
205move "%GSDLHOME%\%cgibin%\gsdlsite.cfg" "%GSDLHOME%\%cgibin%\gsdlsite.cfg.bak"
206echo **** Regenerating %GSDLHOME%\%cgibin%\gsdlsite.cfg
207echo **** Previous version of file now %GSDLHOME%\%cgibin%\gsdlsite.cfg.bak
208
209:relocate
210:: The path to the included perl at the top of gliserver.pl and other cgi-bin perl files needs to use the new gsdlhome
211:: On windows, the path in gliserver.pl and the others uses backslashes
212for /F "delims=*" %%T in ('"echo !gshome!| !_sed! s@\\@\\\\@g"') do set safeoldhome=%%T
213for /F "delims=*" %%T in ('"echo !gsdlhome!| !_sed! s@\\@\\\\@g"') do set safenewhome=%%T
214if exist "%GSDLHOME%\bin\%GSDLOS%\perl" (
215 copy "!cgibin!\gliserver.pl" "!cgibin!\gliserver.pl.bak"
216 type "!cgibin!\gliserver.pl.bak" | !_sed! "s@!safeoldhome!@!safenewhome!@g" > "!cgibin!\gliserver.pl"
217 del "!cgibin!\gliserver.pl.bak"
218
219 copy "!cgibin!\metadata-server.pl" "!cgibin!\metadata-server.pl.bak"
220 type "!cgibin!\metadata-server.pl.bak" | !_sed! "s@!safeoldhome!@!safenewhome!@g" > "!cgibin!\metadata-server.pl"
221 del "!cgibin!\metadata-server.pl.bak"
222
223 copy "!cgibin!\checksum.pl" "!cgibin!\checksum.pl.bak"
224 type "!cgibin!\checksum.pl.bak" | !_sed! "s@!safeoldhome!@!safenewhome!@g" > "!cgibin!\checksum.pl"
225 del "!cgibin!\checksum.pl.bak"
226)
227set safenewhome=
228set safeoldhome=
229set gshome=
230
231for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
232
233:: Also re-initialise the log4j.properties and force regeneration of
234:: Mac .app files since cfgweb will generate these if they don't exist
235type "lib\java\log4j.properties.in" | %_sed% "s\@gsdl2home@\%safepath%\g" > "lib\java\log4j.properties"
236:: No use for Mac .app files on Windows, so they're not there in Windows binaries including caveat
237::for %%G in (gs2-server.app gli.app client-gli.app gems.app) do if exist "%%G\Contents\document.wflow" del "%%G\Contents\document.wflow"
238goto cfgweb
239
240:: Subroutine used to glue parts of a filepath that contains spaces back together again
241:: http://www.computing.net/answers/programming/batch-for-loop-tokens/16727.html
242:concat
243:: first remove any quotes around this part of the filepath
244set suffix=%~1
245if not "%suffix%" == "gsdlhome" if not "%suffix%" == "collecthome" set gshome=%gshome%%suffix%
246shift
247if not "%~1"=="" goto concat
248goto :eof
249
250
251::configure-web
252::configure-cgi
253:cfgweb
254:: first set up Mac's .app files if that's not already been done (if this is the first time we're running GS3)
255:: No use for Mac .app files on Windows, so they're not there in Windows binaries including caveat
256:: for %%G in (gs2-server.app gli.app client-gli.app gems.app) do if not exist "%%G\Contents\document.wflow" type "%%G\Contents\document.wflow.in" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" > "%%G\Contents\document.wflow"
257
258:cfgcgi
259:: Need to preserve the user-assigned collecthome property, if any
260if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto cgimsg
261echo Configuring %cgibin%\gsdlsite.cfg
262echo # **** This file is automatically generated, do not edit **** > "%cgibin%\gsdlsite.cfg"
263echo # For local customization of Greenstone, edit gsdlsite.cfg.in >> "%cgibin%\gsdlsite.cfg"
264echo. >> "%cgibin%\gsdlsite.cfg"
265
266for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\\\\@g"') do set safepath=%%T
267%_sed% "s@\*\*GSDLHOME\*\*@\"%safepath%\"@g" "%cgibin%\gsdlsite.cfg.in" >> "%cgibin%\gsdlsite.cfg"
268set safepath=
269
270goto cgifin
271
272:cgimsg
273echo WARNING: Nothing done for configure-cgi.
274echo If you wish to regenerate the file
275echo %GSDLHOME%\%cgibin%\gsdlsite.cfg
276echo from scratch, delete the existing file first.
277echo.
278
279:cgifin
280if "%target%" == "configure-cgi" goto exit
281if "%target%" == "configure-web" goto cfgapach
282
283::configure-apache
284:cfgapach
285if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file !configfile! does not exist. Using default llssite.cfg)
286
287if exist "%GSDLHOME%\llssite.cfg" (
288 set configfile=!GSDLHOME!\llssite.cfg
289 goto cfgport
290)
291if not exist "%GSDLHOME%\llssite.cfg.in" (
292 echo Unable to proceed as neither !GSDLHOME!\llssite.cfg nor !GSDLHOME!\llssite.cfg.in could be found
293 goto exit
294)
295copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
296set configfile=%GSDLHOME%\llssite.cfg
297
298:cfgport
299echo Configuring the apache webserver...
300:: See http://ss64.com/nt/for_cmd.html, http://ss64.com/nt/findstr.html (and http://ss64.com/nt/find.html)
301FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^portnumber "!configfile!"') do set port=%%G
302FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hostIP "!configfile!"') do set hostIP=%%G
303FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hosts "!configfile!"') do set hosts=%%G
304FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^externalaccess "!configfile!"') do set allowfromall=%%G
305
306if "%allowfromall%" == "1" set allowfromall="Allow"& goto portcon
307set allowfromall="Deny"
308
309:: Using CALL to jump to labels means we can return from them. BUT need to ensure
310:: that command extensions are enabled to call labels. So just use GOTO instead.
311::http://ss64.com/nt/call.html
312goto portcon
313
314
315::configure-port-and-connection
316:setport
317set /p port=Enter port number to use:
318set /p hostIP=Enter host IP to allow (127.0.0.1 is included by default):
319set /p hosts=Enter hostname or list of hosts to allow (localhost included by default):
320set /p allowfromall=Allow external connections [yes/no]:
321
322if "%allowfromall%" == "yes" set allowfromall="Allow"& goto portcon
323if "%allowfromall%" == "y" set allowfromall="Allow"& goto portcon
324set allowfromall="Deny"
325goto portcon
326
327:portcon
328if "%port%" == "" (
329 echo Done
330 goto exit
331)
332if "%safepath%" == "" for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
333
334:: Doesn't work if there are spaces in the collecthome path in gsdlsite.cfg
335::if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set COLLECTHOME=%%G
336
337:: variable in subroutine concat is called gshome,
338:: so forced to use it here for collecthome
339set gshome=
340if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=*" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
341set COLLECTHOME=%gshome%
342set gshome=
343
344if "%COLLECTHOME%" == "" set COLLECTHOME=%GSDLHOME%\collect
345for /F "delims=*" %%T in ('"echo !COLLECTHOME!| !_sed! s@\\@\/@g"') do set safecollectpath=%%T
346
347echo Port: %port%
348echo Stopping web server (if running)
349if not exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" echo Missing conf file
350if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
351echo Setting config file to use port %port%
352type "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf.in" | %_sed% "s@\*\*GSDL_OS_ARCH\*\*@%GSDLOS%%GSDLARCH%@g" | %_sed% "s@\*\*PORT\*\*@%port%@g" | %_sed% "s@\*\*CONNECTPERMISSION\*\*@%allowfromall%@g" | %_sed% "s@\*\*HOST_IP\*\*@%hostIP%@g" | %_sed% "s@\*\*HOSTS\*\*@%hosts%@g" | %_sed% "s@\*\*COLLECTHOME\*\*@%safecollectpath%@g" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" | %_sed% "s@\*\*APACHE_HOME_OS\*\*@%safepath%\/apache-httpd\/%GSDLOS%@g" > "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf"
353echo Type '%0 web-start' to start the web server running on port %port%
354echo Done
355
356set allowfromall=
357set _sed=
358set safepath=
359set port=
360
361:: Extra processing for configure-web and configure-cgi command targets
362if not "%target%" == "configure-web" if not "%target%" == "configure-cgi" goto exit
363if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
364echo %MONITOR_FINISHED%
365goto exit
366
367:exit
368cd "%startdir%"
369set startdir=
370endlocal
Note: See TracBrowser for help on using the repository browser.