#!/usr/bin/env bash
#
# i-Repo GEMBA OS — プラグインインストーラ（macOS / Linux）
#
# このリポジトリの plugins/i-repo-* を ~/.i-repo/plugins/ へ配置し、実行権限を付ける。
# Windows は install.ps1 を使うこと（.cmd shim を生成する）。
#
#   使い方:   ./plugins/install.sh
#   確認:     i-repo plugin list
#
set -euo pipefail

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

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

mkdir -p "$DEST_DIR"

installed=0
for f in "$SRC_DIR"/i-repo-*; do
  [ -f "$f" ] || continue
  name="$(basename "$f")"
  cp "$f" "$DEST_DIR/$name"
  chmod +x "$DEST_DIR/$name"
  echo "  ✓ $name"
  installed=$((installed + 1))
done

# 共有ライブラリ（query/gemba-read の共通エンジン）。プラグインは ./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 個のプラグインをインストールしました。"
echo "確認: i-repo plugin list"

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