#!/bin/bash
# x402-layer Skill Installer
# Usage: curl -fsSL https://api.x402layer.cc/skill/x402-layer/install | bash

set -e

SKILL_NAME="x402-layer"
INSTALL_DIR="${1:-./$SKILL_NAME}"

echo "🚀 Installing x402-layer skill..."
echo "   Install directory: $INSTALL_DIR"

# Create directory
mkdir -p "$INSTALL_DIR/scripts"
mkdir -p "$INSTALL_DIR/references"

# Download main files
echo "📥 Downloading SKILL.md..."
curl -fsSL "https://api.x402layer.cc/skill/x402-layer/SKILL.md" -o "$INSTALL_DIR/SKILL.md"

echo "📥 Downloading requirements.txt..."
curl -fsSL "https://api.x402layer.cc/skill/x402-layer/requirements.txt" -o "$INSTALL_DIR/requirements.txt"

# Download all scripts
SCRIPTS=(
    "pay_base.py"
    "pay_solana.py"
    "consume_credits.py"
    "consume_product.py"
    "check_credits.py"
    "recharge_credits.py"
    "create_endpoint.py"
    "manage_endpoint.py"
    "topup_endpoint.py"
    "discover_marketplace.py"
    "list_on_marketplace.py"
)

echo "📥 Downloading scripts..."
for script in "${SCRIPTS[@]}"; do
    curl -fsSL "https://api.x402layer.cc/skill/x402-layer/scripts/$script" -o "$INSTALL_DIR/scripts/$script"
    echo "   ✓ $script"
done

# Install Python dependencies
echo ""
echo "📦 Installing Python dependencies..."
pip install -r "$INSTALL_DIR/requirements.txt" --quiet

echo ""
echo "✅ x402-layer skill installed successfully!"
echo ""
echo "📍 Location: $INSTALL_DIR"
echo ""
echo "🔧 Next steps:"
echo "   1. Set wallet environment variables:"
echo "      export PRIVATE_KEY='0x...'"
echo "      export WALLET_ADDRESS='0x...'"
echo ""
echo "   2. Try it out:"
echo "      python $INSTALL_DIR/scripts/discover_marketplace.py"
echo ""
echo "📚 Docs: https://studio.x402layer.cc/docs/agentic-access/openclaw-skill"
