#!/bin/bash

# Default WordPress htaccess rules
HTACCESS_CONTENT='# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress'

# Find all WordPress installations and update .htaccess
for wp in $(find /home -type f -name "wp-config.php"); do
    sitepath=$(dirname "$wp")
    echo "Updating .htaccess in $sitepath"

    echo "$HTACCESS_CONTENT" > "$sitepath/.htaccess"
    chown $(stat -c '%U:%G' "$wp") "$sitepath/.htaccess"
    chmod 644 "$sitepath/.htaccess"
done

echo "✅ All WordPress .htaccess files updated!"
