aboutsummaryrefslogtreecommitdiff
path: root/bin/li
blob: 33f5d3aa8aa0d96956f3de8bc855114d088b5788 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  li [-I IMPL ] [-v] [OPTIONS...]
		  li -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -I IMPL       use the given implementation (default: $LISP_CLI_IMPL)
		  -v            verbose mode
		  -h, --help    show this message

		  OPTIONS       options to be forwarded to cl(1) (lisp-cli)


		Run the cl(1) executable with OPTIONS, but make sure an up-to-date
		image exists and load it.


		Examples:

		  Launch a REPL from an image:

		    $ li


		  Give options to cl(1):

		    $ li -I sbcl -e '(print 123)'
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

VERBOSE=false
IMPL="${LISP_CLI_IMPL:-clisp}"
while getopts ':I:vh' flag; do
	case "$flag" in
		I)
			IMPL="$OPTARG"
			;;
		v)
			VERBOSE=true
			;;
		h)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done
if [ "${1:-}" = '--' ]; then
	shift
fi


IMAGE="${XDG_DATA_HOME:-$HOME/.local/share}/lisp-cli/$IMPL.image"
BIN="$(command -v cl)"
INIT="${XDG_CONFIG_HOME:-$HOME/.config}/lisp-cli/init.lisp"

mkdir -p "$(dirname "$IMAGE")"

if [ ! -e "$IMAGE" ]; then
	printf 'Bootstrapping a new "%s" image...\n' "$IMPL" >&2
	cl \
		-I "$IMPL"                             \
		-v                                     \
		-e '(ql:quickload :trivial-dump-core)' \
		-E "(trivial-dump-core:dump-image \"$IMAGE\")"
elif [ -n "$(find "$0" "$BIN" "$INIT" -newer "$IMAGE")" ]; then
	printf 'Refresh existing "%s" image...\n' "$IMPL" >&2
	cl \
		-M "$IMAGE"                            \
		-I "$IMPL"                             \
		-v                                     \
		-e '(ql:quickload :trivial-dump-core)' \
		-E "(trivial-dump-core:dump-image \"$IMAGE\")"
fi

if [ "$VERBOSE" = true ]; then
	set -x
fi
exec cl -M "$IMAGE" "$@"