#!/system/bin/sh

# 配置参数
TARGET_DIRS="/data/user/0/com.netease.yyslscn/app_ntp0
/data/user/0/com.netease.yyslscn/app_so_uf"  # 多目录支持
CLEAN_INTERVAL=1

# 初始化参数
CLEAN_COUNT=0
TOTAL_DELETED=0  # 新增：累计删除总数
START_TIME=$(date +%s)

# 状态显示函数
show_status() {
    current_time=$(date "+%Y-%m-%d %H:%M:%S")
    elapsed_time=$(( $(date +%s) - START_TIME ))
    
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "[ T${CLEAN_COUNT} ] ${current_time}"
    echo "├─ 运行时长：${elapsed_time} 秒"
    echo "├─ 清理次数：${CLEAN_COUNT} 次"
    echo "├─ 累计删除：${TOTAL_DELETED} 个文件"
    echo "├─ 监控目录："
    while read -r dir; do
        [ -n "$dir" ] && echo "│   ${dir}"
    done <<< "$TARGET_DIRS"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}

# 主循环
while true
do
    current_deleted=0  # 本次清理的文件数
    valid_clean=0
    
    # 多目录清理
    while read -r target_dir
    do
        [ -z "$target_dir" ] && continue
        
        if [ -d "$target_dir" ]; then
            # 计算本次删除的文件数并累加到总计数
            deleted=$(find "$target_dir" -mindepth 1 -maxdepth 1 | wc -l)
            current_deleted=$((current_deleted + deleted))
            # 执行删除操作
            find "$target_dir" -mindepth 1 -maxdepth 1 -exec rm -rf {} \;
            valid_clean=1
        fi
    done <<< "$TARGET_DIRS"

    # 更新计数器
    if [ "$valid_clean" -eq 1 ]; then
        CLEAN_COUNT=$((CLEAN_COUNT + 1))
        TOTAL_DELETED=$((TOTAL_DELETED + current_deleted))  # 累加到总删除数
    fi

    # 状态显示
    clear
    show_status
    echo "本次清理文件数：${current_deleted} 个（累计：${TOTAL_DELETED}）"
    echo "下次清理倒计时："
    for i in $(seq "$CLEAN_INTERVAL" -1 1); do
        printf "\r%02d 秒后刷新" "$i"
        sleep 1
    done
done