#!/usr/bin/env bash
# AI 建站 — Linux 一键启动（Docker Compose）
# 用法：cd 项目根 && ./start.sh [--rebuild] [--no-pull]
# 默认从 Gitea 强制对齐最新代码再启动（可用 --no-pull / AIJZ_SKIP_GIT_PULL=1 跳过）
# 生产一键更新也可用：./restart.sh --pull  或  ./pull-and-restart.sh
# 行尾：LF
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT"
# shellcheck disable=SC1091
. "$ROOT/scripts/lib-aijz-deploy.sh"

REBUILD=0
DO_PULL=1
for a in "$@"; do
  case "$a" in
    --rebuild|-r) REBUILD=1 ;;
    --no-pull) DO_PULL=0 ;;
    --pull) DO_PULL=1 ;;
    -h|--help)
      echo "用法: $0 [--rebuild] [--no-pull|--pull]"
      echo "  默认拉取并硬对齐: ${AIJZ_GIT_URL:-https://gitea.yuxindazhineng.com/whm/ai_site.git} 分支 ${GIT_BRANCH:-main}"
      echo "  跳过拉取: --no-pull 或 AIJZ_SKIP_GIT_PULL=1"
      echo "  仅更新重启: ./restart.sh --pull"
      exit 0
      ;;
  esac
done
if [ "${AIJZ_SKIP_GIT_PULL:-0}" = "1" ]; then
  DO_PULL=0
fi

echo "========================================"
echo "  AI 建站 启动"
echo "  路径: $ROOT"
echo "========================================"

if [ "$DO_PULL" = "1" ]; then
  git_pull_hard
else
  echo "已跳过代码拉取（--no-pull / AIJZ_SKIP_GIT_PULL）"
fi

ensure_docker
ensure_env_file
export_defaults
stack_up "$REBUILD"
healthcheck
print_endpoints
