#!/usr/bin/env bash
# Plexum-minimax-provider installer (Phase v0.1).
#
# Builds + installs:
#   ~/.plexum/plugins/plexum-minimax-provider/plexum-minimax-provider-plugin
#   ~/.plexum/plugins/plexum-minimax-provider/manifest.json
#
# Operator then writes the per-profile config:
#   ~/.plexum/plugins/plexum-minimax-provider/config.json
#     {"api_key": "sk-cp-..."}
#
# Re-runnable. Profile data + config.json are never touched.
#
# Flags:
#   --profile <p>    Override profile root (default ~/.plexum)
set -euo pipefail

REPO="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
PROFILE_DIR="${HOME}/.plexum"

while [[ $# -gt 0 ]]; do
    case "$1" in
        --profile) PROFILE_DIR="$2"; shift 2 ;;
        -h|--help) sed -n '2,/^set -euo/p' "$0" | sed -n '/^#/p' | sed 's/^# \{0,1\}//'; exit 0 ;;
        *) echo "unknown flag: $1" >&2; exit 2 ;;
    esac
done

log() { printf '\033[1;34m[minimax-install]\033[0m %s\n' "$*"; }
command -v go >/dev/null || { echo "go not found on PATH" >&2; exit 1; }

PLUGIN_DIR="${PROFILE_DIR}/plugins/plexum-minimax-provider"
mkdir -p "${PLUGIN_DIR}"

cd "${REPO}"
VERSION="$(git describe --tags --always 2>/dev/null || echo dev)"
LDFLAGS="-X main.Version=${VERSION}"
log "building plexum-minimax-provider-plugin (v=${VERSION})"
CGO_ENABLED=0 go build -ldflags="${LDFLAGS}" \
    -o "${PLUGIN_DIR}/plexum-minimax-provider-plugin" \
    ./cmd/plexum-minimax-provider-plugin

cat > "${PLUGIN_DIR}/manifest.json" <<'EOF'
{
  "name": "plexum-minimax-provider",
  "version": "0.1.0",
  "activation": "lazy",
  "executable": "plexum-minimax-provider-plugin",
  "contracts": {
    "provider": {
      "models": ["MiniMax-M2.7", "MiniMax-M2.7-highspeed"]
    }
  }
}
EOF

cat <<EOF

[minimax-install] done.
  plugin binary: ${PLUGIN_DIR}/plexum-minimax-provider-plugin
  manifest:      ${PLUGIN_DIR}/manifest.json
  models:        MiniMax-M2.7 / MiniMax-M2.7-highspeed
  endpoint:      https://api.minimax.io/anthropic (default; CN via region:"cn")

Next steps:
  1. Write API key:
       cat > ${PLUGIN_DIR}/config.json <<JSON
       {"api_key": "sk-cp-..."}
       JSON
       chmod 600 ${PLUGIN_DIR}/config.json
  2. Allow plugin:
       Add "plexum-minimax-provider" to ${PROFILE_DIR}/plexum.json's
       .plugins.allow array
  3. Point an agent at MiniMax:
       plexum agent-add --model MiniMax-M2.7 <agent-id>
  4. systemctl --user restart plexum
EOF
