#!/bin/bash

# Source the setup.bash file
source /opt/ros/noetic/setup.bash

# Check if the first argument (directory path) is provided
if [ $# -lt 1 ]; then
    echo "Usage: $0 <directory_path>"
    exit 1
fi

# Use the first argument as the bags directory
bags_directory="$1"

# Output CSV file path
# output_csv="${bags_directory}bag_info.csv"
output_csv="bag_info.csv"

# Write CSV header
echo "Path,Duration (s),Size (GB)" > "$output_csv"

# Iterate through the bags directory
for bag_file in "$bags_directory"*.bag; do
    # Run rosbag info command and extract desired information
    info_output=$(rosbag info --yaml "$bag_file")

    # Extract path
    path=$(echo "$info_output" | awk '/path:/{print $2}')

    # Extract duration in seconds
    duration_seconds=$(echo "$info_output" | awk '/duration:/{print $2}' | cut -d 's' -f 1)

    # Extract size in bytes
    size_bytes=$(echo "$info_output" | awk '/size:/{print $2}')

    # Convert size to GB
    size_gb=$(awk "BEGIN { printf \"%.2f\", $size_bytes / (1024^3) }")

    # Write bag information to CSV file
    echo "$path,$duration_seconds,$size_gb"
    echo "$path,$duration_seconds,$size_gb" >> "$output_csv"
done

echo "Bag information saved to CSV file: $output_csv"
