#!/bin/bash

# Set up Omarchy Emacs configuration for the current user

SHARE_DIR="/usr/share/omarchy-emacs"
CONFIG_DIR="$HOME/.config/emacs"
THEMED_DIR="$HOME/.config/omarchy/themed"

mkdir -p "$CONFIG_DIR/themes"
mkdir -p "$THEMED_DIR"

# Detect legacy ~/.emacs which takes precedence over ~/.config/emacs/init.el
if [[ -f "$HOME/.emacs" ]]; then
  echo "WARNING: ~/.emacs exists and will override ~/.config/emacs/init.el, preventing Omarchy integration."
  echo "If you move it, add any customizations you want to keep to ~/.config/emacs/init.el instead."
  read -r -p "Move ~/.emacs to ~/.emacs.bak? [y/N] " response
  if [[ "${response,,}" == "y" ]]; then
    mv "$HOME/.emacs" "$HOME/.emacs.bak"
    echo "Moved ~/.emacs to ~/.emacs.bak — review it and migrate any customizations to ~/.config/emacs/init.el"
  else
    echo "Skipping. Note: Omarchy theme/font integration will not work until ~/.emacs is removed or renamed."
  fi
fi

# Detect legacy ~/.emacs.d which takes precedence over ~/.config/emacs/
if [[ -d "$HOME/.emacs.d" ]]; then
  echo "WARNING: ~/.emacs.d exists and will override ~/.config/emacs/, preventing Omarchy integration."
  echo "If you move it, add any customizations you want to keep to ~/.config/emacs/init.el instead."
  read -r -p "Move ~/.emacs.d to ~/.emacs.d.bak? [y/N] " response
  if [[ "${response,,}" == "y" ]]; then
    mv "$HOME/.emacs.d" "$HOME/.emacs.d.bak"
    echo "Moved ~/.emacs.d to ~/.emacs.d.bak — review it and migrate any customizations to ~/.config/emacs/init.el"
  else
    echo "Skipping. Note: Omarchy theme/font integration will not work until ~/.emacs.d is removed or renamed."
  fi
fi

# Write omarchy.el as a thin shim that loads the package-installed
# implementation from $SHARE_DIR. This way pacman/AUR upgrades update
# the integration code in place — re-running setup isn't required to
# pick up new code, just an Emacs reload (omarchy-restart-emacs).
cat > "$CONFIG_DIR/omarchy.el" <<EOF
;;; omarchy.el --- Omarchy Emacs integration shim -*- lexical-binding: t -*-
;;; Managed by omarchy-emacs. Loads the package-installed implementation so
;;; that AUR upgrades propagate without re-running omarchy-emacs-setup.
;;; Put your personal customizations in init.el instead.

(let ((omarchy--system-file "$SHARE_DIR/config/omarchy.el"))
  (when (file-exists-p omarchy--system-file)
    (load omarchy--system-file)))

;;; omarchy.el ends here
EOF

# Copy user-owned files only if they don't exist yet
for file in init.el shell-bashrc; do
  if [[ ! -f "$CONFIG_DIR/$file" ]]; then
    cp "$SHARE_DIR/config/$file" "$CONFIG_DIR/$file"
  fi
done

# Theme is managed by omarchy-emacs (always overwrite).
cp "$SHARE_DIR/config/themes/omarchy-theme.el" "$CONFIG_DIR/themes/omarchy-theme.el"

# Remove legacy split themes from upgrades (<1.9). Safe — they're managed files.
rm -f "$CONFIG_DIR/themes/omarchy-dark-theme.el" "$CONFIG_DIR/themes/omarchy-light-theme.el"

# Install font-set and theme-set hooks to sync Emacs on font/theme changes.
# The logic lives in omarchy-emacs-sync-hooks so the shim (config/omarchy.el)
# can reuse it to self-heal existing installs on Emacs startup.
omarchy-emacs-sync-hooks

# Color template is managed by omarchy-emacs (always overwrite). It gained the
# semantic colors (muted, dark_foreground, lighter_background, selection) in
# 1.10; installing it only when absent would leave every existing user on a
# template that renders them empty, and the theme silently falling back to
# blended shades forever.
cp "$SHARE_DIR/omarchy-colors.el.tpl" "$THEMED_DIR/"

# Generate colors for the current theme
if command -v omarchy-theme-set &>/dev/null; then
  theme=$(cat "$HOME/.local/state/omarchy/current/theme.name" 2>/dev/null)
  if [[ -n $theme ]]; then
    omarchy-theme-set "$theme"
  fi
fi

echo "Omarchy Emacs setup complete!"
