#!/bin/bash

echo "=== 启动简单 inkos 创作 ==="

# 1. 确保inkos已停止
echo "1. 停止inkos..."
pkill -f "node.*inkos" 2>/dev/null || true
sleep 2

# 2. 创建最简单的配置
echo "2. 创建简单配置..."
cat > /root/.openclaw/workspace/tomato-novel/inkos_simple_start.json << 'EOF'
{
  "name": "tomato-novel-simple-start",
  "version": "0.1.0",
  "language": "zh",
  "llm": {
    "provider": "custom",
    "baseUrl": "https://ark.cn-beijing.volces.com/api/coding/v3",
    "model": "deepseek-v3.2"
  },
  "daemon": {
    "schedule": {
      "writeCron": "0 * * * *"
    },
    "maxConcurrentBooks": 1
  },
  "quality": {
    "enforcement": "none"
  }
}
EOF

# 3. 使用简单配置
cp /root/.openclaw/workspace/tomato-novel/inkos_simple_start.json /root/.openclaw/workspace/tomato-novel/inkos.json

# 4. 启动inkos（不进入守护进程模式，直接创作）
echo "3. 启动inkos创作第144章..."
cd /root/.openclaw/workspace/tomato-novel

# 先尝试直接启动守护进程
nohup inkos up > /tmp/inkos_simple.log 2>&1 &
sleep 5

# 检查是否启动
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)"
    
    # 等待一下，然后尝试创作
    sleep 5
    echo "4. 尝试创作下一章..."
    
    # 使用draft命令
    echo "尝试使用 draft 命令..."
    inkos draft 末日重生-开局囤货十亿物资 2>&1 | head -50
    
else
    echo "❌ inkos 启动失败"
    echo "查看日志: tail -50 /tmp/inkos_simple.log"
fi

echo ""
echo "=== 完成 ==="
echo "如果创作成功，inkos将继续自动创作"
echo "如果失败，可能需要更深入的修复"