#!/bin/bash

# CONFIGURAZIONE INFINITY SOLR SERVER
# -------------------------------------------------------------------------------------------

# SOLR_MODE = modalit� di avvio: server/host
SOLR_MODE="server"

# SOLR_HOST = IP 
SOLR_HOST="127.0.0.1"

# SOLR_PORT = porta su cui avviare il server/host SolrCloud
SOLR_PORT="8983"

# SOLRCLOUD_ZOOKEEPER = indirizzo server Zookeeper
# necessario solo se SOLR_MODE == host
SOLRCLOUD_ZOOKEEPER="127.0.0.1:8984"

# SOLRCLOUD_WHITEADDRESS1
SOLRCLOUD_WHITEADDRESS1="127.0.0.1"

# SOLRCLOUD_WHITEADDRESS2 (opzionale)
SOLRCLOUD_WHITEADDRESS2=""

# SOLRCLOUD_WHITEADDRESS3 (opzionale)
SOLRCLOUD_WHITEADDRESS3=""

# SOLRCLOUD_WHITEADDRESS4 (opzionale)
SOLRCLOUD_WHITEADDRESS4=""

# SOLRCLOUD_WHITEADDRESS5 (opzionale)
SOLRCLOUD_WHITEADDRESS5=""

#VERSIONE DI INFINITY SOLR SERVER
#a partire dalla versione 1.3 update 2 la versione viene letta dal file version
#source ./version.sh .
#VERSIONE DI INFINITY SOLR SERVER
INFINITY_SOLR_VERSION="1.4 Update 2 del 31/07/2024"

#  ADVANCED OPTIONS
#  -------------------------------------------------------------------------------------------
SOLR_TIMEZONE=CET
DEBUG_PORT=8100
START_OPTIONS=
# JAVA_HOME=/opt/tomcat/jdk

##POLO - RIMOSSO E VALORIZZATO IN DOCKERFILE

#  CONFIGURAZIONE INFINITY_SOLR_HOME
# --------------------------------------------------------------------------------------------

# cd ..
# SOLR_HOME=`pwd`
# cd ./infinitysolr

#  HELP
#  -------------------------------------------------------------------------------------------
if [ "$1" = "help" ]
then
echo ""
echo "Usage: ./infinitysolr.sh [COMMAND] [TARGET]"
echo "       where COMMAND is one of: start, stop"
echo "" 
echo "       ./infinitysolr.sh start - starts a Infinity Solr server on \"SOLR_PORT\" port in \"SOLR_MODE\" mode"
echo "       ./infinitysolr.sh 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.sh"
exit
fi


## POLO RIMOSSO - SETTARE MANUALMENTE IL FILE CONFIG.PROPERTIES NEL VOLUME SERVER

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

# Verifica whitelist se sono in "start server"
#  -------------------------------------------------------------------------------------------
if [ "$1" = "start" ] &&[ "$SOLR_MODE" =  "server" ]
	then
	WHITE_ADDRESSES=""

	if [ "$SOLRCLOUD_WHITEADDRESS1" != "" ]
		then
		WHITE_ADDRESSES=$WHITE_ADDRESSES" -Djetty.whiteaddress.1="$SOLRCLOUD_WHITEADDRESS1
	fi

	if [ "$SOLRCLOUD_WHITEADDRESS2" != "" ]
		then
		WHITE_ADDRESSES=$WHITE_ADDRESSES" -Djetty.whiteaddress.2="$SOLRCLOUD_WHITEADDRESS2
	fi

	if [ "$SOLRCLOUD_WHITEADDRESS3" != "" ]
		then
		WHITE_ADDRESSES=$WHITE_ADDRESSES" -Djetty.whiteaddress.3="$SOLRCLOUD_WHITEADDRESS3
	fi

	if [ "$SOLRCLOUD_WHITEADDRESS4" != "" ]
		then
		WHITE_ADDRESSES=$WHITE_ADDRESSES" -Djetty.whiteaddress.4="$SOLRCLOUD_WHITEADDRESS4
	fi

	if [ "$SOLRCLOUD_WHITEADDRESS5" != "" ]
		then
		WHITE_ADDRESSES=$WHITE_ADDRESSES" -Djetty.whiteaddress.5="$SOLRCLOUD_WHITEADDRESS5
	fi

	if [ "$WHITE_ADDRESSES" = "" ]
		then
		echo Inserisci almeno un whiteaddress
		exit
	fi
fi	

if [ "$2" = "debug" ] 
	then
	START_OPTIONS= $START_OPTIONS -a "-agentlib:jdwp=transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n"
fi

# Verifica e gestione parametri
#  -------------------------------------------------------------------------------------------
if [ "$1" = "start" ] 
	then
	if [ "$SOLR_MODE" =  "server" ]
		then
		$SOLR_HOME/bin/solr start -force -cloud -p $SOLR_PORT -solr.home "$SOLR_HOME/infinitysolr-data" -verbose -a "$WHITE_ADDRESSES" -sv "$INFINITY_SOLR_VERSION"
		while true; do sleep 60; done
	fi
	if [ "$SOLR_MODE" = "host" ]
		then 
		$SOLR_HOME/bin/solr start -cloud -p $SOLR_PORT -solr.home "$SOLR_HOME/infinitysolr-data" -verbose -z $SOLRCLOUD_ZOOKEEPER
		exit
	fi
	echo Parametri non validi
	exit
fi

if [ "$1" = "stop" ] 
	then
	if [ "$SOLR_MODE" =  "server" ]
		then
		$SOLR_HOME/bin/solr stop -p $SOLR_PORT -solr.home "$SOLR_HOME/infinitysolr-data" -verbose
		exit
	fi
	if [ "$SOLR_MODE" = "host" ]
		then
		$SOLR_HOME/bin/solr stop -p $SOLR_PORT -solr.home "$SOLR_HOME/infinitysolr-data" -verbose -z $SOLRCLOUD_ZOOKEEPER
		exit

	fi
	echo Parametri non validi 
	exit
fi

echo Parametri non validi 
exit

