#!/bin/bash

# Ensure we are running as root
if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root or with sudo."
  exit 1
fi

# Determine the OS and set the default user
if grep -qi 'ubuntu' /etc/os-release; then
  default_user="www-data"
elif grep -qi 'almalinux' /etc/os-release || grep -qi 'rocky' /etc/os-release; then
  default_user="caddy"
elif grep -qi 'debian' /etc/os-release; then
  default_user="www-data"
else
  echo "Unsupported OS. Please specify the user manually."
  exit 1
fi

# Get the current directory
current_dir=$(pwd)
# Adjust if executing from the tools directory
if [ "$(basename "$current_dir")" == "tools" ]; then
  current_dir=$(dirname "$current_dir")
fi

# Check if the user is provided as an argument
current_user=$1

# Prompt for input if no user is provided
if [ -z "$current_user" ]; then
  read -p "Please enter the username of the current user (default: $default_user): " current_user
  if [ -z "$current_user" ]; then
    current_user="$default_user"
  fi
fi

# Check if the current user is in the default group if required
if [ "$current_user" != "$default_user" ] && ! groups "$current_user" | grep -qw "$default_user"; then
  echo "The current user is not in the $default_user group."
  echo "Please add the current user ($current_user) to the $default_user group and try again."
  exit 1
fi

# Set ownership recursively
echo "Setting ownership to $current_user:$default_user for all files and folders in $current_dir"
chown -R "$current_user:$default_user" "$current_dir"

# Set permissions to 775 for directories and 664 for files
echo "Setting permissions to 775 for all directories"
find "$current_dir" -type d -exec chmod 775 {} \;
echo "Setting permissions to 664 for all files"
find "$current_dir" -type f -exec chmod 664 {} \;

# Set permissions to 775 for specific executable files
echo "Setting permissions to 775 for executable files"
chmod +x "$current_dir/tools/permissionFix.sh"
chmod +x "$current_dir/yii"
chmod +x "$current_dir/init"

# Check if 'iso' directory exists, if not, create it
iso_dir="$current_dir/backend/web/iso"
if [ ! -d "$iso_dir" ]; then
  echo "Directory 'iso' does not exist. Creating it."
  mkdir -p "$iso_dir"
fi

authclient_dir="$current_dir/vendor/yiisoft/yii2/authclient/assets"
if [ ! -d "$authclient_dir" ]; then
  echo "Directory 'assets' does not exist. Creating it."
  mkdir -p "$authclient_dir"
fi

# Set ownership and permissions for specific backend directories
echo "Setting ownership to $current_user:$default_user for backend directories"
chown -R "$current_user:$default_user" "$current_dir/backend/runtime/" "$current_dir/backend/web/assets/" "$iso_dir" "$current_dir/vendor"
echo "Ensuring backend directories are writable"
chmod -R 775 "$current_dir/backend/runtime/" "$current_dir/backend/web/assets/" "$iso_dir" "$authclient_dir" 
chmod -R 755 "$current_dir/vendor"

echo "Ownership and permissions updated successfully in $current_dir"
echo "If requested to do this before an update, you can now proceed with updating your MultiPortal Instance."
