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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"

COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}"

require_env() {
  if [[ ! -f ".env" ]]; then
    cat <<'EOF'
缺少 .env 文件。
请在仓库根目录创建 .env（参考 env.example），例如：
  cp env.example .env
然后按需修改其中变量。
EOF
    exit 1
  fi
}

usage() {
  cat <<EOF
用法:
  ./connecthub.sh build
  ./connecthub.sh start
  ./connecthub.sh restart
  ./connecthub.sh stop
  ./connecthub.sh dev-build
  ./connecthub.sh dev-start
  ./connecthub.sh dev-restart
  ./connecthub.sh dev-stop
  ./connecthub.sh log [--follow|-f] [--tail N] [service]

环境变量(可选):
  COMPOSE_FILE=path/to/docker-compose.yml  (默认: docker-compose.yml)

示例:
  ./connecthub.sh log
  ./connecthub.sh log beat
  ./connecthub.sh log -f beat
  ./connecthub.sh log --tail 200 worker
EOF
}

log_usage() {
  cat <<EOF
用法:
  ./connecthub.sh log [--follow|-f] [--tail N] [service]

说明:
  - 不指定 service：查看全部服务日志
  - 指定 service：例如 backend/worker/beat/redis

示例:
  ./connecthub.sh log beat
  ./connecthub.sh log -f beat
  ./connecthub.sh log --tail 200 worker
EOF
}

cmd="${1:-}"
case "$cmd" in
  build)
    require_env
    docker compose -f "$COMPOSE_FILE" build
    docker compose -f "$COMPOSE_FILE" up -d
    ;;
  start)
    require_env
    docker compose -f "$COMPOSE_FILE" up -d
    ;;
  restart)
    require_env
    docker compose -f "$COMPOSE_FILE" down
    docker compose -f "$COMPOSE_FILE" up -d
    ;;
  stop)
    require_env
    docker compose -f "$COMPOSE_FILE" down
    ;;
  dev-build)
    require_env
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml build
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml up -d
    ;;
  dev-start)
    require_env
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml up -d
    ;;
  dev-restart)
    require_env
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml down
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml up -d
    ;;
  dev-stop)
    require_env
    docker compose -f "$COMPOSE_FILE" -f docker-compose.dev.yml down
    ;;
  log)
    shift || true
    follow="0"
    tail=""
    service=""

    while [[ $# -gt 0 ]]; do
      case "$1" in
        -f|--follow)
          follow="1"
          shift
          ;;
        --tail)
          if [[ $# -lt 2 ]]; then
            echo "缺少 --tail 的参数" >&2
            exit 2
          fi
          tail="$2"
          shift 2
          ;;
        --tail=*)
          tail="${1#*=}"
          shift
          ;;
        -h|--help|help)
          log_usage
          exit 0
          ;;
        *)
          if [[ -z "$service" && "${1:0:1}" != "-" ]]; then
            service="$1"
            shift
          else
            echo "未知参数: $1" >&2
            echo
            log_usage
            exit 2
          fi
          ;;
      esac
    done

    args=(docker compose -f "$COMPOSE_FILE" logs)
    if [[ "$follow" = "1" ]]; then
      args+=(--follow)
    fi
    if [[ -n "$tail" ]]; then
      args+=(--tail "$tail")
    fi
    if [[ -n "$service" ]]; then
      args+=("$service")
    fi
    "${args[@]}"
    ;;
  -h|--help|help|"")
    usage
    ;;
  *)
    echo "未知命令: $cmd" >&2
    echo
    usage
    exit 2
    ;;
esac


