@echo off
setlocal enabledelayedExpansion

REM  CONFIGURAZIONE INFINITY SOLR SERVER
REM -------------------------------------------------------------------------------------------

REM SERVICE_NAME= nome del servizio di windows
set SERVICE_NAME=InfinitySolr

REM SERVICE_DESCRIPTION= descrizione del servizio di windows
set SERVICE_DESCRIPTION=InfinitySolr

REM  SOLR_MODE = modalità di avvio: server/host
set SOLR_MODE=server

REM SOLR_HOST = IP 
set SOLR_HOST=127.0.0.1

REM  SOLR_PORT = porta su cui avviare il server/host Infinity Solr Server
set SOLR_PORT=8983

REM  SOLRCLOUD_ZOOKEEPER = indirizzo server Zookeeper
REM necessario solo se SOLR_MODE == host
set SOLRCLOUD_ZOOKEEPER=127.0.0.1:8984 

REM  SOLRCLOUD_WHITEADDRESS1
set SOLRCLOUD_WHITEADDRESS1=127.0.0.1

REM  SOLRCLOUD_WHITEADDRESS2 
set SOLRCLOUD_WHITEADDRESS2=

REM  SOLRCLOUD_WHITEADDRESS3 
set SOLRCLOUD_WHITEADDRESS3=

REM  SOLRCLOUD_WHITEADDRESS4
set SOLRCLOUD_WHITEADDRESS4=

REM  SOLRCLOUD_WHITEADDRESS5
set SOLRCLOUD_WHITEADDRESS5=

set WHITE_ADDRESSES=

REM VERSIONE DI INFINITY SOLR SERVER
REM a partire dalla versione 1.3 update 2 la versione viene letta dal file version
call version.bat

REM  ADVANCED OPTIONS
REM -------------------------------------------------------------------------------------------
set SOLR_TIMEZONE=CET
set DEBUG_PORT=8100
set START_OPTIONS=
set SOLR_ENABLE_REMOTE_STREAMING=true
set SOLR_ENABLE_STREAM_BODY=true
REM set JAVA_HOME=C:\ZWeb\openjdk8

REM  CONFIGURAZIONE INFINITY_SOLR_HOME
REM -------------------------------------------------------------------------------------------
cd ..
set INFINITY_SOLR_HOME=%cd%
cd ./infinitysolr

REM  HELP
REM -------------------------------------------------------------------------------------------
IF [%1] EQU [help] (
echo. 
echo Usage: infinitysolr [COMMAND] 
echo        where COMMAND is one of: install, start, stop, remove
echo. 
echo        infinitysolr.bat install - install the windows service with name SERVICE_NAME
echo        infinitysolr.bat start_service - starts the windows service with name SERVICE_NAME
echo        infinitysolr.bat stop_service - stops the windows service with name SERVICE_NAME
echo        infinitysolr.bat remove - remove the windows service with name SERVICE_NAME
echo. 
echo        infinitysolr.bat start - starts a Infinity Solr server on "SOLR_PORT" port in "SOLR_MODE" mode
echo        infinitysolr.bat stop - stops a Infinity Solr server on "SOLR_PORT" port in "SOLR_MODE" mode     
echo.
echo        "SOLR_PORT" and "SOLR_MODE" are defined in infinitysolr.bat
echo. 
GOTO end
)
REM -------------------------------------------------------------------------------------------

REM CREAZIONE SERVIZIO
IF [%1] EQU [install] (
	GOTO install_service
)

REM STOP SERVIZIO
IF [%1] EQU [remove] (
	GOTO remove_service
)

REM START SERVIZIO
IF [%1] EQU [start_service] (
	GOTO start_service
)

REM STOP SERVIZIO
IF [%1] EQU [stop_service] (
	GOTO stop_service
)

REM Crea file di configurazione
REM -------------------------------------------------------------------------------------------
echo #host - host di infinity > "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo host=%INFINITY_URL% >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo # user - nome utente di servizio >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo user=%INFINITY_USER% >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo # password - password utente di servizio >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo password=%INFINITY_PASSWORD% >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo # company - azienda sulla quale effettuare la login >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"
echo company=%INFINITY_COMPANY% >> "../server/solr-webapp/webapp/WEB-INF/conf/config.properties"

REM Verifica whitelist se sono in start server
REM -------------------------------------------------------------------------------------------
IF [%1] EQU [start] (
	IF [%SOLR_MODE%] EQU [server] (
		
		IF  [%SOLRCLOUD_WHITEADDRESS1%] NEQ [] (
			IF [!WHITE_ADDRESSES!] EQU [] (
				set WHITE_ADDRESSES=-Djetty.whiteaddress.1=%SOLRCLOUD_WHITEADDRESS1%
			) ELSE (
				set WHITE_ADDRESSES=!WHITE_ADDRESSES! -Djetty.whiteaddress.1=%SOLRCLOUD_WHITEADDRESS1%
			)
		)
		
		IF  [%SOLRCLOUD_WHITEADDRESS2%] NEQ [] (
			IF [!WHITE_ADDRESSES!] EQU [] (
				set WHITE_ADDRESSES=-Djetty.whiteaddress.2=%SOLRCLOUD_WHITEADDRESS2%
			) ELSE (
				set WHITE_ADDRESSES=!WHITE_ADDRESSES! -Djetty.whiteaddress.2=%SOLRCLOUD_WHITEADDRESS2%
			)
		)
		
		IF  [%SOLRCLOUD_WHITEADDRESS3%] NEQ [] (
			IF [!WHITE_ADDRESSES!] EQU [] (
				set WHITE_ADDRESSES=-Djetty.whiteaddress.3=%SOLRCLOUD_WHITEADDRESS3%
			) ELSE (
				set WHITE_ADDRESSES=!WHITE_ADDRESSES! -Djetty.whiteaddress.3=%SOLRCLOUD_WHITEADDRESS3%
			)
		)
		
		IF  [%SOLRCLOUD_WHITEADDRESS4%] NEQ [] (
			IF [!WHITE_ADDRESSES!] EQU [] (
				set WHITE_ADDRESSES=-Djetty.whiteaddress.4=%SOLRCLOUD_WHITEADDRESS4%
			) ELSE (
				set WHITE_ADDRESSES=!WHITE_ADDRESSES! -Djetty.whiteaddress.4=%SOLRCLOUD_WHITEADDRESS4%
			)
		)
		
		IF  [%SOLRCLOUD_WHITEADDRESS5%] NEQ [] (
			IF [!WHITE_ADDRESSES!] EQU [] (
				set WHITE_ADDRESSES=-Djetty.whiteaddress.5=%SOLRCLOUD_WHITEADDRESS5%
			) ELSE (
				set WHITE_ADDRESSES=!WHITE_ADDRESSES! -Djetty.whiteaddress.5=%SOLRCLOUD_WHITEADDRESS5%
			)
		)
		
		IF  [!WHITE_ADDRESSES!] EQU [] (
			GOTO emptywhitelist
		)
	)
) 

IF [%2] EQU [debug] (
	set START_OPTIONS= %START_OPTIONS% -a "-agentlib:jdwp=transport=dt_socket,address=%DEBUG_PORT%,server=y,suspend=n"
)

REM Verifica e gestione parametri
REM -------------------------------------------------------------------------------------------
IF [%1] EQU [start] (
	IF [%SOLR_MODE%] EQU [server] ( GOTO start_server
	) ELSE IF [%SOLR_MODE%] EQU [host]  ( GOTO start_host
	) ELSE GOTO errore 
)

IF [%1] EQU [stop] (
	IF [%SOLR_MODE%] EQU [server] ( GOTO stop_server
	) ELSE IF [%SOLR_MODE%] EQU [host] ( GOTO stop_host
	) ELSE GOTO errore
) ELSE GOTO errore

GOTO errore

:install_service
REM  Installa servizio
nssm.exe install %SERVICE_NAME% infinitysolr.bat start
nssm.exe set %SERVICE_NAME% AppDirectory "%cd%"
nssm.exe set %SERVICE_NAME% AppNoConsole 1
nssm.exe set %SERVICE_NAME% Description "%SERVICE_DESCRIPTION%"
GOTO end

:start_service
REM  Start servizio
nssm.exe start %SERVICE_NAME%
GOTO end

:stop_service
REM  Stop servizio
nssm.exe stop %SERVICE_NAME%
GOTO end

:remove_service
REM  Rimuove servizio
nssm.exe remove %SERVICE_NAME%
GOTO end

:start_server
REM Avvio server
../bin/solr start -f -cloud -p %SOLR_PORT% -solr.home "%INFINITY_SOLR_HOME%/infinitysolr-data" -verbose -a "-Dhost=%SOLR_HOST% !WHITE_ADDRESSES!" %START_OPTIONS%
GOTO end

:start_host
REM Avvio host
../bin/solr start -f -cloud -p %SOLR_PORT% -solr.home "%INFINITY_SOLR_HOME%/infinitysolr-data" -verbose -z %SOLRCLOUD_ZOOKEEPER% 
GOTO end

:stop_server
REM Stop server
../bin/solr stop -p %SOLR_PORT% -solr.home "%INFINITY_SOLR_HOME%/infinitysolr-data" -verbose
GOTO end

:stop_host
REM Stop host
../bin/solr stop -p %SOLR_PORT% -solr.home "%INFINITY_SOLR_HOME%/infinitysolr-data" -verbose -z %SOLRCLOUD_ZOOKEEPER%
GOTO end

:emptywhitelist
REM Whitelist vuota
ECHO Inserisci almeno un whiteaddress
GOTO end

:errore
ECHO Parametri non validi
GOTO end

:end





