#!/bin/zsh

# Check for correct number of arguments
if [[ $# -ne 3 ]]; then
    echo "Usage: $0 <filepath> <start [sec]> <end [sec]>"
    exit 1
fi

# Assign arguments to variables
INPUT_PATH="$1"
START_TIME="$2"
END_TIME="$3"

# Convert seconds to nanoseconds
START_TIME_NANO=$((START_TIME * 1000000000))
END_TIME_NANO=$((END_TIME * 1000000000))

# Extract directory and filename from input path
DIR_NAME=$(dirname "$INPUT_PATH")
BASE_NAME=$(basename "$INPUT_PATH")

# Modify directory to create output path
OUTPUT_DIR=${DIR_NAME/original_untrimmed/trimmed}
OUTPUT_PATH="$OUTPUT_DIR/$BASE_NAME"

# Create the output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"

# Run the filtering command
~/mcap-linux-amd64 filter -S "$START_TIME_NANO" -E "$END_TIME_NANO" --include-metadata "$INPUT_PATH" -o "$OUTPUT_PATH"

# Print success message
echo "Filtered file saved to: $OUTPUT_PATH"

