#! /usr/bin/env bash

# Check if a directory argument is given
if [[ -z "$1" ]]; then
    echo "Usage: $0 <directory>"
    exit 1
fi

LOG_DIR="$1"

# Extract GPU name and block size (only once)
GPU=$(grep -h "GPU:" "$LOG_DIR"/* | head -n 1 | awk -F": " '{print $3}')
BLOCK_SIZE=$(grep -h "Block size:" "$LOG_DIR"/* | head -n 1 | awk -F": " '{print $3}')

# Print Header
printf "%-4s %s\n" "GPU:" "${GPU:-Unknown}"
printf "%-8s %s\n" "Block size:" "${BLOCK_SIZE:-Unknown}"
printf "%s\n" "--------------------------------------"

# Loop through code versions
for VERSION in V0 V1 V2; do
    printf "\n%-15s %s\n" "Code version:" "$VERSION"
    printf "%-7s %-18s %-18s %-18s\n" "" "Total" "Mem-xch" "Sorting"

    for Q in {20..30}; do
        FILE=$(grep -l "\[Log\]: Code version:   $VERSION" "$LOG_DIR"/* | xargs grep -l "Q=$Q")

        if [[ -n "$FILE" ]]; then
              TOTAL=$(grep "\[Timing\] Total"   "$FILE" | awk '{print $4, $5}')
            MEM_XCH=$(grep "\[Timing\] Mem-xch" "$FILE" | awk '{print $4, $5}')
            SORTING=$(grep "\[Timing\] Sorting" "$FILE" | awk '{print $4, $5}')
        else
              TOTAL="N/A"
            MEM_XCH="N/A"
            SORTING="N/A"
        fi

        printf "Q%-2d:    %-18s %-18s %-18s\n" "$Q" "$TOTAL" "$MEM_XCH" "$SORTING"
    done
done

