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

Last change on this file since 24829 was 24829, checked in by ak19, 12 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.

File size: 13.4 KB
RevLine 
[19278]1@echo off
2
[24829]3setlocal enabledelayedexpansion
4
[20262]5::set testdone=0
[23966]6set startdir=%CD%
[23987]7cd /d "%~dp0"
8::we're now in the "%GSDLHOME%" folder
9call setup.bat
[23949]10
[23966]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
[23949]16set cgibin=cgi-bin
17::set cgibin=cgi-bin\windows
18
[19278]19set target=%1
20set configfile=%2
[20906]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:"=%
[19278]24
[20262]25:: Construct the signal using the unique GS2 installation
[20906]26:: directory (after replacing spaces, colons and backslashes)
27set signal=%GSDLHOME: =_%
[24829]28for /F "delims=*" %%T in ('"echo !signal!| !_sed! s@[\\:]@_@g"') do set signal=%%T
[20262]29set GS2_APACHE_HTTPD_SIGNAL=GS2_APACHE_HTTPD_%signal%
[20906]30set signal=
[20262]31rem echo Signal is: %GS2_APACHE_HTTPD_SIGNAL%
32
[19970]33:: Need to get greenstone installation directory
34:: set cwd=%CD%
[19278]35
[19970]36set MONITOR_SUCCESS=MAKE SUCCESSFUL
37set MONITOR_FAILED=MAKE FAILED
38set MONITOR_FINISHED=MAKE DONE
39
[19278]40:: check that we have >=1 and <=2 arguments
41:chkargs
[20262]42if "%target%" == "" goto usage
43if not "%3" == "" goto usage
[19278]44 goto begincmd
45
46
47:usage
48echo.
49echo Usage: %0 command
50echo where command is any of the following:
[19970]51echo web-start
52echo web-stop
53echo web-restart
[20262]54::echo web-status
55::echo web-graceful
[19278]56echo configure-admin
57echo configure-web [config-filename]
58echo configure-apache [config-filename]
59echo configure-cgi
[23945]60echo reset-gsdlhome [config-filename]
[20637]61echo set-port
[20262]62echo test-gsdlhome
63echo web-stop-tested
[19278]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
[20159]74if NOT "%GSDLHOME%" == "" if NOT "%GSDLOS%" == "" goto commands
[20338]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
[19278]81
82
83::MAIN MENU
84:commands
[20262]85echo.
86echo Using:
87echo GSDLHOME = %GSDLHOME%
88echo GSDLOS = %GSDLOS%
89echo.
90
[19278]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
[23945]98if "%target%" == "reset-gsdlhome" goto resethome
[20262]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
[20338]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
[19278]108
[20338]109
[19278]110:start
[20159]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
[20262]113START "%GSDLHOME%\apachectl" "%GSDLHOME%\bin\windows\starthttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% "%GSDLHOME%\apache-httpd\windows\bin\httpd.exe"
[20639]114:: if the return value is >= 0, it's succeeded:
115if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
[19970]116echo %MONITOR_FINISHED%
117goto exit
118
[19278]119:stop
[20262]120:stoptest
[20435]121if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL% SILENT
[20262]122if "%target%" == "web-stop-tested" goto exit
[20639]123:: if the return value is >= 0, it's succeeded:
124if ERRORLEVEL 0 (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
[20262]125echo %MONITOR_FINISHED%
[19970]126goto exit
127
[20637]128
[19278]129:restart
[20637]130:: Need to stop server, wait and start it again.
131:: We're using Ping to implement batch file Wait
[20262]132if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" "%GSDLHOME%\bin\windows\stophttpd.exe" %GS2_APACHE_HTTPD_SIGNAL%
[20637]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
[19970]139goto start
140
[20637]141:failmsg
142echo Unable to wait for restart. Manually run %0 web-start
143goto exit
144
145
[20338]146::status
147::graceful
148::echo Command %target% is not operational on this operating system
149::goto exit
[19278]150
151
152::configure-admin
153:cfgadmin
154echo.
[19288]155echo Configuring admin user password:
156for /F %%T in ('getpw') do set encrypted_password=%%T
[19290]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.
[19970]160:: When piping, need to double-escape the angle brackets with three hat signs,
[19290]161:: but when redirecting to a file, need to escape only once (one hat sign).
[19288]162if ERRORLEVEL 0 (
163 (
164 echo [admin]
[19290]165 echo ^<enabled^>true
166 echo ^<groups^>administrator,colbuilder,all-collections-editor
[24829]167 echo ^<password^>!encrypted_password!
[19290]168 echo ^<username^>admin
[24829]169 ) > "!GSDLHOME!\etc\users.txt"
170 type "!GSDLHOME!\etc\users.txt" | txt2db -append "!GSDLHOME!\etc\users.gdb"
171 del "!GSDLHOME!\etc\users.txt"
[19288]172) else (
173 echo Did not set password
174)
[20262]175echo.
[19288]176goto exit
[19278]177
[20338]178
[23974]179:: reset-gsdlhome forces configure-cgi by renaming any
180:: existing gsdlsite.cfg and by deleting Mac .app files
[24297]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
[23945]183:resethome
184echo.
[24297]185if not exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto relocate
[23963]186
[24765]187set gshome=
[24829]188FOR /F "tokens=*" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
[24765]189
190:: The following doesn't work if there are spaces in the gsdlhome filepath
[24829]191::FOR /F "tokens=2" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set gshome=%%G
[24300]192:: before string comparison, remove any quotes around gsdlhome value defined in config file
[24765]193:: if not [%gshome%] == [] set gshome=%gshome:"=%
194
[24300]195if "%gshome%" == "%GSDLHOME%" set gshome=& goto exit
[24297]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
[24786]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
[24829]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
[24819]206if exist "%GSDLHOME%\bin\%GSDLOS%\perl" (
[24829]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"
[24819]210)
[24786]211set safenewhome=
212set safeoldhome=
[24297]213set gshome=
214
[24829]215for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
[23963]216
[23974]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
[23966]219type "lib\java\log4j.properties.in" | %_sed% "s\@gsdl2home@\%safepath%\g" > "lib\java\log4j.properties"
[24006]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"
[23945]222goto cfgweb
223
[24765]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
[23963]233
[24765]234
[20338]235::configure-web
236::configure-cgi
237:cfgweb
[23974]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)
[24006]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"
[23974]241
[20338]242:cfgcgi
[24295]243:: Need to preserve the user-assigned collecthome property, if any
[23949]244if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" goto cgimsg
245echo Configuring %cgibin%\gsdlsite.cfg
[23987]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"
[24295]249
[24829]250for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\\\\@g"') do set safepath=%%T
[23987]251%_sed% "s@\*\*GSDLHOME\*\*@\"%safepath%\"@g" "%cgibin%\gsdlsite.cfg.in" >> "%cgibin%\gsdlsite.cfg"
[20338]252set safepath=
[24295]253
[20338]254goto cgifin
255
256:cgimsg
257echo WARNING: Nothing done for configure-cgi.
258echo If you wish to regenerate the file
[23949]259echo %GSDLHOME%\%cgibin%\gsdlsite.cfg
[20338]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
[24829]269if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file !configfile! does not exist. Using default llssite.cfg)
[20338]270
271if exist "%GSDLHOME%\llssite.cfg" (
[24829]272 set configfile=!GSDLHOME!\llssite.cfg
[20338]273 goto cfgport
274)
275if not exist "%GSDLHOME%\llssite.cfg.in" (
[24829]276 echo Unable to proceed as neither !GSDLHOME!\llssite.cfg nor !GSDLHOME!\llssite.cfg.in could be found
[20338]277 goto exit
278)
279copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
[20906]280set configfile=%GSDLHOME%\llssite.cfg
[20338]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)
[24829]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
[20338]289
[20637]290if "%allowfromall%" == "1" set allowfromall="Allow"& goto portcon
291set allowfromall="Deny"
[20615]292
[20338]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
[20637]298
[19970]299::configure-port-and-connection
[20338]300:setport
[19970]301set /p port=Enter port number to use:
[20637]302set /p hostIP=Enter host IP to allow (127.0.0.1 is included by default):
[23252]303set /p hosts=Enter hostname or list of hosts to allow (localhost included by default):
[20637]304set /p allowfromall=Allow external connections [yes/no]:
[20338]305
[20637]306if "%allowfromall%" == "yes" set allowfromall="Allow"& goto portcon
307if "%allowfromall%" == "y" set allowfromall="Allow"& goto portcon
308set allowfromall="Deny"
[20615]309goto portcon
310
[20338]311:portcon
312if "%port%" == "" (
[20505]313 echo Done
314 goto exit
[20338]315)
[24829]316if "%safepath%" == "" for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
[19970]317
[24765]318:: Doesn't work if there are spaces in the collecthome path in gsdlsite.cfg
[24829]319::if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set COLLECTHOME=%%G
[24765]320
321:: variable in subroutine concat is called gshome,
322:: so forced to use it here for collecthome
323set gshome=
[24829]324if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=*" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
[24765]325set COLLECTHOME=%gshome%
326set gshome=
327
[22613]328if "%COLLECTHOME%" == "" set COLLECTHOME=%GSDLHOME%\collect
[24829]329for /F "delims=*" %%T in ('"echo !COLLECTHOME!| !_sed! s@\\@\/@g"') do set safecollectpath=%%T
[22613]330
[20338]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%
[23252]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"
[20338]337echo Type '%0 web-start' to start the web server running on port %port%
338echo Done
[19970]339
[20637]340set allowfromall=
[20338]341set _sed=
342set safepath=
[20637]343set port=
[20338]344
345:: Extra processing for configure-web and configure-cgi command targets
[20637]346if not "%target%" == "configure-web" if not "%target%" == "configure-cgi" goto exit
[20338]347if exist "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf" (echo %MONITOR_SUCCESS%) else (echo %MONITOR_FAILED%)
348echo %MONITOR_FINISHED%
349goto exit
350
[19278]351:exit
[23966]352cd "%startdir%"
353set startdir=
[19970]354endlocal
Note: See TracBrowser for help on using the repository browser.