Files
omarchy-pkgs/pkgbuilds/omarchy-nvim/omarchy-nvim-setup
T
2026-06-12 00:44:30 -04:00

154 lines
4.3 KiB
Bash
Executable File

#!/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. Fall back to the package share dir for older package layouts.
if [[ ! -d "$CONFIG_SOURCE_DIR" || ! -d "$DATA_SOURCE_DIR" ]]; then
CONFIG_SOURCE_DIR="$PACKAGE_DIR/config"
DATA_SOURCE_DIR="$PACKAGE_DIR/data"
fi
if [[ ! -d "$CONFIG_SOURCE_DIR" || ! -d "$DATA_SOURCE_DIR" ]]; then
echo "Error: omarchy-nvim defaults not found; is omarchy-nvim installed?" >&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.
mkdir -p "${CONFIG_DIR}/lua/plugins"
ln -snf "../../../../.local/state/omarchy/current/theme/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