#!/bin/bash

set -e          # Exit immediately if a command fails
set -u          # Treat unset variables as errors
set -o pipefail # Prevent errors in a pipeline from being masked

REPO_URL="https://github.com/Axenide/Ax-Shell.git"
INSTALL_DIR="$HOME/.config/Ax-Shell"
PACKAGES=(
  awww-git
  brightnessctl
  cava
  cliphist
  ddcutil
  fabric-cli-git
  gnome-bluetooth-3.0
  gobject-introspection
  gpu-screen-recorder
  hypridle
  hyprlock
  hyprpicker
  hyprshot
  hyprsunset
  imagemagick
  libnotify
  matugen-bin
  network-manager-applet
  networkmanager
  nm-connection-editor
  noto-fonts-emoji
  nvtop
  playerctl
  power-profiles-daemon
  python-fabric-git
  python-gobject
  python-ijson
  python-numpy
  python-pillow
  python-psutil
  python-pywayland
  python-requests
  python-setproctitle
  python-toml
  python-watchdog
  swappy
  tesseract
  tesseract-data-eng
  tesseract-data-spa
  tmux
  ttf-nerd-fonts-symbols-mono
  unzip
  upower
  uwsm
  vte3
  webp-pixbuf-loader
  wl-clipboard
)

# Prevent running as root
if [ "$(id -u)" -eq 0 ]; then
  echo "Please do not run this script as root."
  exit 1
fi

aur_helper="yay"

# Check if paru exists, otherwise use yay
if command -v paru &>/dev/null; then
  aur_helper="paru"
elif ! command -v yay &>/dev/null; then
  echo "Installing yay-bin..."
  tmpdir=$(mktemp -d)
  git clone --depth=1 https://aur.archlinux.org/yay-bin.git "$tmpdir/yay-bin"
  (cd "$tmpdir/yay-bin" && makepkg -si --noconfirm)
  rm -rf "$tmpdir"
fi

# Clone or update the repository
if [ -d "$INSTALL_DIR" ]; then
  echo "Updating Ax-Shell..."
  git -C "$INSTALL_DIR" pull
else
  echo "Cloning Ax-Shell..."
  git clone --depth=1 "$REPO_URL" "$INSTALL_DIR"
fi

# Install required packages using the detected AUR helper (only if missing)
echo "Installing required packages..."
$aur_helper -Syy --needed --devel --noconfirm "${PACKAGES[@]}" || true

echo "Installing gray-git..."
yes | $aur_helper -Syy --needed --devel --noconfirm gray-git || true

echo "Installing required fonts..."

FONT_URL="https://github.com/zed-industries/zed-fonts/releases/download/1.2.0/zed-sans-1.2.0.zip"
FONT_DIR="$HOME/.fonts/zed-sans"
TEMP_ZIP="/tmp/zed-sans-1.2.0.zip"

# Check if fonts are already installed
if [ ! -d "$FONT_DIR" ]; then
  echo "Downloading fonts from $FONT_URL..."
  curl -L -o "$TEMP_ZIP" "$FONT_URL"

  echo "Extracting fonts to $FONT_DIR..."
  mkdir -p "$FONT_DIR"
  unzip -o "$TEMP_ZIP" -d "$FONT_DIR"

  echo "Cleaning up..."
  rm "$TEMP_ZIP"
else
  echo "Fonts are already installed. Skipping download and extraction."
fi

# Network services handling
echo "Configuring network services..."

# Disable iwd if enabled/active
if systemctl is-enabled --quiet iwd 2>/dev/null || systemctl is-active --quiet iwd 2>/dev/null; then
  echo "Disabling iwd..."
  sudo systemctl disable --now iwd
else
  echo "iwd is already disabled."
fi

# Enable NetworkManager if not enabled
if ! systemctl is-enabled --quiet NetworkManager 2>/dev/null; then
  echo "Enabling NetworkManager..."
  sudo systemctl enable NetworkManager
else
  echo "NetworkManager is already enabled."
fi

# Start NetworkManager if not running
if ! systemctl is-active --quiet NetworkManager 2>/dev/null; then
  echo "Starting NetworkManager..."
  sudo systemctl start NetworkManager
else
  echo "NetworkManager is already running."
fi

# Copy local fonts if not already present
if [ ! -d "$HOME/.fonts/tabler-icons" ]; then
  echo "Copying local fonts to $HOME/.fonts/tabler-icons..."
  mkdir -p "$HOME/.fonts/tabler-icons"
  cp -r "$INSTALL_DIR/assets/fonts/"* "$HOME/.fonts"
else
  echo "Local fonts are already installed. Skipping copy."
fi

python "$INSTALL_DIR/config/config.py"
echo "Starting Ax-Shell..."
killall ax-shell 2>/dev/null || true
uwsm app -- python "$INSTALL_DIR/main.py" >/dev/null 2>&1 &
disown

echo "Installation complete."
