#!/bin/bash

# HTMLPurifier Cache Directory Setup Script
# This script ensures the HTMLPurifier cache directory has correct permissions
# Can be run standalone or called from deploy.sh
#
# Usage:
#   ./deployment-htmlpurifier-fix.sh [WEB_USER] [WEB_GROUP]
#
# Arguments:
#   WEB_USER  - Web server user (optional, will auto-detect if not provided)
#   WEB_GROUP - Web server group (optional, defaults to WEB_USER)
#

# Exit codes: 0=success, continue even if errors occur
set +e

# Parse arguments
PASSED_WEB_USER="${1:-}"
PASSED_WEB_GROUP="${2:-}"

# Get the web server user from arguments or auto-detect
if [ -n "$PASSED_WEB_USER" ]; then
    WEB_USER="$PASSED_WEB_USER"
    echo "Using provided web server user: $WEB_USER"
else
    WEB_USER=$(ps aux | grep -E 'apache|nginx|httpd' | grep -v grep | head -1 | awk '{print $1}')

    if [ -z "$WEB_USER" ]; then
        echo "WARNING: Could not detect web server user automatically."
        echo "Defaulting to www-data. If this is incorrect, run: ./deployment-htmlpurifier-fix.sh YOUR_WEB_USER"
        WEB_USER="www-data"
    else
        echo "Auto-detected web server user: $WEB_USER"
    fi
fi

# Get web server group
if [ -n "$PASSED_WEB_GROUP" ]; then
    WEB_GROUP="$PASSED_WEB_GROUP"
else
    WEB_GROUP="$WEB_USER"
fi

echo "Using web server group: $WEB_GROUP"
echo ""

# Create cache directory if it doesn't exist
CACHE_DIR="storage/framework/cache/htmlpurifier"

if [ ! -d "$CACHE_DIR" ]; then
    echo "Creating HTMLPurifier cache directory..."
    if mkdir -p "$CACHE_DIR" 2>/dev/null; then
        echo "Directory created: $CACHE_DIR"
    else
        echo "WARNING: Failed to create directory (may already exist or permission issue)"
    fi
else
    echo "Cache directory already exists: $CACHE_DIR"
fi

# Set ownership (try with and without sudo)
echo ""
echo "Setting ownership to $WEB_USER:$WEB_GROUP..."

if chown -R "$WEB_USER:$WEB_GROUP" "$CACHE_DIR" 2>/dev/null; then
    echo "Ownership set successfully (without sudo)"
elif sudo chown -R "$WEB_USER:$WEB_GROUP" "$CACHE_DIR" 2>/dev/null; then
    echo "Ownership set successfully (with sudo)"
else
    echo "WARNING: Could not set ownership. Directory may still work if already writable."
fi

# Set permissions (try with and without sudo)
echo "Setting permissions to 755..."
if chmod -R 755 "$CACHE_DIR" 2>/dev/null; then
    echo "Permissions set successfully (without sudo)"
elif sudo chmod -R 755 "$CACHE_DIR" 2>/dev/null; then
    echo "Permissions set successfully (with sudo)"
else
    echo "WARNING: Could not set permissions. Directory may still work if already writable."
fi

# Verify
echo ""
if [ -d "$CACHE_DIR" ] && [ -w "$CACHE_DIR" ]; then
    echo "SUCCESS: HTMLPurifier cache directory is writable"
    ls -ld "$CACHE_DIR" 2>/dev/null || echo "(Could not display permissions)"
else
    echo "WARNING: HTMLPurifier cache directory may not be writable"
    echo "If you encounter errors, manually run: sudo chown -R $WEB_USER:$WEB_GROUP $CACHE_DIR"
fi

echo ""
echo "HTMLPurifier cache directory setup complete."

# Always exit successfully so deployment continues
exit 0
