#!/usr/bin/env bash
set -euo pipefail

WEB_ROOT="${WEB_ROOT:-/var/www/html}"
WEB_USER="${WEB_USER:-www-data}"
WEB_GROUP="${WEB_GROUP:-www-data}"

printf '[Rakku Rights] Verwende WEB_ROOT: %s\n' "$WEB_ROOT"

if [ ! -d "$WEB_ROOT" ]; then
  echo "[Rakku Rights] Ordner nicht gefunden: $WEB_ROOT"
  exit 1
fi

chown -R "$WEB_USER:$WEB_GROUP" "$WEB_ROOT"
find "$WEB_ROOT" -type d -exec chmod 755 {} \;
find "$WEB_ROOT" -type f -exec chmod 644 {} \;
find "$WEB_ROOT" -type f -name "*.sh" -exec chmod 755 {} \;

if [ -d "$WEB_ROOT/public/uploads" ]; then
  chmod -R 775 "$WEB_ROOT/public/uploads"
fi

if [ -f "$WEB_ROOT/.env" ]; then
  chmod 600 "$WEB_ROOT/.env"
fi

echo "[Rakku Rights] Fertig."
