#!/bin/bash

# Set the size threshold in bytes (e.g., 10MB = 10 * 1024 * 1024)
THRESHOLD=$((10 * 1024 * 1024))

# Ensure Git LFS is initialized
git lfs install --skip-smudge

# Find large files in the current commit and throughout history
echo "Identifying large files exceeding $((THRESHOLD / 1024 / 1024))MB..."
large_files=$(find . -type f -size +${THRESHOLD}c ! -path "./.git/*")

if [ -z "$large_files" ]; then
  echo "No files larger than $((THRESHOLD / 1024 / 1024))MB found."
  exit 0
fi

# Track large files with Git LFS
for file in $large_files; do
  if git check-ignore -q "$file"; then
    continue
  fi
  echo "Tracking large file with Git LFS: $file"
  git rm --cached "$file" >/dev/null 2>&1 ## Untrack the file from git
  echo "[LFS]" $(git lfs track "$file")   ## Track the file with git-lfs
done

# Add changes to .gitattributes
if [ -f .gitattributes ]; then
  git add .gitattributes
else
  echo "No .gitattributes file found; no changes made."
fi
