#!/usr/bin/env bash
set -euo pipefail

REGISTRY="${CLAWHUB_REGISTRY:-http://skillhub.intra.example.com:8080}"
ROOT_DIR="${1:-seed-skills/dev-tools}"
NAMESPACE="${2:-${SKILLHUB_NAMESPACE:-dev-tools}}"

export CLAWHUB_REGISTRY="${REGISTRY}"

LOCAL_CLAWHUB="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/cli/clawhub"

if [ -x "${LOCAL_CLAWHUB}" ]; then
  CLI=("${LOCAL_CLAWHUB}")
elif command -v clawhub >/dev/null 2>&1; then
  CLI=(clawhub)
elif command -v npx >/dev/null 2>&1; then
  CLI=(npx -y clawhub)
else
  echo "Neither clawhub nor npx is available. Install the ClawHub CLI before publishing seed skills." >&2
  exit 1
fi

for skill_dir in "${ROOT_DIR}"/*; do
  [ -d "${skill_dir}" ] || continue
  case "$(basename "${skill_dir}")" in
    _*) continue ;;
  esac
  "${CLI[@]}" publish "${skill_dir}" --namespace "${NAMESPACE}"
done
