#!/usr/bin/env bash set -euo pipefail DATA_DIR="${LABEL_STUDIO_BASE_DATA_DIR:-/label-studio/data}" LOG_DIR="${LABEL_STUDIO_LOG_DIR:-/label-studio/logs}" mkdir -p "$DATA_DIR" "$LOG_DIR" export PYTHONPATH="/app/scripts:${PYTHONPATH:-}" if [[ "${R2_SYNC_ON_START:-1}" == "1" ]] && [[ -n "${R2_ACCESS_KEY_ID:-}" ]]; then python3 /app/scripts/sync_label_studio_r2.py pull --data-dir "$DATA_DIR" || true fi if [[ -n "${SECRET_KEY:-}" ]]; then printf 'SECRET_KEY=%s\n' "$SECRET_KEY" > "$DATA_DIR/.env" fi _sync_push() { if [[ "${R2_SYNC_ON_STOP:-1}" == "1" ]] && [[ -n "${R2_ACCESS_KEY_ID:-}" ]]; then python3 /app/scripts/sync_label_studio_r2.py push --data-dir "$DATA_DIR" || true fi } trap _sync_push EXIT INT TERM if [[ "${R2_SYNC_INTERVAL_SEC:-300}" =~ ^[0-9]+$ ]] && [[ "${R2_SYNC_INTERVAL_SEC}" -gt 0 ]]; then ( while true; do sleep "${R2_SYNC_INTERVAL_SEC}" _sync_push || true done ) & fi if [[ -n "${SPACE_HOST:-}" ]]; then exec label-studio --host="$SPACE_HOST" else exec label-studio --host="${LABEL_STUDIO_HOST:-0.0.0.0}" --port="${LABEL_STUDIO_PORT:-8080}" fi