#!/bin/bash
# 너드나비스 조직 레포 - macOS / Linux 셋업
# 사용: bash setup_macos.sh

set -euo pipefail

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

echo "=== 너드나비스 조직 레포 셋업 ==="
echo "NERDNAVIS_ROOT: $NERDNAVIS_ROOT"

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

# 2. paths.local.json
PATHS_FILE="$NERDNAVIS_ROOT/paths.local.json"
if [ ! -f "$PATHS_FILE" ]; then
cat > "$PATHS_FILE" <<EOF
{
  "_description": "로컬 환경 경로. 커밋 금지.",
  "NERDNAVIS_ROOT": "$NERDNAVIS_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

# 3. 메모리 symlink
ORG_MEM="$NERDNAVIS_ROOT/memory/org"
mkdir -p "$ORG_MEM"

CLAUDE_BASE="$HOME/.claude/projects"
if [ -d "$CLAUDE_BASE" ]; then
    for d in "$CLAUDE_BASE"/*Documents*/ "$CLAUDE_BASE"/*너드나비스*/; do
        [ -d "$d" ] || continue
        MEM="$d/memory"
        if [ -L "$MEM" ]; then
            echo "이미 symlink. 유지: $MEM"
        elif [ -d "$MEM" ]; then
            mv "$MEM" "$MEM.bak_$(date +%Y%m%d_%H%M)"
            ln -s "$ORG_MEM" "$MEM"
            echo "Symlink 생성: $MEM -> $ORG_MEM"
        else
            ln -s "$ORG_MEM" "$MEM"
            echo "Symlink 생성: $MEM -> $ORG_MEM"
        fi
    done
fi

# 3.5. Live 증분 동기화 중앙 저장소 + Symlink (C34, 2026-04-18 PD님 직접 지시)
# worktree 격리로 인한 .live/ 물리 분리 해결을 위해
# $HOME/.claude/nerdnavis-live/ 중앙 디렉토리로 symlink 연결한다.
CENTRAL_LIVE="$HOME/.claude/nerdnavis-live"
LOCAL_LIVE="$NERDNAVIS_ROOT/.live"
MARKER_NAME=".junction-marker"

mkdir -p "$CENTRAL_LIVE"
if [ ! -f "$CENTRAL_LIVE/$MARKER_NAME" ]; then
    echo "nerdnavis-live central junction target (C34, 2026-04-18)" > "$CENTRAL_LIVE/$MARKER_NAME"
fi

if [ -L "$LOCAL_LIVE" ]; then
    echo "Live symlink 이미 존재. 유지: $LOCAL_LIVE"
elif [ -d "$LOCAL_LIVE" ]; then
    BAK="$LOCAL_LIVE.bak_$(date +%Y%m%d_%H%M)"
    for f in "$LOCAL_LIVE"/*.md "$LOCAL_LIVE"/*.json; do
        [ -f "$f" ] || continue
        DST="$CENTRAL_LIVE/$(basename "$f")"
        [ ! -f "$DST" ] && cp "$f" "$DST"
    done
    mv "$LOCAL_LIVE" "$BAK"
    ln -s "$CENTRAL_LIVE" "$LOCAL_LIVE"
    echo "Live Symlink 생성: $LOCAL_LIVE -> $CENTRAL_LIVE (백업: $BAK)"
else
    ln -s "$CENTRAL_LIVE" "$LOCAL_LIVE"
    echo "Live Symlink 생성: $LOCAL_LIVE -> $CENTRAL_LIVE"
fi

# 3.6. memory/org/ 중앙 저장소 + Symlink (C34-16, 2026-04-19 신설)
CENTRAL_MEM="$HOME/.claude/nerdnavis-memory"
MEMORY_MARKER=".memory-junction-marker"

mkdir -p "$CENTRAL_MEM"
if [ ! -f "$CENTRAL_MEM/$MEMORY_MARKER" ]; then
    echo "nerdnavis-memory central (C34-16, 2026-04-19)" > "$CENTRAL_MEM/$MEMORY_MARKER"
fi

# 초기 sync 레포 → 중앙
if [ -d "$NERDNAVIS_ROOT/memory/org" ]; then
    for f in "$NERDNAVIS_ROOT/memory/org"/*.md "$NERDNAVIS_ROOT/memory/org"/*.json; do
        [ -f "$f" ] || continue
        BASENAME=$(basename "$f")
        [ ! -f "$CENTRAL_MEM/$BASENAME" ] && cp "$f" "$CENTRAL_MEM/$BASENAME"
    done
fi

# user memory symlink 중앙 재연결
if [ -d "$CLAUDE_BASE" ]; then
    for d in "$CLAUDE_BASE"/E--NerdNavisAi*/; do
        [ -d "$d" ] || continue
        mem_link="${d}memory"

        if [ -L "$mem_link" ] && [ -f "$mem_link/$MEMORY_MARKER" ]; then
            continue
        fi

        if [ -d "$mem_link" ] && [ ! -L "$mem_link" ]; then
            mv "$mem_link" "$mem_link.bak_$(date +%Y%m%d_%H%M)"
        elif [ -L "$mem_link" ]; then
            rm -f "$mem_link"
        fi

        ln -s "$CENTRAL_MEM" "$mem_link"
        echo "memory Symlink 중앙 연결: $mem_link -> $CENTRAL_MEM"
    done
fi

echo "셋업 완료."
