#!/bin/bash
# System Permission & Ownership Repair Script (fix.sh)
# Place in: /var/www/number1podcast/fix.sh

# 1. Align recursive group ownership to www-data
sudo chown -R $USER:www-data /var/www/

# 2. Set SetGID (2775) on all directories so new files inherit www-data group
sudo find /var/www/ -type d -exec chmod 2775 {} +

# 3. Set 0664 (rw-rw-r--) on all data files (json, log, txt, pw, xml)
sudo find /var/www/ -type f ! -name "*.php" ! -name "*.sh" -exec chmod 0664 {} +

# 4. Set 0775 (rwxrwxr-x) on all executable scripts (php, sh)
sudo find /var/www/ -type f \( -name "*.php" -o -name "*.sh" \) -exec chmod 0775 {} +

echo "[SUCCESS] Ownership set to $USER:www-data with SetGID 2775 dirs, 0775 scripts, and 0664 data files."
