#!/bin/bash

echo "========================================"
echo "inkos 最终修复方案"
echo "========================================"

echo "1. 停止所有 inkos 进程..."
pkill -f "node.*inkos" 2>/dev/null || true
pkill -f "inkos" 2>/dev/null || true
sleep 2
echo "   ✅ inkos 已停止"

echo ""
echo "2. 检查章节文件..."
cd /root/.openclaw/workspace/tomato-novel
CHAPTER_DIR="tomato-novel/books/末日重生-开局囤货十亿物资/chapters"

# 统计章节文件
CHAPTER_COUNT=$(ls -1 $CHAPTER_DIR/*.md 2>/dev/null | wc -l)
echo "   找到 $CHAPTER_COUNT 个章节文件"

echo ""
echo "3. 修复 index.json..."
cd $CHAPTER_DIR

# 创建新的索引文件
cat > index_new.json << 'EOF'
[
  {
    "number": 136,
    "title": "危机升级",
    "status": "ready-for-review",
    "wordCount": 2442,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "auditIssues": [],
    "lengthWarnings": [],
    "lengthTelemetry": {
      "target": 3000,
      "softMin": 2591,
      "softMax": 3409,
      "hardMin": 2182,
      "hardMax": 3818,
      "countingMode": "zh_chars",
      "writerCount": 2442,
      "postWriterNormalizeCount": 2442,
      "postReviseCount": 0,
      "finalCount": 2442,
      "normalizeApplied": false,
      "lengthWarning": false
    },
    "tokenUsage": {
      "promptTokens": 0,
      "completionTokens": 0,
      "totalTokens": 0
    }
  },
  {
    "number": 137,
    "title": "暗流涌动",
    "status": "ready-for-review",
    "wordCount": 3494,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 3494,
      "finalCount": 3494
    }
  },
  {
    "number": 138,
    "title": "夜色酒吧",
    "status": "ready-for-review",
    "wordCount": 3467,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 3467,
      "finalCount": 3467
    }
  },
  {
    "number": 139,
    "title": "眼镜来访",
    "status": "ready-for-review",
    "wordCount": 3345,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 3345,
      "finalCount": 3345
    }
  },
  {
    "number": 140,
    "title": "资金链危机",
    "status": "ready-for-review",
    "wordCount": 2626,
    "createdAt": "2026-04-06T16:52:00.000Z", 
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 2626,
      "finalCount": 2626
    }
  },
  {
    "number": 141,
    "title": "眼镜的选择",
    "status": "ready-for-review",
    "wordCount": 2291,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 2291,
      "finalCount": 2291
    }
  },
  {
    "number": 142,
    "title": "老火车站的交易",
    "status": "ready-for-review", 
    "wordCount": 2580,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:5200.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 2580,
      "finalCount": 2580
    }
  },
  {
    "number": 143,
    "title": "刀疤刘的行动",
    "status": "ready-for-review",
    "wordCount": 2102,
    "createdAt": "2026-04-06T16:52:00.000Z",
    "updatedAt": "2026-04-06T16:52:00.000Z",
    "audits": [],
    "lengthTelemetry": {
      "target": 3000,
      "writerCount": 2102,
      "finalCount": 2102
    }
  }
]
EOF

echo "   已创建新的索引文件"
cp index_new.json index.json
echo "   ✅ index.json 已更新"

echo ""
echo "4. 启动 inkos..."
cd /root/.openclaw/workspace/tomato-novel
nohup inkos up > /tmp/inkos_final_start.log 2>&1 &

sleep 10

echo ""
echo "5. 检查 inkos 状态..."
if ps aux | grep -q "inkos up" | grep -v grep; then
    PID=$(ps aux | grep "inkos up" | grep -v grep | awk '{print $2}')
    echo "   ✅ inkos 已启动 (PID: $PID)"
    echo "   日志: /tmp/inkos_final_start.log"
else
    echo "   ❌ inkos 启动失败"
    echo "   请检查日志: tail -50 /tmp/inkos_final_start.log"
fi

echo ""
echo "6. 检查inkos状态..."
inkos status 2>&1

echo ""
echo "========================================"
echo "修复完成!"
echo "========================================"
echo ""
echo "下一步建议:"
echo "1. 等待5-10分钟让 inkos 稳定运行"
echo "2. 使用 'inkos draft 末日重生-开局囤货十亿物资' 测试创作"
echo "3. 检查 inkos 日志: tail -100 /tmp/inkos_final_start.log"
echo "4. 监控 inkos 进程: ps aux | grep inkos"