#!/usr/bin/env sh
# Top-level entrypoint for the CoderAI distributable image.
# Prepares shared state directories and hands off to supervisord, which runs
# nginx + the main server + the bundled tool web UIs on the single published port.
set -eu

: "${CODERAI_CONFIG_DIR:=/config}"
: "${CODERAI_MODELS_DIR:=/models}"
: "${CODERAI_CACHE_DIR:=/cache}"
# Default parler model id; referenced by supervisord even when the parler program
# is disabled, so it must always be defined.
: "${CODERAI_PARLER_MODEL:=parler-tts/parler-tts-mini-multilingual}"
# Dedicated temp dir on the cache volume, shared by the server and the tool
# processes (so scratch from upscaling/lip-sync/ffmpeg lands in one place). The
# server's built-in janitor age-prunes it; see CODERAI_TMP below.
: "${CODERAI_TMP:=$CODERAI_CACHE_DIR/coderai-tmp}"
export TMPDIR="$CODERAI_TMP" TMP="$CODERAI_TMP" TEMP="$CODERAI_TMP"
# Writable disk-offload dir on the cache volume. Used as the default ONLY when the
# config still has the relative './offload' (which would land in the read-only
# /opt/coderai/app tree). An explicit offload.directory in config wins.
: "${CODERAI_OFFLOAD_DIR:=$CODERAI_CACHE_DIR/offload}"
export CODERAI_OFFLOAD_DIR
# Don't write .pyc into the read-only /opt/coderai tree (esp. when run as --user).
export PYTHONDONTWRITEBYTECODE=1
# Demo tool web-UI autostart toggles, consumed by supervisord.conf's
# autostart=%(ENV_CODERAI_TOOL_*)s. Default the three UIs ON and parler OFF; the
# run script (or any `-e CODERAI_TOOL_*=true|false`) overrides these. supervisord
# fails to start if a referenced ENV var is undefined, so always export them.
: "${CODERAI_TOOL_VIDEO_EDITOR:=true}"
: "${CODERAI_TOOL_VIDEOGEN:=true}"
: "${CODERAI_TOOL_TOWNSHIP:=true}"
: "${CODERAI_TOOL_PARLER:=false}"
export CODERAI_TOOL_VIDEO_EDITOR CODERAI_TOOL_VIDEOGEN CODERAI_TOOL_TOWNSHIP CODERAI_TOOL_PARLER
# Per-tool extra CLI args appended by supervisord (%(ENV_CODERAI_*_ARGS)s). The run
# script sets these via --tool-arg/--tool-args; default empty. supervisord fails to
# start if a referenced ENV var is undefined, so always export them.
: "${CODERAI_VIDEO_EDITOR_ARGS:=}"
: "${CODERAI_VIDEOGEN_ARGS:=}"
: "${CODERAI_TOWNSHIP_ARGS:=}"
: "${CODERAI_PARLER_ARGS:=}"
export CODERAI_VIDEO_EDITOR_ARGS CODERAI_VIDEOGEN_ARGS CODERAI_TOWNSHIP_ARGS CODERAI_PARLER_ARGS
export CODERAI_CONFIG_DIR CODERAI_MODELS_DIR CODERAI_CACHE_DIR CODERAI_PARLER_MODEL CODERAI_TMP

mkdir -p \
  "$CODERAI_CONFIG_DIR/coderai" \
  "$CODERAI_MODELS_DIR/coderai" \
  "$CODERAI_CACHE_DIR/coderai" \
  "$CODERAI_CACHE_DIR/township_output" \
  "$CODERAI_CACHE_DIR/videogen_output" \
  "$CODERAI_CACHE_DIR/video_editor" \
  "$CODERAI_CACHE_DIR/video_editor/sessions" \
  "$CODERAI_OFFLOAD_DIR" \
  "$CODERAI_TMP" \
  /tmp/nginx-client-body /tmp/nginx-proxy /tmp/nginx-fastcgi \
  /tmp/nginx-uwsgi /tmp/nginx-scgi

# Some server paths (trained LoRAs, characters, environments, voices) are resolved
# from the legacy HOME-based dir ~/.coderai, NOT from --config. In the container the
# user's home (e.g. /home/ubuntu) is not where config is mounted ($CONFIG_DIR/coderai),
# so those lookups would miss the mounted data and report e.g. "LoRA not found on
# server". Alias ~/.coderai -> the mounted config dir so HOME-based and --config
# lookups agree. Only act when it's safe (a symlink, missing, or an empty dir) so a
# user-mounted volume or real data is never clobbered.
if [ -n "${HOME:-}" ]; then
  _legacy="$HOME/.coderai"
  _target="$CODERAI_CONFIG_DIR/coderai"
  if [ "$_legacy" != "$_target" ]; then
    if [ -L "$_legacy" ] || [ ! -e "$_legacy" ]; then
      ln -sfn "$_target" "$_legacy" 2>/dev/null || true
    elif [ -d "$_legacy" ] && [ -z "$(ls -A "$_legacy" 2>/dev/null)" ]; then
      rmdir "$_legacy" 2>/dev/null && ln -sfn "$_target" "$_legacy" 2>/dev/null || true
    fi
  fi
fi

# Seed the ds4 working dir on the cache volume from the bundled binary + scripts
# (DeepSeek-V4 weights download here at runtime, so it must be writable/persistent).
if [ -d /opt/coderai/ds4 ] && [ ! -e "$CODERAI_CACHE_DIR/ds4/ds4-server" ]; then
  mkdir -p "$CODERAI_CACHE_DIR/ds4"
  cp -an /opt/coderai/ds4/. "$CODERAI_CACHE_DIR/ds4/" 2>/dev/null || true
fi

# If invoked with arguments, run them directly (debugging / one-off commands)
# instead of the supervised stack.
if [ "$#" -gt 0 ]; then
  exec "$@"
fi

# supervisord runs on the system python3; a leaked PYTHONHOME (pointing at the
# standalone 3.13) would break it. The per-service launchers set their own.
unset PYTHONHOME
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
