root/main/trunk/greenstone2/gsicontrol.bat @ 27065

Revision 24904, 14.1 KB (checked in by ak19, 8 years ago)

Need quotes around collecthome in gsdlsite.cfg to handle spaces and brackets in any non-standard collecthome path.

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