#!/bin/bash # Put this machine's tmuxrdr on the checkout's config and plugin. Dry run by # default; pass `yes` to apply -- the same interface as dot's link.sh. Only # tmuxrdr is touched: stock herdr keeps its own config and plugins in # ~/.config/herdr. # # ~/.local/share/tmuxrdr/plugin -> /tmuxrdr/plugin # the stable path config.toml's popups run from, whatever the checkout # is called; registered with `tmuxrdr plugin link` # ~/.config/tmuxrdr/config.toml -> /tmuxrdr/config/config.toml # herdr saves its settings with an in-place write (src/config/write.rs), # so the link survives and every change lands here, under git. A real # file already there is kept beside it as config.toml.pre-tmuxrdr. # # Run it again after moving the checkout. Needs the tmuxrdr binary # (scripts/build), or $HERDR pointing at another tmuxrdr build; it refuses a # binary that reads its config from anywhere else, such as stock herdr. set -euo pipefail repo="$(cd "$(dirname "$0")/../.." && pwd -P)" herdr="${HERDR:-tmuxrdr}" apply="${1:-}" plugin_id="tmuxrdr.tmux-mode" plugin_link="${XDG_DATA_HOME:-$HOME/.local/share}/tmuxrdr/plugin" config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/tmuxrdr" config_link="$config_dir/config.toml" run() { if [[ "$apply" == "yes" ]]; then "$@" else echo "would run: $*" fi } # link TARGET LINK: make LINK a symlink to TARGET, keeping a real file that is # in the way as LINK.pre-tmuxrdr. A real directory is left alone: too much to # guess about. link() { local target="$1" link="$2" if [[ -L "$link" && "$(readlink "$link")" == "$target" ]]; then echo "ok: $link" return fi if [[ -d "$link" && ! -L "$link" ]]; then >&2 echo "ERROR: $link is a directory; move it away and run again" exit 1 fi if [[ -f "$link" && ! -L "$link" ]]; then run mv "$link" "$link.pre-tmuxrdr" fi run mkdir -p "$(dirname "$link")" run ln -sfn "$target" "$link" [[ "$apply" == "yes" ]] && echo "linked: $link -> $target" return 0 } command -v "$herdr" >/dev/null || { >&2 echo "ERROR: no $herdr; build it: tmuxrdr/scripts/build"; exit 1; } reads="$("$herdr" --help 2>/dev/null | sed -n 's/^Config: *//p')" [[ "$reads" == "$config_link" ]] || { >&2 echo "ERROR: $herdr reads its config from ${reads:-?}, not $config_link: not a tmuxrdr build" exit 1 } command -v jq >/dev/null || { >&2 echo "ERROR: the plugin needs jq"; exit 1; } link "$repo/tmuxrdr/plugin" "$plugin_link" link "$repo/tmuxrdr/config/config.toml" "$config_link" # herdr records the link resolved, so compare against the checkout itself; # registered under another path -- an older checkout -- means link again. registered="$("$herdr" plugin list --json 2>/dev/null \ | jq -r --arg id "$plugin_id" '.. | objects | select(.plugin_id? == $id) | .plugin_root' \ | head -n 1)" if [[ -n "$registered" && "$registered" != "$repo/tmuxrdr/plugin" ]]; then run "$herdr" plugin unlink "$plugin_id" registered="" fi if [[ -z "$registered" ]]; then run "$herdr" plugin link "$plugin_link" >/dev/null [[ "$apply" == "yes" ]] && echo "registered: plugin $plugin_id" else echo "ok: plugin $plugin_id" fi if [[ "$apply" == "yes" ]]; then "$herdr" config check echo "A running tmuxrdr picks this up on prefix+r." else echo "Execute with: $0 yes" fi