#!/usr/bin/env bash
#
# i-Repo GEMBA OS — プラグインインストーラ（macOS / Linux）
#
# このリポジトリの plugins/i-repo-* を ~/.i-repo/plugins/ へ配置し、実行権限を付ける。
# 既定は core セットのみ（外部ツール依存ゼロ）。全部/選択は下記フラグで。
# Windows は install.ps1 を使うこと（.cmd shim を生成する）。
#
#   使い方:
#     ./plugins/install.sh                 # 既定: core（archive sqlite report-delete）
#     ./plugins/install.sh --all           # 全プラグイン
#     ./plugins/install.sh --core          # core を明示
#     ./plugins/install.sh sqlite mongo    # 指定したものだけ
#   確認:     i-repo plugin list  /  i-repo plugin available
#
set -euo pipefail

SRC_DIR="$(cd "$(dirname "$0")" && pwd)"
DEST_DIR="${IREPO_PLUGIN_DIR:-$HOME/.i-repo/plugins}"

# core = 外部 CLI 依存ゼロ。archive は deliver の bundle ハブ、sqlite は基準 sink、report-delete は独立機能。
CORE="archive sqlite report-delete"

# ── 引数解析: 既定 core / --all / --core / プラグイン名指定 ──
mode="core"
names=""
for arg in "$@"; do
  case "$arg" in
    --)     : ;;  # 引数区切り（-File 経由等で紛れ込む裸の -- を無視）
    --all)  mode="all" ;;
    --core) mode="core" ;;
    -*)     echo "unknown option: $arg" >&2; echo "usage: install.sh [--all|--core|<name>...]" >&2; exit 2 ;;
    *)      mode="names"; names="$names $arg" ;;
  esac
done

# インストール対象の basename（i-repo-<name>）リストを作る。
targets=""
case "$mode" in
  all)
    for f in "$SRC_DIR"/i-repo-*; do [ -f "$f" ] && targets="$targets $(basename "$f")"; done
    ;;
  core)
    for n in $CORE; do targets="$targets i-repo-$n"; done
    ;;
  names)
    for n in $names; do targets="$targets i-repo-$n"; done
    ;;
esac

echo "i-Repo GEMBA OS プラグインインストーラ"
echo "  from: $SRC_DIR"
echo "  to:   $DEST_DIR"
echo "  set:  $mode"
echo ""

mkdir -p "$DEST_DIR"

installed=0
for name in $targets; do
  src="$SRC_DIR/$name"
  if [ ! -f "$src" ]; then
    echo "  ✗ $name（同梱に見つかりません・skip）" >&2
    continue
  fi
  cp "$src" "$DEST_DIR/$name"
  chmod +x "$DEST_DIR/$name"
  echo "  ✓ $name"
  installed=$((installed + 1))
done

# 共有ライブラリ（query/aggregate の共通エンジン）。プラグインは ./lib/ を相対 require する。常に配置。
if [ -d "$SRC_DIR/lib" ]; then
  mkdir -p "$DEST_DIR/lib"
  for f in "$SRC_DIR"/lib/*.js; do
    [ -f "$f" ] || continue
    cp "$f" "$DEST_DIR/lib/$(basename "$f")"
    echo "  ✓ lib/$(basename "$f")"
  done
fi

echo ""
echo "${installed} 個のプラグインをインストールしました（set: ${mode}）。"
if [ "$mode" = "core" ]; then
  echo "追加で入れるには: install.sh --all（全部） / install.sh <name>… / i-repo plugin install <name>"
fi

# 既に導入済みだが今回の対象外＝更新されず古いまま残るプラグインを警告（既定 core 化の互換配慮）。
# 導入済みは同梱コピーより解決優先なので silent に stale 化する。更新導線を提示する。
in_targets() { for t in $targets; do [ "$t" = "$1" ] && return 0; done; return 1; }
stale=""
for f in "$DEST_DIR"/i-repo-*; do
  [ -f "$f" ] || continue
  b="$(basename "$f")"
  case "$b" in *.cmd) continue ;; esac
  in_targets "$b" || stale="$stale ${b#i-repo-}"
done
if [ -n "$stale" ]; then
  echo ""
  echo "⚠️  導入済みだが今回更新しなかったプラグイン:$stale"
  echo "    最新へ更新するには: install.sh --all  または  i-repo plugin install$stale"
fi

echo "確認: i-repo plugin list  /  i-repo plugin available"

# Node 製プラグインは node 実行系が必要。簡易チェック。
if ! command -v node >/dev/null 2>&1; then
  echo ""
  echo "⚠️  node が見つかりません。プラグインの実行には Node.js（>=22.5）が必要です。"
fi
