#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"

PYTHON_BIN="${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}"

detect_cpu_count() {
    local cpu_count
    cpu_count="$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc 2>/dev/null || echo 1)"
    if [[ ! "${cpu_count}" =~ ^[0-9]+$ ]] || (( cpu_count <= 0 )); then
        cpu_count=1
    fi
    echo "${cpu_count}"
}

calc_workers() {
    local cpu_count="$1"
    if (( cpu_count >= 16 )); then
        echo 4
    elif (( cpu_count >= 8 )); then
        echo 2
    else
        echo 1
    fi
}

CPU_COUNT="$(detect_cpu_count)"

# 归档重写主要是串行读取 + gzip 写出，默认给一套偏稳妥的并发配置；
# 如需手动调节，可在命令前覆盖这些环境变量。
VIS_REWRITE_WORKERS="${VIS_REWRITE_WORKERS:-$(calc_workers "${CPU_COUNT}")}"
VIS_REWRITE_GZIP_LEVEL="${VIS_REWRITE_GZIP_LEVEL:-1}"

exec "${PYTHON_BIN}" \
    "${PROJECT_ROOT}/tools/convert_gt_to_label/rewrite_visualization_archives.py" \
    --workers "${VIS_REWRITE_WORKERS}" \
    --gzip-compresslevel "${VIS_REWRITE_GZIP_LEVEL}" \
    "$@"
