#!/bin/bash
echo "Starting server"

PREFIX="SM_SGLANG_"
ARG_PREFIX="--"

ARGS=()

while IFS='=' read -r key value; do
    arg_name=$(echo "${key#"${PREFIX}"}" | tr '[:upper:]' '[:lower:]' | tr '_' '-')

    ARGS+=("${ARG_PREFIX}${arg_name}")
    if [ -n "$value" ]; then
        ARGS+=("$value")
    fi
done < <(env | grep "^${PREFIX}")

# Add default port only if not already set
if ! [[ " ${ARGS[@]} " =~ " --port " ]]; then
    ARGS+=(--port "${SM_SGLANG_PORT:-8080}")
fi

# Add default host only if not already set
if ! [[ " ${ARGS[@]} " =~ " --host " ]]; then
    ARGS+=(--host "${SM_SGLANG_HOST:-0.0.0.0}")
fi

# Add default model-path only if not already set
if ! [[ " ${ARGS[@]} " =~ " --model-path " ]]; then
    ARGS+=(--model-path "${SM_SGLANG_MODEL_PATH:-/opt/ml/model}")
fi

echo "Running command: exec python3 -m sglang.launch_server ${ARGS[@]}"
exec python3 -m sglang.launch_server "${ARGS[@]}"
