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

# Usage: scripts/fix-perms.sh [user] [group]
# Defaults: user=$(id -un); group=www-data
USER_TO_SET=${1:-$(id -un)}
GROUP_TO_SET=${2:-www-data}

echo "Setting ownership to: ${USER_TO_SET}:${GROUP_TO_SET} for storage and bootstrap/cache"

sudo chown -R "${USER_TO_SET}:${GROUP_TO_SET}" storage bootstrap/cache || true
sudo find storage bootstrap/cache -type d -exec chmod 775 {} + || true
sudo find storage bootstrap/cache -type f -exec chmod 664 {} + || true

cat <<EOF
Permissions set. If you used a different webserver user (eg. nginx), call the script like:

  ./scripts/fix-perms.sh $USER_TO_SET nginx

You may also want to add your user to the webserver group:

  sudo usermod -aG ${GROUP_TO_SET} ${USER_TO_SET}
  # Then log out/in to apply new group membership
EOF
