dotfiles/.local/bin/dotfiles-install
Alexander Hess a540f9203c
Add TODO branch ...
... for temporary and not publicly disclosed work
2026-10-20 16:45:46 +02:00

211 lines
5.4 KiB
Shell
Executable file

#!/bin/sh
# Installation script to make the dotfiles available in ~/
#
# `git` is the only dependency for this script to run (See: https://git-scm.com)
#
# See: https://code.webartifex.de/alexander/dotfiles#installation
set -eu
XDG_BIN_HOME="${XDG_BIN_HOME:-$HOME/.local/bin}"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
# Each branch's first commit sets the default to its own name
# => Overridable via the environment (e.g., done by `dotfiles-update`)
DOTFILES_BRANCH="${DOTFILES_BRANCH:-TODO}"
DOTFILES_DIR="${DOTFILES_DIR:-$XDG_DATA_HOME/dotfiles}"
DOTFILES_HTTPS="${DOTFILES_HTTPS:-https://code.webartifex.de/alexander/dotfiles}"
DOTFILES_SSH="${DOTFILES_SSH:-git@git.webartifex.de:alexander/dotfiles.git}"
BACKUPS_DIR="$XDG_STATE_HOME/dotfiles-backups"
BACKUP_DIR="$BACKUPS_DIR/$(date +%Y%m%d-%H%M%S)"
_force=0
if [ "$#" -gt 1 ]; then
echo "Usage: $(basename "$0") [--force]" >&2
exit 1
fi
case "${1:-}" in
"") ;;
--force) _force=1 ;;
*)
echo "Usage: $(basename "$0") [--force]" >&2
exit 1
;;
esac
if [ -d "$DOTFILES_DIR" ]; then
if [ "$_force" -ne 1 ]; then
echo ""
echo "The dotfiles are already installed at: $DOTFILES_DIR"
echo ""
echo "Options:"
echo " - Run the installation again with '--force'"
echo " - Update with 'dotfiles pull' and continue using them"
echo ""
exit 0
fi
echo "Backing up the existing repository to: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
mv "$DOTFILES_DIR" "$BACKUP_DIR/dotfiles"
fi
git clone --bare "$DOTFILES_HTTPS" "$DOTFILES_DIR"
_git() {
git --git-dir="$DOTFILES_DIR" --work-tree="$HOME" "$@"
}
if ! _git show-ref --verify --quiet "refs/heads/$DOTFILES_BRANCH"; then
echo "" >&2
echo "Internal error: branch '$DOTFILES_BRANCH' missing in the repository" >&2
echo "" >&2
echo "Available branches:" >&2
_git branch --format=' %(refname:short)' >&2
echo "" >&2
exit 1
fi
_git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
_git fetch origin
# Do not checkout project documentation intended for web GUIs
#
# Note: The patterns match at every depth, like in a .gitignore,
# which excludes the per-folder README.md files as well
_git config core.sparseCheckout true
{
echo "/*"
echo "!LICENSE.txt"
echo "!README.md"
echo "!.ssh"
} > "$DOTFILES_DIR/info/sparse-checkout"
# Back up files in the user's $HOME folder if a checkout is about to overwrite them
#
# Note: This may also back up local-only files marked sparse in the repo,
# which is not considered a big problem because it's very unlikely
_conflicts=$(_git ls-tree -r --name-only "$DOTFILES_BRANCH" | while IFS= read -r f; do
if [ -e "$HOME/$f" ] || [ -L "$HOME/$f" ]; then echo "$f"; fi
done)
if [ -n "$_conflicts" ]; then
echo "Backing up pre-existing files to: $BACKUP_DIR"
echo "$_conflicts" | while IFS= read -r f; do
mkdir -p "$BACKUP_DIR/$(dirname "$f")"
cp -a "$HOME/$f" "$BACKUP_DIR/$f"
done
fi
# Put the dotfiles in the user's $HOME folder
_git checkout --force "$DOTFILES_BRANCH"
# Do not show files not tracked in the dotfiles repository because there are simply too many
_git config --local status.showUntrackedFiles no
# Prefer `ssh` for syncing, but only if this machine may have access,
# which we proxy with the presence of a default private key file
for _key in id_ed25519 id_ecdsa id_rsa; do
if [ -f "$HOME/.ssh/$_key" ]; then
_git remote set-url origin "$DOTFILES_SSH"
echo "The dotfiles now sync via: $DOTFILES_SSH"
break
fi
done
unset _key
locales_ok=1
sh "$XDG_BIN_HOME/ensure-locales" >/dev/null 2>&1 || locales_ok=0
if command -v batcat >/dev/null 2>&1 && ! command -v bat >/dev/null 2>&1; then
ln -sf "$(command -v batcat)" "$XDG_BIN_HOME/bat"
fi
sh "$XDG_BIN_HOME/generate-shell-completions" --quieter \
|| echo "WARN: generating shell completions failed" >&2
if command -v python3 >/dev/null 2>&1; then
python3 -m \
pip install \
--user \
--upgrade \
--break-system-packages \
coloraide \
rofimoji \
|| echo "WARN: pip install failed" >&2
python3 "$XDG_BIN_HOME/color-palette" --write \
|| echo "WARN: color-palette failed" >&2
fi
if systemctl --user daemon-reload 2>/dev/null; then
systemctl --user enable gnome-restore.service
systemctl --user enable night-light-reset.timer
systemctl --user enable set-brightness-default.service
systemctl --user enable set-keyboard-brightness-default.service
systemctl --user enable set-volume-default.service
systemctl --user enable xcape.service
else
echo "WARN: no systemd user session, units not enabled" >&2
fi
# Keep only the most recent backups
_keep=10
if [ -d "$BACKUPS_DIR" ]; then
# `head -n -N` is a GNU extension; fine on Debian, fails on, e.g., BSD/BusyBox
find "$BACKUPS_DIR" -mindepth 1 -maxdepth 1 -type d | sort | head -n -"$_keep" \
| while IFS= read -r _old; do
rm -f -r "$_old"
done
fi
echo ""
echo "The dotfiles were installed successfully (branch: $DOTFILES_BRANCH)"
echo ""
if [ "$locales_ok" -ne 1 ]; then
echo "WARNING: Some locales are missing, fix:"
echo " sudo $XDG_BIN_HOME/ensure-locales"
echo ""
fi
echo "Reload your shell to use them:"
echo " exec \$SHELL -l"
echo ""