#!/usr/bin/env bash

# export TERMINFO=/usr/lib/terminfo
TERM="xterm"

fileName="fabric-1.21.1.jar"
memory=8192
configPath="./config"
datapackPath="./world/datapacks"
serverConfigDir="server-properties"
packwizPath="https://files.ferdin.land/ferdinland/modpacks/server/smp/"
loading_text="./config/server-properties/helpers/loading/loading.txt"
splash_smp="./config/server-properties/helpers/splash__smp.txt"
splash_creative="./config/server-properties/helpers/splash__creative.txt"

# import env
source ./.env
# import loading screen
source ./config/server-properties/helpers/loading/bash_loading_animations.sh

# loading 
cat "$loading_text"

# print info
echo
echo "==========================="
echo "======= SERVER INFO ======="
echo "==========================="
echo
echo "SERVER NAME:     | $SERVER_NAME"
echo "ENVRONMENT:      | $ENV"
echo "TYPE:            | $SERVER_TYPE"
echo "CONFIG BRANCH:   | $CONFIG_BRANCH"
echo "DATAPACK BRANCH: | $DATAPACK_BRANCH"
echo 
echo "==========================="
echo "======= +++++++++++ ======="
echo "==========================="
echo 
echo
echo "setting up $ENV environment..."

#
# Update config directory
#
echo "updating ./config..."
(
cd "$configPath"
initial_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$CONFIG_BRANCH" != "$initial_branch" ]]; then
	echo
	echo -e "\e[31m./config is on branch '$initial_branch', but '$CONFIG_BRANCH' was requested\e[0m"
	echo "switching to '$CONFIG_BRANCH'..."

	git checkout $CONFIG_BRANCH

	echo
fi
git pull
)

#
# Update datapacks
#
echo "updating datapack..."
(
cd "$datapackPath/smp-datapack"
initial_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$CONFIG_BRANCH" != "$initial_branch" ]]; then
	echo
	echo -e "\e[31mDatapack is on branch '$initial_branch', but '$DATAPACK_BRANCH' was requested\e[0m"
	echo "switching to '$DATAPACK_BRANCH'..."

	git checkout $DATAPACK_BRANCH

	echo
fi
git pull
)

#
# Import dev-tools
#
if [[ "$ENV" = "dev" || "stag" ]]; then
	echo
	echo "checking devtools..."
	(
	source ./config/server-properties/helpers/curl.sh
	cd ./world/datapacks
	if [[ ! -d "Benchmark" ]]; then
		file="benchmark-v2-1-0.tar.gz"
		echo "missing Benchmark v2, downloading..."
		__curl "http://nginxwebdav:80/ferdinland/devtools/benchmark-v2-1-0.tar.gz" > $file
		tar -zxvf $file
		rm $file
	fi
	)
	echo "devtools all good!"
	echo
fi

#
# Copy server-properties
#
echo
echo copying server.properties...
cp --force "$configPath"/"$serverConfigDir"/server.properties ./server.properties
#
# Copy server-icon.png
#
echo copying server-icon.png...
cp --force "$configPath"/"$serverConfigDir"/server.properties ./server-icon.png

#
# manage mods
#
echo
echo installing mods from "$packwizPath"/pack.toml...
echo
java -jar packwiz-installer-bootstrap.jar -g -s server "$packwizPath"/pack.toml

#
# start server
#
echo
echo "starting the server..."
echo
case "$SERVER_TYPE" in
	smp)
		cat "$splash_smp"
		;;
	creative)
		cat "$splash_creative"
		;;
	personal)
		echo
		echo
		echo "============================================"
		echo
		echo "  Welcome to your server, $PERSONAL_OWNER!"
		echo
		echo "============================================"
		echo
		echo
		;;
esac

declare -i memory

java -Xms"$memory"M -Xmx"$memory"M -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+AlwaysActAsServerClassMachine -XX:+AlwaysPreTouch -XX:+DisableExplicitGC -XX:+UseNUMA -XX:NmethodSweepActivity=1 -XX:ReservedCodeCacheSize=400M -XX:NonNMethodCodeHeapSize=12M -XX:ProfiledCodeHeapSize=194M -XX:NonProfiledCodeHeapSize=194M -XX:-DontCompileHugeMethods -XX:MaxNodeLimit=240000 -XX:NodeLimitFudgeFactor=8000 -XX:+UseVectorCmov -XX:+PerfDisableSharedMem -XX:+UseFastUnorderedTimeStamps -XX:+UseCriticalJavaThreadPriority -XX:ThreadPriorityPolicy=1 -XX:AllocatePrefetchStyle=3 -XX:+UseZGC -XX:AllocatePrefetchStyle=1 -XX:-ZProactive -jar "$fileName" --nogui
