aboutsummaryrefslogtreecommitdiff
path: root/tmuxrdr/scripts/install
blob: f4c0f1eaf615a2a50f8438f0e72abed1ea44636a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash

# Put this machine on tmuxrdr's config and plugin. Dry run by default; pass
# `yes` to apply -- the same interface as dot's link.sh.
#
#   ~/.local/share/tmuxrdr/plugin -> <repo>/tmuxrdr/plugin
#       the stable path config.toml's popups run from, whatever the checkout
#       is called; registered with `herdr plugin link`
#   ~/.config/herdr/config.toml   -> <repo>/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. Uses whichever herdr is first on
# PATH, or $HERDR.

set -euo pipefail

repo="$(cd "$(dirname "$0")/../.." && pwd -P)"
herdr="${HERDR:-herdr}"
apply="${1:-}"
plugin_id="tmuxrdr.tmux-mode"
plugin_link="${XDG_DATA_HOME:-$HOME/.local/share}/tmuxrdr/plugin"
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/herdr"
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 on PATH"; 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 "Running herdr picks this up on prefix+r."
else
  echo "Execute with: $0 yes"
fi