#!/bin/bash

# ================================
# Laravel Permission Fix Script
# ================================

# Set project path (auto-detect if script is inside the project)
PROJECT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "Fixing permissions for: $PROJECT_PATH"
sleep 1

echo "Changing ownership to current user for project files..."
sudo chown -R $(whoami):$(whoami) "$PROJECT_PATH"

echo "Assigning www-data ownership to storage and cache..."
sudo chown -R www-data:www-data "$PROJECT_PATH/storage" "$PROJECT_PATH/bootstrap/cache"

echo "Setting file permissions (644)..."
sudo find "$PROJECT_PATH" -type f -exec chmod 644 {} \;

echo "Setting directory permissions (755)..."
sudo find "$PROJECT_PATH" -type d -exec chmod 755 {} \;

echo "Making storage and cache writable (775)..."
sudo chmod -R 775 "$PROJECT_PATH/storage" "$PROJECT_PATH/bootstrap/cache"

echo "Fixing .git permissions..."
sudo chown -R $(whoami):$(whoami) "$PROJECT_PATH/.git"
sudo chmod -R u+rwX "$PROJECT_PATH/.git"

echo ""
echo "---------------------------------------"
echo " Permissions fixed successfully! "
echo "---------------------------------------"
