#!/bin/bash

# 交互式命令行模式
interactive_mode() {
    while true; do
        read -p "Enter command (c: clear log, p: print log, l: less log, t: tail log, q: quit): " cmd
        case $cmd in
            c)
                echo "Clearing log..."
                > storage/logs/laravel.log
                ;;
            p)
                cat storage/logs/laravel.log
                ;;
            l)
                less storage/logs/laravel.log
                ;;
            t)
                tail -f -n 1000 storage/logs/laravel.log
                ;;
            q)
                exit 0
                ;;
            *)
                echo "Invalid command. Use 'c' to clear, 'p' to print, 'l' to less, 't' to tail, or 'q' to quit."
                ;;
        esac
    done
}

# 处理命令行参数
if [ $# -eq 0 ]; then
    interactive_mode
else
    case "$1" in
        -c)
            echo "Clearing log..."
            > storage/logs/laravel.log
            ;;
        -p)
            cat storage/logs/laravel.log
            ;;
        -l)
            less storage/logs/laravel.log
            ;;
        -t)
            tail -f -n 1000 storage/logs/laravel.log
            ;;
        -h)
            echo "Usage: $0 [-c|-p|-l|-t|-h]"
            echo "  -c: clear log"
            echo "  -p: print log"
            echo "  -l: less log"
            echo "  -t: tail log (last 1000 lines, follow mode)"
            echo "  -h: show this help message"
            echo "  No arguments: enter interactive mode"
            ;;
        *)
            echo "Invalid option. Use '$0 -h' for help."
            exit 1
            ;;
    esac
fi
