#!/bin/bash
# BurningTimes 조직 레포 - macOS / Linux 셋업
# 사용: bash setup_macos.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BURNINGTIMES_ROOT="${BURNINGTIMES_ROOT:-$(cd "$SCRIPT_DIR/.." && pwd)}"
UNITY_ROOT="${UNITY_ROOT:-$HOME/dev/DeckBuilding}"
FRAMEWORK_ROOT="${FRAMEWORK_ROOT:-$HOME/dev/BurningTimes.Framework}"
GITEA_URL="${GITEA_URL:-https://burning.i234.me}"
GITEA_SSH="${GITEA_SSH:-ssh://git@burning.i234.me:30030}"

echo "=== BurningTimes 조직 레포 셋업 ==="
echo "BURNINGTIMES_ROOT: $BURNINGTIMES_ROOT"

# 1. git 확인
command -v git >/dev/null 2>&1 || { echo "git 필요"; exit 1; }

# 2. paths.local.json
PATHS_FILE="$BURNINGTIMES_ROOT/paths.local.json"
if [ ! -f "$PATHS_FILE" ]; then
cat > "$PATHS_FILE" <<EOF
{
  "_description": "로컬 환경 경로. 커밋 금지.",
  "BURNINGTIMES_ROOT": "$BURNINGTIMES_ROOT",
  "UNITY_PROJECT_ROOT": "$UNITY_ROOT",
  "FRAMEWORK_PKG_ROOT": "$FRAMEWORK_ROOT",
  "TABLE_EXPORT_ROOT": "$UNITY_ROOT/Assets/ResWork/Table/Export",
  "GITEA_URL": "$GITEA_URL",
  "GITEA_SSH": "$GITEA_SSH",
  "HOSTNAME": "$(hostname)"
}
EOF
echo "paths.local.json 작성 완료"
else
echo "paths.local.json 이미 존재. 유지."
fi

if [ ! -d "$UNITY_MCP_PATH" ]; then
    echo "[setup] unity-mcp 외부 저장소 clone 시작..."
    mkdir -p "$(dirname "$UNITY_MCP_PATH")"
    if git clone https://github.com/CoplayDev/unity-mcp.git "$UNITY_MCP_PATH"; then
        echo "✅ unity-mcp clone 완료: $UNITY_MCP_PATH"
    else
        echo "⚠️ unity-mcp clone 실패 — 네트워크·권한 확인 후 수동 실행:"
        echo "    git clone https://github.com/CoplayDev/unity-mcp.git \"$UNITY_MCP_PATH\""
    fi
else
    echo "[setup] unity-mcp 이미 존재: $UNITY_MCP_PATH"
    echo "  (업데이트는 수동: cd \"$UNITY_MCP_PATH\" && git pull)"
fi

echo "셋업 완료."
