#!/bin/bash
# shellcheck disable=SC2090,SC2086,SC2089,SC1091
#! Default project path
PROJECT_PATH="$HOME/projects/mt_dao"

#! Parse command-line options
if ! OPTIONS=$(getopt -o p: --long project_path: -n 'parse-options' -- "$@"); then
	echo "mt_dao_base.sh: Error parsing options" >&2
	exit 1
fi

eval set -- "$OPTIONS"

while true; do
	case "$1" in
	-p | --project_path)
		PROJECT_PATH="$2"
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		break
		;;
	esac
done

#! Get the date and time
DATETIME=$(date '+%Y%m%d_%H%M%S')
mkdir -p "$mt_dao_SAVE_PATH"/"$DATETIME"

#! Launch ServerApp
FLWR_HOME=$PROJECT_PATH uv run flwr-serverapp \
	--insecure \
	--serverappio-api-address $SERVERAPP_API_ADDRESS >"$mt_dao_SAVE_PATH"/"$DATETIME"/serverapp.log 2>&1 &

sleep 5

echo "serverapp.sh: Flower ServerApp started."
