#!/usr/bin/env bash set -Eeuo pipefail SCRIPT_PATH="$(readlink -f -- "${BASH_SOURCE[0]}" 2>/dev/null || true)" if [[ -z "${SCRIPT_PATH}" ]]; then SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" else SCRIPT_DIR="$(cd -- "$(dirname -- "${SCRIPT_PATH}")" && pwd)" fi cd -- "${SCRIPT_DIR}" CONFIG_FILE="${SCRIPT_DIR}/commonwealth-server.json" UPDATE_DEPENDENCIES=0 SERVER_ARGS=() ARGS=("$@") INDEX=0 while [[ ${INDEX} -lt ${#ARGS[@]} ]]; do arg="${ARGS[${INDEX}]}" case "${arg}" in --update-dependencies) UPDATE_DEPENDENCIES=1 ;; --config|-c) INDEX=$((INDEX + 1)); [[ ${INDEX} -lt ${#ARGS[@]} ]] || { echo "ERROR: ${arg} requires a path" >&2; exit 1; } CONFIG_FILE="${ARGS[${INDEX}]}" ;; --config=*) CONFIG_FILE="${arg#--config=}" ;; --host|--port|-H|-p) SERVER_ARGS+=("${arg}"); INDEX=$((INDEX + 1)); [[ ${INDEX} -lt ${#ARGS[@]} ]] || { echo "ERROR: ${arg} requires a value" >&2; exit 1; } SERVER_ARGS+=("${ARGS[${INDEX}]}") ;; --host=*|--port=*|--interactive|-i) SERVER_ARGS+=("${arg}") ;; *.json) CONFIG_FILE="${arg}" ;; -*) echo "ERROR: Unknown option: ${arg}" >&2; exit 1 ;; *) [[ -f "${arg}" ]] && CONFIG_FILE="${arg}" || { echo "ERROR: Unexpected argument: ${arg}" >&2; exit 1; } ;; esac INDEX=$((INDEX + 1)) done if [[ "${CONFIG_FILE}" != /* ]]; then CONFIG_FILE="${SCRIPT_DIR}/${CONFIG_FILE}"; fi CONFIG_FILE="$(cd -- "$(dirname -- "${CONFIG_FILE}")" && pwd)/$(basename -- "${CONFIG_FILE}")" resolve_server() { local apphost="${SCRIPT_DIR}/CommonwealthOnline.Server" local dll="${SCRIPT_DIR}/CommonwealthOnline.Server.dll" local project="${SCRIPT_DIR}/CommonwealthOnline.Server.csproj" if [[ -f "${apphost}" ]]; then chmod +x "${apphost}" 2>/dev/null || true SERVER_CMD=("${apphost}") return 0 fi if [[ -f "${dll}" ]] && command -v dotnet >/dev/null 2>&1; then SERVER_CMD=(dotnet "${dll}") return 0 fi if [[ -f "${project}" ]] && command -v dotnet >/dev/null 2>&1; then SERVER_CMD=(dotnet run --project "${project}" -c Release --no-launch-profile --) return 0 fi return 1 } if ! resolve_server; then echo "ERROR: CommonwealthOnline.Server is not published and the .NET 8 SDK/runtime is unavailable." >&2 exit 1 fi if [[ ${UPDATE_DEPENDENCIES} -eq 1 ]]; then command -v dotnet >/dev/null 2>&1 || { echo "ERROR: --update-dependencies requires the .NET SDK" >&2; exit 1; } dotnet restore "${SCRIPT_DIR}/CommonwealthOnline.Server.csproj" fi if [[ ! -f "${CONFIG_FILE}" ]]; then echo "Generating default configuration: ${CONFIG_FILE}" "${SERVER_CMD[@]}" config init "${CONFIG_FILE}" fi if [[ -t 0 && -t 1 ]] && [[ ! " ${SERVER_ARGS[*]} " =~ " --interactive " ]] && [[ ! " ${SERVER_ARGS[*]} " =~ " -i " ]]; then SERVER_ARGS+=(--interactive) fi echo "Starting Commonwealth Online C# server" exec "${SERVER_CMD[@]}" serve --config "${CONFIG_FILE}" "${SERVER_ARGS[@]}"