#!/bin/bash
# ==============================================
# 🔧 Fix file permissions for Laravel Docker
# ==============================================

current_user=$(whoami)
echo "Setting ACLs for user: $current_user"

# Ensure user exists
if ! id -u "$current_user" > /dev/null 2>&1; then
  echo "Error: user '$current_user' does not exist. Exiting."
  exit 1
fi

# Apply ACL to ./app
sudo setfacl -R -m u:"$current_user":rwx ./app || echo "⚠️ ACL failed, you may need sudo"

# Fix ownership for all files inside app
sudo chown -R $(id -u):$(id -g) ./app

# Optional: fix storage & cache permissions
if [ -d ./app/workopia/storage ]; then
    chmod -R 775 ./app/workopia/storage
fi

if [ -d ./app/workopia/bootstrap/cache ]; then
    chmod -R 775 ./app/workopia/bootstrap/cache
fi

echo "✅ Permissions fixed."
