#!/bin/bash

# ============================================
# 配置定时自动回测
# 安装：./auto_backtest_crontab.sh install
# 查看：crontab -l
# 卸载：./auto_backtest_crontab.sh uninstall
# ============================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CI_CD_SCRIPT="$SCRIPT_DIR/sanguo_nas_ci_cd.sh"
CRON_ENTRY="0 8 * * * $CI_CD_SCRIPT >> $SCRIPT_DIR/auto_backtest.log 2>&1"

install() {
    echo "安装定时自动回测..."
    echo ""
    
    # 添加到 crontab
    (crontab -l 2>/dev/null; echo "$CRON_ENTRY") | crontab -
    
    echo ""
    echo "✅ 安装完成！"
    echo "每天早上 8:00 自动运行一次全流程回测"
    echo "日志位置: $SCRIPT_DIR/auto_backtest.log"
    echo ""
}

uninstall() {
    echo "卸载定时自动回测..."
    echo ""
    
    # 移除 crontab
    crontab -l 2>/dev/null | grep -v "sanguo_nas_ci_cd.sh" | crontab -
    
    echo ""
    echo "✅ 卸载完成！"
    echo ""
}

status() {
    echo "当前定时任务:"
    echo ""
    crontab -l | grep "sanguo"
    echo ""
}

case "${1:-help}" in
    install)
        install
        ;;
    uninstall)
        uninstall
        ;;
    status)
        status
        ;;
    help)
        echo "使用方法:"
        echo "  $0 install      - 安装每天 8:00 自动回测"
        echo "  $0 uninstall    - 卸载定时回测"
        echo "  $0 status      - 查看当前状态"
        echo ""
        ;;
esac
