#!/bin/bash

set -x

DEST_DIR=$1
PORT_START=$2
PORT_COUNT=$3
MANAGEMENT_SERVER_URL=$4
LOCAL_IP=$5

if [ $# -ne 5 ];
then
	echo "Usage: $0 [dest dir] [port range start] [port count] [management server URL] [local IP]"
fi

function create_config_file {
	FILE_NAME=$DEST_DIR/server_$1.conf
	CURRENT_PORT=`expr $PORT_START + $1 - 1`

	if [ -f $FILE_NAME ]
	then
		rm $FILE_NAME
	fi

	touch $FILE_NAME

	echo "server.listen-port=$CURRENT_PORT" >> $FILE_NAME
	echo "server.run-as-daemon=false" >> $FILE_NAME
	echo "server.id=$LOCAL_IP:$CURRENT_PORT" >> $FILE_NAME

	echo "mgmt-server.url=$MANAGEMENT_SERVER_URL" >> $FILE_NAME
	echo "mgmt-server.active=true" >> $FILE_NAME

	echo "logger.enabled=true" >> $FILE_NAME
	echo "logger.level=TRACE" >> $FILE_NAME
	echo "logger.file=true" >> $FILE_NAME
	echo "logger.stdout=false" >> $FILE_NAME
	echo "logger.filename=/var/opt/speedtest/log/speedtestserver_$1.log" >> $FILE_NAME
}

mkdir -p $DEST_DIR

for i in $(seq 1 $PORT_COUNT)
do
	create_config_file $i
done

exit 0