#!/bin/sh
# This program is licensed under GPLv3
# https://www.gnu.org/licenses/gpl-3.0.html

script_dir=$(dirname -- "$(realpath -- "$0")")
gtk3_file="$HOME/.config/gtk-3.0/gtk.css"
gtk4_file="$HOME/.config/gtk-4.0/gtk.css"
backup_number=$(date +%s)

WARNING_TEXT="This program will overwrite and create backups of the configuration files ~/.config/gtk-3.0/gtk.css and ~/.config/gtk-4.0/gtk.css if they exist. Are you sure you want to proceed?"

echo "$WARNING_TEXT"
read -p "Proceed? (y/n) " -n 1 -r

case "$REPLY" in
  [Yy]* ) echo "Continuing...";;
  [Nn]* ) echo "Aborting."; exit 1;;
  * ) echo "Invalid input. Please enter y or n.";;
esac

# Define color codes
BLUE="\e[34m"
TEAL="\e[36m"
GREEN="\e[32m"
YELLOW="\e[33m"
ORANGE="\e[38;5;208m"  # Using 256-color mode for orange
RED="\e[31m"
PINK="\e[38;5;213m"    # Using 256-color mode for pink
PURPLE="\e[35m"
SLATE="\e[38;5;245m"  # Using 256-color mode for slate
MAIA="\e[32;1;24;144;200m"
RESET="\e[0m"

# Check GNOME Shell version
gnome_shell_output=$(gnome-shell --version)
gnome_shell_version=$(echo "$gnome_shell_output" | sed -n 's/.*\(GNOME Shell \)\([0-9.]*\).*/\2/p')

# Set required minimum version
required_version="47.0"

# Compare versions
if [ "$(printf '%s\n' "$required_version" "$gnome_shell_version" | sort -V | head -n1)" = "$required_version" ]; then
  echo "Your GNOME Shell version is $gnome_shell_version. Proceeding..."
else
  echo "Error: Your GNOME Shell version ($gnome_shell_version) needs to be 47.0 or newer."
  exit 1
fi

# Check if gsettings command exists
if ! command -v gsettings >/dev/null; then
  echo "Error: gsettings command not found. Install dconf or dconf-editor/dconf-tools."
  exit 1
fi

# Define colors and their corresponding codes
colors=("blue" "teal" "green" "yellow" "orange" "red" "pink" "purple" "slate" "maia")
color_codes=("$BLUE" "$TEAL" "$GREEN" "$YELLOW" "$ORANGE" "$RED" "$PINK" "$PURPLE" "$SLATE" "$MAIA")

# Display the menu
echo "Available colors:"
i=1
for color in "${colors[@]}"; do
  echo -e "${color_codes[i-1]}$i. $color${RESET}"
  i=$((i + 1))
done

read -p "Choose your accent color by typing a number between 1-9 and press enter (choose nothing to cancel): " choice

if [ "$choice" -ge 1 ] && [ "$choice" -le "${#colors[@]}" ]; then
  selected_color="${colors[$((choice - 1))]}"
  echo -e "You selected: ${color_codes[$((choice - 1))]}$selected_color${RESET}"

  # Set the accent color with gsettings for libadwaita and gnome-shell
  gsettings set org.gnome.desktop.interface accent-color "$selected_color"

  # Set the accent color with gsettings for gdm
  machinectl shell gdm@ /usr/bin/gsettings set org.gnome.desktop.interface accent-color "$selected_color"

  # Procced to add the gtk.css files to set global accent color for gtk3 and gtk4
  # Note: only gtk3 themes that use gtk named colors support accent colors
  # This script will create backups if gtk.css files exist and create them if they do not
  if [ -f "$gtk4_file" ]; then
    cp "$gtk4_file" "${gtk4_file}.${backup_number}.bak"
    echo "Backup created at: ${gtk4_file}.${backup_number}.bak"
  else
    mkdir -p ~/.config/gtk-4.0/ && touch ~/.config/gtk-4.0/gtk.css
  fi

  if [ -f "$gtk3_file" ]; then
    cp "$gtk3_file" "${gtk3_file}.${backup_number}.bak"
    echo "Backup created at: ${gtk3_file}.${backup_number}.bak"
  else
    mkdir -p ~/.config/gtk-3.0/ && touch ~/.config/gtk-3.0/gtk.css
  fi

  # Add the accent color
  # Write the CSS rules to the gtk.css files
  {
    echo ":root {"
    echo "  --accent-bg-color: var(--accent-$selected_color);"
    echo "}"
  } > "$gtk4_file"

  echo "Updated $gtk4_file with new accent color settings."

  {
    echo "@define-color accent_blue #3584e4;"
    echo "@define-color accent_teal #2190a4;"
    echo "@define-color accent_green #3a944a;"
    echo "@define-color accent_yellow #c88800;"
    echo "@define-color accent_orange #ed5b00;"
    echo "@define-color accent_red #e62d42;"
    echo "@define-color accent_pink #d56199;"
    echo "@define-color accent_purple #9141ac;"
    echo "@define-color accent_slate #6f8396;"
    echo "@define-color accent_maia #16a085;"
    echo "@define-color accent_bg_color @accent_$selected_color;"
  } > "$gtk3_file"

  echo "Updated $gtk3_file with new accent color settings."
  echo "Accent color updated. Some applications may require a restart for changes to take effect."

else
  echo "Invalid choice. Please run the script again."
  exit 1
fi
