#!/bin/bash
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
OUTPUT_FILE="$SCRIPT_DIR/old_images.txt"

# Change to the IoT directory
cd "$SCRIPT_DIR"

# Find all image files older than 14 days in images directory and subdirectories
# Using -iname for case-insensitive matching
find images -type f \
    \( -iname "*.jpg" -o -iname "*.jpeg" \
    -o -iname "*.png" -o -iname "*.bmp" \) \
    -mtime +14 \
    -printf "%P\n" > "${OUTPUT_FILE}.tmp"

# Only update the file if we found anything or if the original doesn't exist
if [ -s "${OUTPUT_FILE}.tmp" ] || [ ! -f "$OUTPUT_FILE" ]; then
    mv "${OUTPUT_FILE}.tmp" "$OUTPUT_FILE"
else
    rm "${OUTPUT_FILE}.tmp"
fi