#!/bin/bash
set -e

# Ensure proper permissions for Laravel directories
echo "Setting up Laravel permissions..."

# Create directories if they don't exist (if we have permissions)
mkdir -p /var/www/html/bootstrap/cache 2>/dev/null || true
mkdir -p /var/www/html/storage/framework/sessions 2>/dev/null || true
mkdir -p /var/www/html/storage/framework/views 2>/dev/null || true
mkdir -p /var/www/html/storage/framework/cache 2>/dev/null || true
mkdir -p /var/www/html/storage/logs 2>/dev/null || true
mkdir -p /var/www/html/storage/app/public 2>/dev/null || true

# Set permissions (the container already runs as APP_USER from docker-compose.yml)
# No chown needed since the user is set at container level
chmod -R 775 /var/www/html/bootstrap/cache 2>/dev/null || true
chmod -R 775 /var/www/html/storage 2>/dev/null || true

echo "Permissions set successfully!"

# Execute the main command
exec "$@"
