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

Revision 23964, 10.5 KB (checked in by ak19, 9 years ago)

Minor fixes to gems.app and the recent changes to gsicontrol.bat regarding replacing the GSDLHOME placeholder string

Line 
1@echo off
2
3setlocal
4::set testdone=0
5
6set cgibin=cgi-bin
7::set cgibin=cgi-bin\windows
8
9set target=%1
10set configfile=%2
11:: remove any quotes around configfile, if we were given parameter configfile
12:: To test for the existence of a cmdline parameter: http://ss64.com/nt/if.html
13if not [%2] == [] set configfile=%configfile:"=%
14
15:: Construct the signal using the unique GS2 installation
16:: directory (after replacing spaces, colons and backslashes)
17if "%GSDLHOME%" == "" goto testgsdl
18set signal=%GSDLHOME: =_%
19for /F "delims=*" %%T in ('"echo %signal%| bin\windows\sed.exe s@[\\:]@_@g"') do set signal=%%T
20set GS2_APACHE_HTTPD_SIGNAL=GS2_APACHE_HTTPD_%signal%
21set signal=
22rem echo Signal is: %GS2_APACHE_HTTPD_SIGNAL%
23
24:: Need to get greenstone installation directory
25:: set cwd=%CD%
26
27set MONITOR_SUCCESS=MAKE SUCCESSFUL
28set MONITOR_FAILED=MAKE FAILED
29set MONITOR_FINISHED=MAKE DONE
30
31:: check that we have >=1 and <=2 arguments
32:chkargs
33if "%target%" == "" goto usage
34if not "%3" == "" goto usage
35    goto begincmd
36
37
38:usage
39echo.
40echo    Usage: %0 command
41echo           where command is any of the following:
42echo    web-start
43echo    web-stop
44echo    web-restart
45::echo    web-status
46::echo    web-graceful
47echo    configure-admin
48echo    configure-web    [config-filename]
49echo    configure-apache [config-filename]
50echo    configure-cgi
51echo    reset-gsdlhome [config-filename]
52echo        (Save a copy of %cgibin%\gsdlsite.cfg to preserve any customisations)
53echo    set-port
54echo    test-gsdlhome
55echo    web-stop-tested
56echo.
57goto exit
58
59
60:begincmd
61:: web-stop-tested command assumes GSDLHOME/greenstone environment is already set
62if "%target%" == "web-stop-tested" goto stoptest
63
64:: For all other commands, the greenstone environment needs to be set first before they can be run
65:testgsdl
66if NOT "%GSDLHOME%" == "" if NOT "%GSDLOS%" == "" goto commands
67echo.
68echo Environment variable GSDLHOME (or GSDLOS) not set.
69echo   This needs to be set to run the gsicontrol command %target%.
70echo   Have you run setup.bat?
71echo.
72goto exit
73
74
75::MAIN MENU
76:commands
77echo.
78echo Using:
79echo   GSDLHOME = %GSDLHOME%
80echo   GSDLOS = %GSDLOS%
81echo.
82
83if "%target%" == "web-start" goto start
84if "%target%" == "web-stop" goto stop
85if "%target%" == "web-restart" goto restart
86if "%target%" == "configure-admin" goto cfgadmin
87if "%target%" == "configure-web" goto cfgweb
88if "%target%" == "configure-apache" goto cfgapach
89if "%target%" == "configure-cgi" goto cfgcgi
90if "%target%" == "reset-gsdlhome" goto resethome
91:: already tested gsdlhome (and web-stop-tested) above, don't want to keep looping on it
92if "%target%" == "test-gsdlhome" goto exit
93rem if "%target%" == "web-stop-tested" goto exit
94rem if "%target%" == "web-status" goto status
95rem if "%target%" == "web-graceful" goto graceful
96if "%target%" == "set-port" goto setport
97:: unknown command
98echo Command unrecognised: %target%
99goto usage
100
101
102:start
103:: START starts the app in a new console named by the string following immediately thereafter
104:: then we start up apache-httpd and pass the signal that the stop command will respond to
105START "%GSDLHOME%\apachectl" "%GSDLHOME%\bin\windows\starthttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% "%GSDLHOME%\apache-httpd\windows\bin\httpd.exe"
106:: if the return value is >= 0, it's succeeded:
107if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
108echo %MONITOR_FINISHED%
109goto exit
110
111:stop
112:stoptest
113if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
114if "%target%" == "web-stop-tested" goto exit
115:: if the return value is >= 0, it's succeeded:
116if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
117echo %MONITOR_FINISHED%
118goto exit
119
120
121:restart
122:: Need to stop server, wait and start it again.
123:: We're using Ping to implement batch file Wait
124if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL%
125
126:: Wait 5 seconds and then start. See http://ss64.com/nt/sleep.html (and http://malektips.com/dos0017.html)
127:: if loopback IP address (127.0.0.1) does not exist, we ask them to manually start it up again
128ping -n 1 -w 1000 127.0.0.1 |find "TTL=">nul || goto failmsg
129echo Waiting for re-start....
130ping -n 5 -w 1000 127.0.0.1> nul
131goto start
132
133:failmsg
134echo Unable to wait for restart. Manually run %0 web-start
135goto exit
136
137
138::status
139::graceful
140::echo Command %target% is not operational on this operating system
141::goto exit
142
143
144::configure-admin
145:cfgadmin
146echo.
147echo Configuring admin user password:
148for /F %%T in ('getpw') do set encrypted_password=%%T
149
150:: Have to create an intermediate file in the following, because echoing
151:: lines straight into a pipe adds spaces before the end of each line.
152:: When piping, need to double-escape the angle brackets with three hat signs,
153:: but when redirecting to a file, need to escape only once (one hat sign).
154if ERRORLEVEL 0 (
155    (
156    echo [admin]
157    echo ^<enabled^>true
158    echo ^<groups^>administrator,colbuilder,all-collections-editor
159    echo ^<password^>%encrypted_password%
160    echo ^<username^>admin
161    ) > "%GSDLHOME%\etc\users.txt"
162    type "%GSDLHOME%\etc\users.txt" | txt2db -append "%GSDLHOME%\etc\users.gdb"
163    del "%GSDLHOME%\etc\users.txt" 
164) else (
165    echo Did not set password
166)
167echo.
168goto exit
169
170
171:: reset-gsdlhome forces configure-cgi by deleting gsdlsite.cfg
172:resethome
173echo.
174if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" (
175    echo **** WARNING. Local changes made to %GSDLHOME%\%cgibin%\gsdlsite.cfg will be lost.
176    del "%GSDLHOME%\%cgibin%\gsdlsite.cfg"
177)
178
179set _sed="bin\windows\sed.exe"
180for /F "delims=*" %%T in ('"echo %GSDLHOME%| bin\windows\sed.exe s@\\@\/@g"') do set safepath=%%T
181
182type "gs2-server.app\Contents\document.wflow.in" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" > "gs2-server.app\Contents\document.wflow"
183type "gli.app\Contents\document.wflow.in" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" > "gli.app\Contents\document.wflow"
184type "client-gli.app\Contents\document.wflow.in" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" > "client-gli.app\Contents\document.wflow"
185type "gems.app\Contents\document.wflow.in" | %_sed% "s@\*\*GSDLHOME\*\*@%safepath%@g" > "gems.app\Contents\document.wflow"
186
187goto cfgweb
188
189
190::configure-web
191::configure-cgi
192:cfgweb
193:cfgcgi
194if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto cgimsg
195echo Configuring %cgibin%\gsdlsite.cfg
196for /F "delims=*" %%T in ('"echo %GSDLHOME%| bin\windows\sed.exe s@\\@\\\\@g"') do set safepath=%%T
197bin\windows\sed.exe "s@\*\*GSDLHOME\*\*@\"%safepath%\"@g" %cgibin%\gsdlsite.cfg.in > %cgibin%\gsdlsite.cfg 
198set safepath=
199goto cgifin
200
201:cgimsg
202echo WARNING: Nothing done for configure-cgi.
203echo    If you wish to regenerate the file
204echo    %GSDLHOME%\%cgibin%\gsdlsite.cfg
205echo    from scratch, delete the existing file first.
206echo.
207
208:cgifin
209if "%target%" == "configure-cgi" goto exit
210if "%target%" == "configure-web" goto cfgapach
211
212::configure-apache
213:cfgapach
214if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file %configfile% does not exist. Using default llssite.cfg)
215
216if exist "%GSDLHOME%\llssite.cfg" (
217  set configfile=%GSDLHOME%\llssite.cfg
218  goto cfgport
219)
220if not exist "%GSDLHOME%\llssite.cfg.in" (
221  echo Unable to proceed as neither %GSDLHOME%\llssite.cfg nor %GSDLHOME%\llssite.cfg.in could be found
222  goto exit
223)
224copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
225set configfile=%GSDLHOME%\llssite.cfg
226
227:cfgport
228echo Configuring the apache webserver...
229:: See http://ss64.com/nt/for_cmd.html, http://ss64.com/nt/findstr.html (and http://ss64.com/nt/find.html)
230FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^portnumber "%configfile%"') do set port=%%G
231FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hostIP "%configfile%"') do set hostIP=%%G
232FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hosts "%configfile%"') do set hosts=%%G
233FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^externalaccess "%configfile%"') do set allowfromall=%%G
234
235if "%allowfromall%" == "1" set allowfromall="Allow"& goto portcon
236set allowfromall="Deny"
237
238:: Using CALL to jump to labels means we can return from them. BUT need to ensure
239:: that command extensions are enabled to call labels. So just use GOTO instead.
240::http://ss64.com/nt/call.html
241goto portcon
242
243
244::configure-port-and-connection
245:setport
246set /p port=Enter port number to use:
247set /p hostIP=Enter host IP to allow (127.0.0.1 is included by default):
248set /p hosts=Enter hostname or list of hosts to allow (localhost included by default):
249set /p allowfromall=Allow external connections [yes/no]:
250
251if "%allowfromall%" == "yes" set allowfromall="Allow"& goto portcon
252if "%allowfromall%" == "y" set allowfromall="Allow"& goto portcon
253set allowfromall="Deny"
254goto portcon
255
256:portcon
257if "%port%" == "" (
258   echo Done
259   goto exit
260)
261if "%_sed%" == "" set _sed="bin\windows\sed.exe"
262if "%safepath%" == "" for /F "delims=*" %%T in ('"echo %GSDLHOME%| bin\windows\sed.exe s@\\@\/@g"') do set safepath=%%T
263
264if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do set COLLECTHOME=%%G
265if "%COLLECTHOME%" == "" set COLLECTHOME=%GSDLHOME%\collect
266for /F "delims=*" %%T in ('"echo %COLLECTHOME%| bin\windows\sed.exe s@\\@\/@g"') do set safecollectpath=%%T
267
268
269echo Port: %port%
270echo Stopping web server (if running)
271if not exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" echo Missing conf file
272if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
273echo Setting config file to use port %port%
274type "%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"
275echo Type '%0 web-start' to start the web server running on port %port%
276echo Done
277
278set allowfromall=
279set _sed=
280set safepath=
281set port=
282
283:: Extra processing for configure-web and configure-cgi command targets
284if not "%target%" == "configure-web" if not "%target%" == "configure-cgi" goto exit
285if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
286echo %MONITOR_FINISHED%
287goto exit
288
289:exit
290endlocal
Note: See TracBrowser for help on using the browser.