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

Revision 24829, 13.4 KB (checked in by ak19, 8 years ago)

Changes to bat files and perl code to deal with brackets in (Windows) filepath. Also checked winmake.bat files to see if changes were needed there. These changes go together with the commits 24826 to 24828 for gems.bat, and commit 24820 on makegs2.bat.

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