#!/bin/bash
# Version: 1.1.0
# Author: Tomi Eckert

# Check if any arguments were provided
if [ "$#" -eq 0 ]; then
  echo "Usage: $0 <retention_days>:<path> [<retention_days>:<path> ...]"
  exit 1
fi

# Loop through each argument provided
for ARG in "$@"; do
  # Split the argument at the first colon
  IFS=':' read -r RETENTION_DAYS TARGET_DIR <<< "$ARG"

  # Validate that both a retention period and a path were provided
  if [ -z "$RETENTION_DAYS" ] || [ -z "$TARGET_DIR" ]; then
    echo "Invalid format for argument: $ARG. Please use the format <retention_days>:<path>"
    continue
  fi

  echo "Starting cleanup of files older than $RETENTION_DAYS days in $TARGET_DIR..."

  # Use find to locate and delete files, handling potential errors
  find "$TARGET_DIR" -type f -mtime +"$RETENTION_DAYS" -delete -print || echo "Could not process $TARGET_DIR. Check permissions."

  echo "Cleanup complete for $TARGET_DIR."
  echo "--------------------------------------------------"
done

echo "All cleanup tasks finished."