#!/bin/bash

 . /etc/default/scylla-server

CPUSET=""
if [ x"$SCYLLA_CPU_SET" != "x" ]; then
	CPUSET="--cpuset $SCYLLA_CPU_SET"
fi

if [ "$SCYLLA_PRODUCTION" == "true" ]; then
	DEV_MODE=""
	if [ ! -f /var/lib/scylla/.io_setup_done ]; then
		DATA_DIR=`/usr/lib/scylla/scylla_config_get.py --config $SCYLLA_CONF/scylla.yaml --get data_file_directories|head -n1`
		iotune --evaluation-directory $DATA_DIR --format envfile --options-file /var/lib/scylla/io.conf $CPUSET --timeout 600
		if [ $? -ne 0 ]; then
			echo "/var/lib/scylla did not pass validation tests, it may not be on XFS and/or has limited disk space."
			echo "This is a non-supported setup, please bind mount an XFS volume."
			exit 1
		fi
		touch /var/lib/scylla/.io_setup_done
	fi
	source /var/lib/scylla/io.conf
else
	DEV_MODE="--developer-mode true"
fi

if [[ -n $SCYLLA_LISTEN_DEVICE ]]
then
	echo "SCYLLA_LISTEN_DEVICE was pre-set via environment to: $SCYLLA_LISTEN_DEVICE. Getting IP of device..."
	DEVICE_IP=$(ip -4 a show eth0 | awk '/inet /{ gsub(/\/[0-9]*/, "", $2); print $2 }')

	if [ $? -eq 0 ]
	then
		echo "IP sucessfully retrieved. Setting $DEVICE_IP as SCYLLA_LISTEN_ADDRESS."
		SCYLLA_LISTEN_ADDRESS=$DEVICE_IP
	else
		echo "IP of device could not be retrieved! Not setting SCYLLA_LISTEN_ADDRESS."
	fi
fi

if [[ -z $SCYLLA_LISTEN_ADDRESS ]]
then
	SCYLLA_LISTEN_ADDRESS=$(hostname -i)
	echo "SCYLLA_LISTEN_ADDRESS was dynamically set to 'hostname -i' address: $SCYLLA_LISTEN_ADDRESS"
else
	echo "SCYLLA_LISTEN_ADDRESS was pre-set via environment to: $SCYLLA_LISTEN_ADDRESS"
	if [[ $SCYLLA_LISTEN_ADDRESS =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
	then
		echo "SCYLLA_LISTEN_ADDRESS appears to be a valid IP address, moving on."
	else
		echo "SCYLLA_LISTEN_ADDRESS does not appear to be a valid IP address, trying to resolve..."
		RESOLVED_IP=$(host $SCYLLA_LISTEN_ADDRESS)

		if [ $? -eq 0 ]
		then
			SCYLLA_LISTEN_ADDRESS=$(echo $RESOLVED_IP | awk '{ print $4 }')
			echo "Resolved SCYLLA_LISTEN_ADDRESS to $SCYLLA_LISTEN_ADDRESS."
		else
			echo "Could not successfully resolve SCYLLA_LISTEN_ADDRESS, continuing."
		fi
	fi
fi

EXPERIMENTAL_ARG=""
if [[ -n $SCYLLA_EXPERIMENTAL ]]
then
	EXPERIMENTAL_ARG="--experimental=on"
fi

if [ x"$SCYLLA_SEEDS" != "x" ];then
	SEEDS="$SCYLLA_SEEDS"
else
	SEEDS="$SCYLLA_LISTEN_ADDRESS"
fi

sed -i "s/seeds: \"127.0.0.1\"/seeds: \"$SEEDS\"/g" /etc/scylla/scylla.yaml
sed -i "s/listen_address: localhost/listen_address: $SCYLLA_LISTEN_ADDRESS/g" /etc/scylla/scylla.yaml

if [ x"$SCYLLA_BROADCAST_ADDRESS" != "x" ];then
	sed -i "s/.*broadcast_address:.*/broadcast_address: $SCYLLA_BROADCAST_ADDRESS/g" /etc/scylla/scylla.yaml
fi

/usr/bin/scylla --log-to-syslog 1 --log-to-stdout 0 $DEV_MODE $SEASTAR_IO $CPU_SET --default-log-level info --options-file /etc/scylla/scylla.yaml --listen-address $SCYLLA_LISTEN_ADDRESS --rpc-address $SCYLLA_LISTEN_ADDRESS --network-stack $EXPERIMENTAL_ARG posix &

source /etc/default/scylla-jmx
export SCYLLA_HOME SCYLLA_CONF
exec /usr/lib/scylla/jmx/scylla-jmx -l /usr/lib/scylla/jmx &
