#!/usr/bin/env bash
# XAMPP/LAMPP: Apache rulează ca user "daemon" — storage trebuie grup daemon + scriere
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT}"

WEB_GROUP="${WEB_GROUP:-daemon}"

echo "Fixing storage/bootstrap/cache for web group: ${WEB_GROUP}"

if command -v sudo >/dev/null 2>&1; then
  sudo chgrp -R "${WEB_GROUP}" storage bootstrap/cache 2>/dev/null || chgrp -R "${WEB_GROUP}" storage bootstrap/cache
  sudo chmod -R ug+rwX storage bootstrap/cache 2>/dev/null || chmod -R ug+rwX storage bootstrap/cache
  sudo find storage bootstrap/cache -type d -exec chmod g+s {} \; 2>/dev/null || true
else
  chgrp -R "${WEB_GROUP}" storage bootstrap/cache
  chmod -R ug+rwX storage bootstrap/cache
  find storage bootstrap/cache -type d -exec chmod g+s {} \;
fi

php artisan view:clear 2>/dev/null || true
echo "Done. Reîncarcă site-ul în browser."
