#!/bin/bash

echo "Finding actual duplicates - this may take a few minutes..."

# Create output files
> duplicates_by_size.txt
> duplicates_by_name.txt
> file_inventory.txt

# Find files with same names (likely duplicates)
echo "=== DUPLICATE FILENAMES ===" > duplicates_by_name.txt
find Photography Videography -type f \( -name "*.jpg" -o -name "*.jpeg" -o -name "*.mp4" -o -name "*.mov" -o -name "*.cr2" \) -print0 | \
  xargs -0 -I {} basename {} | sort | uniq -d | while read filename; do
    echo "--- Files named: $filename ---" >> duplicates_by_name.txt
    find Photography Videography -name "$filename" -type f >> duplicates_by_name.txt
    echo "" >> duplicates_by_name.txt
done

# Find files with same sizes (potential duplicates)
echo "=== FILES WITH SAME SIZES ===" > duplicates_by_size.txt
find Photography Videography -type f \( -name "*.jpg" -o -name "*.jpeg" -o -name "*.mp4" -o -name "*.mov" -o -name "*.cr2" \) -exec stat -f "%z %N" {} \; | \
  sort -n | awk '{
    if ($1 == prev_size && $1 > 1000000) {  # Only show files > 1MB
      if (!printed_size) {
        print "Size: " prev_size " bytes"
        print "  " prev_file
        printed_size = 1
      }
      print "  " $2
    } else {
      printed_size = 0
      prev_size = $1
      prev_file = $2
    }
  }' >> duplicates_by_size.txt

# Create inventory by year
echo "=== 2024 FILES ===" > file_inventory.txt
find Photography -path "*2024*" -type f | wc -l | xargs echo "Photography 2024 files:" >> file_inventory.txt
find Videography -path "*2024*" -type f | wc -l | xargs echo "Videography 2024 files:" >> file_inventory.txt

echo "=== 2025 FILES ===" >> file_inventory.txt  
find Photography -path "*2025*" -type f | wc -l | xargs echo "Photography 2025 files:" >> file_inventory.txt
find Videography -path "*2025*" -type f | wc -l | xargs echo "Videography 2025 files:" >> file_inventory.txt

echo "=== 2026 FILES ===" >> file_inventory.txt
find Photography -path "*2026*" -type f | wc -l | xargs echo "Photography 2026 files:" >> file_inventory.txt

echo "Done! Check these files:"
echo "- duplicates_by_name.txt (same filenames)"  
echo "- duplicates_by_size.txt (same file sizes)"
echo "- file_inventory.txt (counts by year)"

ls -la *.txt