#!/usr/bin/bash

# Become root so we can restart desktop managers
if [[ $EUID != 0 ]]; then
  exec pkexec "$(realpath "$0")" "$@"
fi

# Check if the username argument is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <username>"
  exit 1
fi

# Get the username from the first argument
USERNAME=$1

# Find the home directory of the specified user
USER_HOME=$(getent passwd "$USERNAME" | cut -d: -f6)

# Check if the user exists
if [ -z "$USER_HOME" ]; then
  echo "User '$USERNAME' does not exist."
  exit 1
fi

# Define the target directories
HOMEBREW_DIR="$USER_HOME/homebrew/"
SERVICE_DIR="$USER_HOME/homebrew/services/"
PLUGINS_DIR="$USER_HOME/homebrew/plugins/"
MODIFIED="no"

# Ensure homebrew directory exists
if [ ! -d "$HOMEBREW_DIR" ]; then
  mkdir -p "$HOMEBREW_DIR" || { echo "Failed to create directory $HOMEBREW_DIR"; exit 1; }
  MODIFIED="yes"
fi

# Ensure service directory exists and copy PluginLoader
if [ ! -d "$SERVICE_DIR" ]; then
  mkdir -p "$SERVICE_DIR" || { echo "Failed to create directory $SERVICE_DIR"; exit 1; }
  cp /usr/share/deckyloader/PluginLoader "$SERVICE_DIR" || { echo "Failed to copy PluginLoader"; exit 1; }
  MODIFIED="yes"
fi

# Ensure plugins directory exists
if [ ! -d "$PLUGINS_DIR" ]; then
  mkdir -p "$PLUGINS_DIR" || { echo "Failed to create directory $PLUGINS_DIR"; exit 1; }
  MODIFIED="yes"
fi

# Copy plugins if they do not exist
for plugin in /usr/share/deckyloader/plugins/*; do
  plugin_name=$(basename "$plugin")
  if [ ! -e "$PLUGINS_DIR/$plugin_name" ]; then
    cp -R "$plugin" "$PLUGINS_DIR" || { echo "Failed to copy $plugin_name"; exit 1; }
    MODIFIED="yes"
  fi
done

# Decky Loader won't show in steam if these don't exist.
sudo -u $USERNAME  touch "$USER_HOME/.local/share/Steam/.cef-enable-remote-debugging"
# if installed as flatpak, put .cef-enable-remote-debugging there
[ -d "$USER_HOME/.var/app/com.valvesoftware.Steam/data/Steam/" ] && sudo -u $USERNAME touch "$USER_HOME/.var/app/com.valvesoftware.Steam/data/Steam/.cef-enable-remote-debugging"


# Set ownership and permissions if modifications were made
if [ "$MODIFIED" == "yes" ]; then
  chown -R $USERNAME:$USERNAME "$HOMEBREW_DIR"
  chmod -R 775 "$HOMEBREW_DIR"
fi

# Define the file path
FILE_PATH="/etc/systemd/system/plugin_loader.service"

# Check if the file exists and create or update it
if [ ! -f "$FILE_PATH" ] || ! grep -q "$USERNAME" "$FILE_PATH"; then
  cat <<EOF > "$FILE_PATH"
[Unit]
Description=SteamDeck Plugin Loader
After=network.target

[Service]
Type=simple
User=root
Restart=always
KillMode=process
TimeoutStopSec=15
ExecStart=$USER_HOME/homebrew/services/PluginLoader
WorkingDirectory=$USER_HOME/homebrew/services
Environment=UNPRIVILEGED_PATH=$USER_HOME/homebrew
Environment=PRIVILEGED_PATH=$USER_HOME/homebrew
Environment=LOG_LEVEL=DEBUG

[Install]
WantedBy=multi-user.target
EOF
  systemctl daemon-reload
fi

# Start the service
systemctl restart plugin_loader.service

# Wait for the service to be fully running
while ! systemctl is-active --quiet plugin_loader.service; do
  sleep 1
done

echo "Service is fully running."
