#!/bin/bash
set -e

# 进入工作区目录
cd /root/.openclaw/workspace

# 1. 确保敏感文件不会被提交到Git
if ! grep -q "secrets.md" .gitignore; then
    echo "secrets.md" >> .gitignore
    echo "*.env" >> .gitignore
    echo "memory/*.private.md" >> .gitignore
fi

# 2. 提交所有变更
git add .
git commit -m "🤖 每日自动备份 - $(date +'%Y-%m-%d %H:%M:%S')" || echo "没有新的变更需要提交"

# 3. 推送到远程仓库
if git push; then
    echo "Git推送成功 - $(date +'%Y-%m-%d %H:%M:%S')"
    # 发送备份成功通知
    /root/.nvm/versions/node/v24.14.0/bin/node /root/.nvm/versions/node/v24.14.0/bin/openclaw message send --channel feishu --target user:ou_9cb5bc9a5f1b6cab2d78fd36139ecb87 --message "✅ 每日工作区备份成功！所有变更已同步到Git仓库，备份时间：$(date +'%Y-%m-%d %H:%M:%S')"
else
    echo "Git推送失败 - $(date +'%Y-%m-%d %H:%M:%S')"
    # 发送备份失败通知
    /root/.nvm/versions/node/v24.14.0/bin/node /root/.nvm/versions/node/v24.14.0/bin/openclaw message send --channel feishu --target user:ou_9cb5bc9a5f1b6cab2d78fd36139ecb87 --message "❌ 每日工作区备份失败！请检查Git仓库配置和网络连接，错误时间：$(date +'%Y-%m-%d %H:%M:%S')"
fi
