about summary refs log tree commit diff
path: root/scripts/install.sh
blob: 595fd8c98064d3355f41a1bdc2f4692615a2388c (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
#!/bin/sh
# Build ai-memory from this checkout and put it on PATH, pinned.
#
# The reason this exists: `guix build -f guix.scm' prints a store path that
# changes with every rebuild, so scripts end up hard-coding one and keep running
# a binary that predates the source -- which is how a store was created with a
# schema too old for the code that later wrote to it.
#
#   scripts/install.sh                 # build, GC-root it, link ~/.local/bin
#   AI_MEMORY_ROOT=/opt/am scripts/install.sh
#   scripts/install.sh --no-link       # do not touch ~/.local/bin
#
# The GC root is what keeps a `guix gc' from taking the binary away; the link is
# what keeps people from having to remember a store hash.
set -eu

ROOT=${AI_MEMORY_ROOT:-${XDG_STATE_HOME:-$HOME/.local/state}/ai-memory/root}
LINK=${AI_MEMORY_LINK:-${XDG_BIN_HOME:-$HOME/.local/bin}/ai-memory}
here=$(cd "$(dirname "$0")/.." && pwd)

link=yes
for arg in "$@"; do
	case $arg in
	--no-link) link=no ;;
	-h|--help) sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
	*) echo "install.sh: unknown argument $arg" >&2; exit 2 ;;
	esac
done

command -v guix >/dev/null 2>&1 || {
	echo "install.sh: guix is not on PATH" >&2
	exit 1
}

mkdir -p "$(dirname "$ROOT")"
# guix build --root cannot replace an existing root: it answers "failed to create
# GC root ...: File exists".  Drop the old symlink first, and refuse to touch
# anything that is not one -- a directory here is somebody's mistake, not ours.
if [ -L "$ROOT" ]; then
	rm -f "$ROOT"
elif [ -e "$ROOT" ]; then
	echo "install.sh: $ROOT exists and is not a symlink; remove it or set AI_MEMORY_ROOT" >&2
	exit 1
fi
# Build from this checkout (guix.scm's source is a local-file, so the store path
# is a function of the tree) and root it in one step.
store=$(cd "$here" && guix build -f guix.scm --root="$ROOT")

if [ ! -x "$ROOT/bin/ai-memory" ]; then
	echo "install.sh: $ROOT/bin/ai-memory is missing after the build" >&2
	exit 1
fi

echo "ai-memory: $store"
echo "ai-memory: root $ROOT -> $(readlink -f "$ROOT")"

if [ "$link" = yes ]; then
	mkdir -p "$(dirname "$LINK")"
	ln -sf "$ROOT/bin/ai-memory" "$LINK"
	echo "ai-memory: $LINK -> $ROOT/bin/ai-memory"
	case :$PATH: in
	*:"$(dirname "$LINK")":*) ;;
	*) echo "ai-memory: note: $(dirname "$LINK") is not on PATH" ;;
	esac
fi

"$ROOT/bin/ai-memory" --help >/dev/null 2>&1 || {
	echo "install.sh: $ROOT/bin/ai-memory does not run" >&2
	exit 1
}
echo "ai-memory: ok"