#!/bin/bash

# Check if a filename was provided
if [ -z "$1" ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

TARGET="$1"

# Check if the file actually exists
if [ ! -f "$TARGET" ]; then
    echo "Error: File '$TARGET' not found."
    exit 1
fi

# Get creation time (%W).
# Note: Returns 0 or '-' if the filesystem doesn't support birth time.
BTIME=$(stat -c %W "$TARGET")

# Fallback to last modification time (%Y) if birth time is unavailable
if [ "$BTIME" -eq 0 ] || [ "$BTIME" == "-" ]; then
    BTIME=$(stat -c %Y "$TARGET")
fi

# Calculate the minute-based timestamp (equivalent to Math.floor(ms / 60000))
# Since stat returns seconds, we divide by 60.
FORMATTED_DATE=$(( BTIME / 60 ))

# Define the new name
NEW_NAME="${FORMATTED_DATE} - ${TARGET}"

# Perform the rename
mv "$TARGET" "$NEW_NAME"

echo "Renamed: '$TARGET' -> '$NEW_NAME'"
