#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
echo "[INFO] Code Performance Advisor - Bootstrap"
echo "[INFO] Skill root: $SCRIPT_DIR"
if ! command -v python3 &> /dev/null; then
echo "[ERROR] python3 not found. Please install Python 3.7+"
exit 1
fi
if [ ! -d "assets/rules/special_rules" ]; then
echo "[ERROR] Rules directory not found: assets/rules/special_rules"
echo "[ERROR] Please ensure you are running this script from the skill root"
exit 1
fi
if [ -f "assets/manifests/index.json" ]; then
backup_file="assets/manifests/index.json.backup.$(date +%Y%m%d_%H%M%S)"
echo "[INFO] Backing up existing index to: $backup_file"
cp "assets/manifests/index.json" "$backup_file"
fi
rm -f assets/manifests/index.json
echo "[INFO] Building rule index..."
rule_count=0
error_count=0
for rule_file in assets/rules/special_rules/R_*/R_*.md; do
if [ -f "$rule_file" ]; then
rule_name=$(basename "$(dirname "$rule_file")")
if python3 scripts/analysis_engine/cli.py update-index --rule "$rule_file" 2>&1 >/dev/null; then
rule_count=$((rule_count + 1))
echo -n "."
else
echo "[WARN] Failed to index: $rule_file"
error_count=$((error_count + 1))
fi
fi
done
echo ""
echo ""
echo "========================================="
echo "Bootstrap Summary"
echo "========================================="
echo "Rules indexed: $rule_count"
echo "Errors: $error_count"
if [ -f "assets/manifests/index.json" ]; then
indexed_count=$(python3 -c "import json; data=json.load(open('assets/manifests/index.json')); print(len(data['rules']))" 2>/dev/null || echo "0")
echo "Index file size: $(wc -c < assets/manifests/index.json) bytes"
echo "Rules in index: $indexed_count"
if [ "$indexed_count" -gt 0 ]; then
echo ""
echo "✅ Bootstrap completed successfully!"
echo ""
echo "Next steps:"
echo " 1. Initialize workspace: python3 scripts/analysis_engine/init_workspace.py --op <operator>"
echo " 2. Generate tags: Use code_tag subskill"
echo " 3. Score rules: python3 scripts/analysis_engine/cli.py score"
exit 0
else
echo ""
echo "❌ Index file created but contains no rules"
exit 1
fi
else
echo ""
echo "❌ Bootstrap failed: index.json not created"
exit 1
fi