#!/bin/sh
set -e

DATA_ROOT="${DATA_ROOT:-/app/docker-data}"
mkdir -p "$DATA_ROOT/archive/sec" "$DATA_ROOT/config/ai"

# 首次啟動：從映像檔複製預設 AI 設定（之後以 volume 為準）
if [ ! -f "$DATA_ROOT/config/ai/agent.md" ] && [ -f /app/config/ai/agent.md ]; then
  cp -a /app/config/ai/. "$DATA_ROOT/config/ai/"
fi

ln -snf "$DATA_ROOT/data.db" /app/data.db
ln -snf "$DATA_ROOT/archive" /app/archive
rm -rf /app/config/ai
ln -snf "$DATA_ROOT/config/ai" /app/config/ai

exec node --disable-warning=ExperimentalWarning server.js