#!/bin/bash

# Directory path on your server
TARGET_DIRECTORY="/data/dmz/www/data/cdp_spire/"

# Track overall grand totals
grand_total_files=0
grand_total_bytes=0

# Declare associative arrays for daily metrics (Requires Bash 4+)
declare -A daily_count
declare -A daily_bytes

# Check if directory exists
if [ ! -d "$TARGET_DIRECTORY" ]; then
    echo "Error: The directory '$TARGET_DIRECTORY' does not exist."
    exit 1
fi

# Enable nullglob so the loop doesn't run if no .nc files are found
shopt -s nullglob

# Read and process files
for file_path in "$TARGET_DIRECTORY"/*.nc; do
    filename=$(basename "$file_path")

    # Regex to match the first 8-digit date string followed by 'T'
    # Equivalent to Python's re.search(r"_(\d{8})T", filename)
    if [[ "$filename" =~ _([0-9]{8})T ]]; then
        date_str="${BASH_REMATCH[1]}"
        
        # Format date to YYYY-MM-DD using string slicing
        formatted_date="${date_str:0:4}-${date_str:4:2}-${date_str:6:2}"

        # Get file size in bytes (Linux stat syntax)
        if file_size_bytes=$(stat -c %s "$file_path" 2>/dev/null); then
            
            # Update daily metrics
            daily_count["$formatted_date"]=$(( ${daily_count["$formatted_date"]:-0} + 1 ))
            daily_bytes["$formatted_date"]=$(( ${daily_bytes["$formatted_date"]:-0} + file_size_bytes ))

            # Update grand totals
            ((grand_total_files++))
            grand_total_bytes=$((grand_total_bytes + file_size_bytes))
        else
            echo "Error reading file $filename" >&2
        fi
    fi
done

# --- Print Daily Breakdown ---
printf '%.0s=' {1..60}; echo
printf "%-15s | %-15s | %-15s\n" "DATE" "DATASET COUNT" "DAILY SIZE"
printf '%.0s=' {1..60}; echo

# Sort the associative array keys (dates) and loop through them
if [ ${#daily_count[@]} -gt 0 ]; then
    for day in $(printf '%s\n' "${!daily_count[@]}" | sort); do
        count=${daily_count[$day]}
        bytes=${daily_bytes[$day]}

        # Convert bytes to Gigabytes (GB) using awk for floating point math
        daily_size_gb=$(awk -v b="$bytes" 'BEGIN {printf "%.3f", b / (1024^3)}')
        
        printf "%-15s | %-15s | %s GB\n" "$day" "$count" "$daily_size_gb"
    done
fi

# --- Print Grand Totals ---
printf '%.0s-' {1..60}; echo
total_size_gb=$(awk -v b="$grand_total_bytes" 'BEGIN {printf "%.3f", b / (1024^3)}')
printf "%-15s | %-15s | %s GB\n" "GRAND TOTAL" "$grand_total_files" "$total_size_gb"
printf '%.0s=' {1..60}; echo
