#!/bin/bash
# Setup/refresh script for omarchy-nvim
#
# omarchy-nvim-setup: install missing config/data for the current user.
# omarchy-nvim-refresh: back up existing config, reset cached data, and copy
#                        the skel-seeded Omarchy Neovim defaults again.

set -euo pipefail

SKEL_CONFIG_DIR="/etc/skel/.config/nvim"
SKEL_DATA_DIR="/etc/skel/.local/share/nvim"
PACKAGE_DIR="/usr/share/omarchy-nvim"
CONFIG_SOURCE_DIR="$SKEL_CONFIG_DIR"
DATA_SOURCE_DIR="$SKEL_DATA_DIR"
CONFIG_DIR="${HOME}/.config/nvim"
DATA_DIR="${HOME}/.local/share/nvim"
STATE_DIR="${HOME}/.local/state/nvim"
CACHE_DIR="${HOME}/.cache/nvim"

command_name="$(basename "$0")"
refresh=false

if [[ $command_name == "omarchy-nvim-refresh" ]]; then
  refresh=true
fi

while (( $# > 0 )); do
  case "$1" in
    --force|--refresh)
      refresh=true
      ;;
    -h|--help)
      cat <<'EOF'
Usage: omarchy-nvim-setup [--force]
       omarchy-nvim-refresh

omarchy-nvim-setup installs Omarchy's skel-seeded Neovim config/data only when
missing. omarchy-nvim-refresh backs up an existing config and overwrites it with
the skel-seeded defaults.
EOF
      exit 0
      ;;
    *)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
  esac
  shift
done

# Prefer /etc/skel so legacy setup/refresh uses the exact seed copied into new
# users' homes. Only the config tree still has a package-dir copy to fall back
# on for older package layouts; the plugin cache ships once, in /etc/skel.
if [[ ! -d "$CONFIG_SOURCE_DIR" ]]; then
  CONFIG_SOURCE_DIR="$PACKAGE_DIR/config"
fi

if [[ ! -d "$CONFIG_SOURCE_DIR" || ! -d "$DATA_SOURCE_DIR" ]]; then
  echo "Error: omarchy-nvim defaults not found in $CONFIG_SOURCE_DIR / $DATA_SOURCE_DIR." >&2
  echo "The package owns both; restore them with: sudo pacman -S omarchy-nvim" >&2
  exit 1
fi

backup_path() {
  local path="$1"
  local backup="${path}.backup.$(date +%Y%m%d-%H%M%S)"

  if [[ -e $path || -L $path ]]; then
    echo "Backing up $path to $backup"
    mv "$path" "$backup"
  fi
}

copy_tree() {
  local src="$1"
  local dest="$2"

  mkdir -p "$(dirname "$dest")"
  cp -a "$src" "$dest"
}

install_config() {
  if [[ -d "$CONFIG_DIR" && $refresh != true ]]; then
    echo "Neovim config already exists at $CONFIG_DIR; leaving it in place."
    return
  fi

  backup_path "$CONFIG_DIR"
  copy_tree "$CONFIG_SOURCE_DIR" "$CONFIG_DIR"
  echo "Installed Neovim config to $CONFIG_DIR"
}

install_data() {
  if [[ -d "$DATA_DIR" && $refresh != true ]]; then
    echo "Neovim data already exists at $DATA_DIR; leaving it in place."
    return
  fi

  if [[ $refresh == true ]]; then
    rm -rf "$DATA_DIR" "$CACHE_DIR" "$STATE_DIR"
  fi

  copy_tree "$DATA_SOURCE_DIR" "$DATA_DIR"
  echo "Installed Neovim data to $DATA_DIR"
}

install_config
install_data

# Files copied into $HOME by the install user already have the right owner.
# Only adjust ownership when this script is explicitly run as root.
if (( EUID == 0 )); then
  target_user="${SUDO_USER:-${USER:-root}}"
  if [[ $target_user != "root" ]]; then
    chown -R "${target_user}:${target_user}" "$CONFIG_DIR" "$DATA_DIR"
  fi
fi

# Use a relative symlink so the same config works from /etc/skel and from any
# user's home directory. Omarchy 4 keeps the active theme under
# ~/.local/state/omarchy/current while Omarchy 3.x keeps it under
# ~/.config/omarchy/current. Fresh 3.x installs run this before the first
# theme is set, so when neither location exists yet, decide by the installed
# Omarchy version.
theme_dir=".local/state/omarchy/current/theme"
if [[ ! -d "$HOME/.local/state/omarchy/current" ]]; then
  omarchy_version=$(cat "${OMARCHY_PATH:-$HOME/.local/share/omarchy}/version" 2>/dev/null || true)
  if [[ -d "$HOME/.config/omarchy/current" || ${omarchy_version%%.*} == "3" ]]; then
    theme_dir=".config/omarchy/current/theme"
  fi
fi
mkdir -p "${CONFIG_DIR}/lua/plugins"
ln -snf "../../../../${theme_dir}/neovim.lua" "${CONFIG_DIR}/lua/plugins/theme.lua"

# Restore git working trees from the packaged lazy plugin cache.
for dir in "$DATA_DIR"/lazy/*/; do
  if [[ -d "$dir/.git" ]]; then
    git --git-dir="$dir/.git" --work-tree="$dir" restore .
  fi
done

# Set nvim.desktop as default so it takes priority in Nautilus.
xdg-mime default nvim.desktop text/plain
xdg-mime default nvim.desktop text/english
xdg-mime default nvim.desktop text/x-makefile
xdg-mime default nvim.desktop text/x-c++hdr
xdg-mime default nvim.desktop text/x-c++src
xdg-mime default nvim.desktop text/x-chdr
xdg-mime default nvim.desktop text/x-csrc
xdg-mime default nvim.desktop text/x-java
xdg-mime default nvim.desktop text/x-moc
xdg-mime default nvim.desktop text/x-pascal
xdg-mime default nvim.desktop text/x-tcl
xdg-mime default nvim.desktop text/x-tex
xdg-mime default nvim.desktop application/x-shellscript
xdg-mime default nvim.desktop text/x-c
xdg-mime default nvim.desktop text/x-c++
xdg-mime default nvim.desktop application/xml
xdg-mime default nvim.desktop text/xml

if [[ $refresh == true ]]; then
  echo "Neovim refresh complete!"
else
  echo "Neovim setup complete!"
fi
