#!/bin/bash
#
# Fix Laravel Storage and Cache Permissions
# Run this script if you experience permission issues with cache or storage
#

echo "Fixing Laravel permissions..."

# Set ownership
echo "Setting ownership to asterisk:asterisk..."
chown -R asterisk:asterisk /var/www/ccsystem/storage
chown -R asterisk:asterisk /var/www/ccsystem/bootstrap/cache

# Set permissions
echo "Setting permissions to 775..."
chmod -R 775 /var/www/ccsystem/storage
chmod -R 775 /var/www/ccsystem/bootstrap/cache

# Ensure subdirectories maintain permissions
echo "Setting directory permissions..."
find /var/www/ccsystem/storage -type d -exec chmod 775 {} \;
find /var/www/ccsystem/bootstrap/cache -type d -exec chmod 775 {} \;

# Set file permissions
echo "Setting file permissions..."
find /var/www/ccsystem/storage -type f -exec chmod 664 {} \;
find /var/www/ccsystem/bootstrap/cache -type f -exec chmod 664 {} \;

echo "Permissions fixed successfully!"
echo ""
echo "Note: With umask(0002) now set in bootstrap/app.php, artisan, and public/index.php,"
echo "new cache files and directories will automatically be created with correct permissions."
