#!/bin/bash

# Get today's date in YYYYMMDD format
TODAY=$(date +%Y%m%d)

# Base logs directory
BASE_DIR="/var/www/html/workers/logs"

# Loop through each subdirectory
for PROJECT_DIR in "$BASE_DIR"/*/; do
    # Check if it's actually a directory
    [ -d "$PROJECT_DIR" ] || continue

    echo "Processing: $PROJECT_DIR"

    # Define full paths
    ERROR_LOG="${PROJECT_DIR}error_log.log"
    OUTPUT_LOG="${PROJECT_DIR}output_log.log"

    # Copy and truncate error_log.log if it exists
    if [ -f "$ERROR_LOG" ]; then
        sudo cp "$ERROR_LOG" "${PROJECT_DIR}${TODAY}_error_log.log"
        sudo truncate -s0 "$ERROR_LOG"
        echo "  -> error_log.log copied and truncated."
    else
        echo "  -> error_log.log not found."
    fi

    # Copy and truncate output_log.log if it exists
    if [ -f "$OUTPUT_LOG" ]; then
        sudo cp "$OUTPUT_LOG" "${PROJECT_DIR}${TODAY}_output_log.log"
        sudo truncate -s0 "$OUTPUT_LOG"
        echo "  -> output_log.log copied and truncated."
    else
        echo "  -> output_log.log not found."
    fi
done
