#!/bin/bash

# 使用方法:
# bash ./clear.sh

bt_base_path="/www/wwwroot/"
# bt_base_path="/Users/leek/Sites/"
bt_www_log_path="/www/wwwlogs/"

java_services_base_path="/home/app/services/"
# java_services_base_path="/Users/leek/Sites/"

function readDir() {
    # echo -e "\033[30m 黑色字 \033[0m"
    # echo -e "\033[31m 红色字 \033[0m"
    # echo -e "\033[32m 绿色字 \033[0m"
    # echo -e "\033[33m 黄色字 \033[0m"
    # echo -e "\033[34m 蓝色字 \033[0m" 
    # echo -e "\033[35m 紫色字 \033[0m" 
    # echo -e "\033[36m 天蓝字 \033[0m" 
    # echo -e "\033[37m 白色字 \033[0m"

    time=$(date "+%Y%m%d %H:%M:%S")
    echo -e "\033[33m 开始时间：$time \033[0m"
    echo ''
    echo ''

    base_path=$bt_base_path

    echo '----------开始清理laravel项目----------'
    laravel_dir=$(ls -l $base_path |awk '/^d/ {print $NF}')


    # for i in $laravel_dir
    # do
    #     echo $i
    # done 

    for project_name in $(ls $base_path); do
        echo -e "\033[36m 项目名称：$project_name \033[0m" 

        project_path=$base_path$project_name

        project_artisan_path=$project_path/artisan
        # echo $project_artisan_path

        project_process_log_path=$bt_www_log_path$project_name.log
        project_error_log_path=$bt_www_log_path$project_name.error.log
        # echo $project_process_log_path
        # echo $project_error_log_path

        if [ -f "$project_artisan_path" ]; then
            echo -e "\033[35m $project_name 项目找到 artisan 文件，继续执行 \033[0m"

            if [ -f "$project_process_log_path" ]; then
                echo -e "\033[35m $project_name 项目找到 nginx 日志，开始清理 nginx 日志 \033[0m"

                echo "" >$project_process_log_path
                echo "" >$project_error_log_path

                echo -e "\033[32m $project_name 项目完成清理 nginx 日志 \033[0m"
            else
                echo -e "\033[31m $project_name 项目未找到 nginx 日志，跳过 \033[0m"
            fi
        else
            echo -e "\033[31m $project_name 项目未找到 artisan 文件，跳过 \033[0m"
        fi

        # echo $project_path/storage/logs/laravel.log

        if [ -f "$project_artisan_path" ]; then
            echo -e "\033[34m $project_name 项目清理前占用磁盘大小为 $(du -sh $project_path) \033[0m" 
         
            project_laravel_log_path=$project_path/storage/logs/laravel.log
            project_wechat_log_path=$project_path/storage/logs/wechat.log
            project_alipay_log_path=$project_path/storage/logs/alipay.log

            # echo $project_laravel_log_path

            if [ -f "$project_laravel_log_path" ]; then
                echo -e "\033[35m $project_name 项目开始清理 laravel single 日志 \033[0m"
                echo "" >$project_laravel_log_path
                echo -e "\033[32m $project_name 项目完成清理 laravel single 日志 \033[0m"
            fi

            if [ -f "$project_wechat_log_path" ]; then
                echo -e "\033[35m $project_name 项目开始清理 wechat single 日志 \033[0m"
                echo "" >$project_wechat_log_path
                echo -e "\033[32m $project_name 项目完成清理 wechat single 日志 \033[0m"
            fi

            if [ -f "$project_alipay_log_path" ]; then
                echo -e "\033[35m $project_name 项目开始清理 alipay single 日志 \033[0m"
                echo "" >$project_alipay_log_path
                echo -e "\033[32m $project_name 项目完成清理 alipay single 日志 \033[0m"
            fi
            
            project_daliy_laravel_log_folder_path=$project_path/storage/logs/

            echo -e "\033[35m $project_name 项目开始清理 laravel daily 日志 \033[0m"
            find $project_daliy_laravel_log_folder_path -mtime +1 -name "laravel-*.log"  -exec rm -f {} \;
            echo -e "\033[32m $project_name 项目完成清理 laravel daily 日志 \033[0m"

            echo -e "\033[35m $project_name 项目开始清理 wechat daily 日志 \033[0m"
            find $project_daliy_laravel_log_folder_path -mtime +1 -name "wechat-*.log"  -exec rm -f {} \;
            echo -e "\033[32m $project_name 项目完成清理 wechat daily 日志 \033[0m"

            echo -e "\033[34m $project_name 项目清理后占用磁盘大小为 $(du -sh $project_path) \033[0m" 
        fi

        echo ''
        echo ''
    done
    echo '----------结束清理laravel项目----------'

    
    echo ''
    echo ''
    echo ''
    echo ''
    echo ''

    echo '----------开始清理java项目----------'
    java_dir=$(ls -l $base_path |awk '/^d/ {print $NF}')

    # for i in $java_dir
    # do
    #     echo $i
    # done 

    for project_name in $(ls $java_services_base_path); do
        echo -e "\033[36m 项目名称：$project_name \033[0m" 

        project_path=$java_services_base_path$project_name

        # echo $project_path
       

        project_output_path=$project_path/output
        project_logs_path=$project_path/logs/
        # echo $project_output_path
        # echo $project_logs_path

        if [ -f "$project_output_path" ] || [ -d "$project_logs_path" ]; then
            echo -e "\033[34m $project_name 项目清理前占用磁盘大小为 $(du -sh $project_path) \033[0m" 

            if [ -f "$project_output_path" ]; then
                echo -e "\033[35m $project_name 项目找到 output 文件，开始清理 output 日志 \033[0m"
                echo "" >$project_output_path;
                echo -e "\033[32m $project_name 项目完成清理 output 日志 \033[0m"
            else
                echo -e "\033[31m $project_name 项目未找到 output 日志，跳过 \033[0m"
            fi

            if [ -d "$project_logs_path" ]; then
                echo -e "\033[35m $project_name 项目找到 logs 目录，开始清理 logs 目录日志 \033[0m"

                project_logs_web_debug_path=$project_path/logs/web_debug.log
                project_logs_web_error_path=$project_path/logs/web_error.log
                project_logs_web_info_path=$project_path/logs/web_info.log

                if [ -f "$project_logs_web_debug_path" ]; then
                    echo "" >$project_logs_web_debug_path;
                fi

                if [ -f "$project_logs_web_error_path" ]; then
                    echo "" >$project_logs_web_error_path;
                fi

                if [ -f "$project_logs_web_info_path" ]; then
                    echo "" >$project_logs_web_info_path;
                fi

                find $project_logs_path -mtime +1 -name "info-*.log"  -exec rm -f {} \;
                find $project_logs_path -mtime +1 -name "error-*.log"  -exec rm -f {} \;
                echo -e "\033[32m $project_name 项目完成清理 logs 目录日志 \033[0m"
            else
                echo -e "\033[31m $project_name 项目未找到 logs 目录，跳过 \033[0m"
            fi

            echo -e "\033[34m $project_name 项目清理后占用磁盘大小为 $(du -sh $project_path) \033[0m" 
        else
            echo -e "\033[31m $project_name 项目未找到相关日志文件，跳过 \033[0m"
        fi

        echo ''
        echo ''
    done

    echo '----------结束清理java项目----------'

    echo ''
    echo ''
    time=$(date "+%Y%m%d %H:%M:%S")
    echo -e "\033[33m 结束时间：$time \033[0m"
}

readDir
