#!/bin/sh
set -eu

run_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
# shellcheck disable=SC1090
. "$run_dir/effective_config.env"

: "${HOST_MODEL_ROOT:?set HOST_MODEL_ROOT to the host directory mounted at /opt/local-models}"
: "${HOST_HF_CACHE:?set HOST_HF_CACHE to the host Hugging Face cache directory}"
: "${HOST_PATCH_BUNDLE_PATH:=$patch_bundle_path}"
: "${HOST_RUNTIME_ROOT:=$run_dir/runtime}"
: "${VNEXT_CONTAINER_NAME:=vnext_${profile_name}}"
: "${VNEXT_REPLACE_CONTAINER:=1}"

case "$tp_size" in
    4) default_devices=0,1,2,3 ;;
    8) default_devices=0,1,2,3,4,5,6,7 ;;
    *) default_devices=0 ;;
esac
: "${HIP_VISIBLE_DEVICES:=$default_devices}"

mkdir -p \
    "$HOST_HF_CACHE" \
    "$HOST_RUNTIME_ROOT/root/.cache/vllm" \
    "$HOST_RUNTIME_ROOT/root/.triton" \
    "$HOST_RUNTIME_ROOT/tmp/torchinductor_root"

if [ "$VNEXT_REPLACE_CONTAINER" = "1" ]; then
    docker stop "$VNEXT_CONTAINER_NAME" >/dev/null 2>&1 || true
    docker rm "$VNEXT_CONTAINER_NAME" >/dev/null 2>&1 || true
fi

set -- docker run -d \
    --name "$VNEXT_CONTAINER_NAME" \
    --network host \
    --ipc host \
    --device /dev/kfd \
    --device /dev/dri \
    --group-add video \
    --cap-add SYS_PTRACE \
    --cap-add IPC_LOCK \
    --security-opt seccomp=unconfined \
    --security-opt label=disable \
    --ulimit memlock=-1:-1 \
    -v "$HOST_HF_CACHE:/root/.cache/huggingface" \
    -v "$HOST_MODEL_ROOT:/opt/local-models:ro" \
    -v "$run_dir:/runwork" \
    -v "$HOST_RUNTIME_ROOT/root/.cache/vllm:/root/.cache/vllm" \
    -v "$HOST_RUNTIME_ROOT/root/.triton:/root/.triton" \
    -v "$HOST_RUNTIME_ROOT/tmp/torchinductor_root:/tmp/torchinductor_root" \
    -e HIP_VISIBLE_DEVICES="$HIP_VISIBLE_DEVICES" \
    -e LD_LIBRARY_PATH=/opt/gfx906:/rccl-overlay/install/lib:/usr/local/lib:/opt/rocm/lib:/opt/venv/lib/python3.12/site-packages/torch/lib \
    --entrypoint /runwork/container_entrypoint.sh

if [ -n "$HOST_PATCH_BUNDLE_PATH" ]; then
    set -- "$@" \
        -v "$HOST_PATCH_BUNDLE_PATH:/opt/vllm_patch_bundle:ro" \
        -e VLLM_PATCH_BUNDLE=/opt/vllm_patch_bundle
fi

set -- "$@" "$runtime_image"

"$@" > "$run_dir/container_id.txt"

docker logs -f "$VNEXT_CONTAINER_NAME" > "$run_dir/container.log" 2>&1 &
echo "$!" > "$run_dir/log_tail_pid.txt"
echo "$VNEXT_CONTAINER_NAME" > "$run_dir/container_name.txt"
printf '%s\n' "$VNEXT_CONTAINER_NAME"
