Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·287 lines (264 loc) · 12.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·287 lines (264 loc) · 12.3 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/sh
# OpenCircle for Linux, one command, no sudo:
#
# curl -fsSL https://raw.githubusercontent.com/AmerAlreyahi/OpenCircle/main/scripts/install.sh | sh
# ... | sh -s -- --uninstall
#
# Downloads the latest release's AppImage, checks it, puts it in ~/.local/bin, adds
# the app-menu entry and icon (app id com.opencircle.app), and starts it. Checks, in
# this order, and stops at the first that fails (nothing is installed then):
# 1. the minisign signature over SHA256SUMS.txt, made with the release key the
# app's own updater trusts, with the trusted comment "OpenCircle v<tag>";
# 2. the AppImage's SHA-256 against that signed list.
# Needs: curl or wget, openssl 3. Safe to run again (it upgrades in place).
set -eu
REPO="AmerAlreyahi/OpenCircle"
APP_ID="com.opencircle.app"
# The release signing key (minisign public key). The same line is verify.rs's
# RELEASE_PUBLIC_KEY; a test keeps the two equal.
KEY="RWSbS2U2Cjix7vMbkgnr+jEZI2VKl7+HWo2Scq4uImBxKNgXCPo+K5oY"
# DER header of an Ed25519 public key (SubjectPublicKeyInfo), base64.
SPKI="MCowBQYDK2VwAyEA"
# Test mode (the script's own test): another key, another place to download from,
# no app start. Off unless asked for, and loud when on.
if [ "${OPENCIRCLE_INSTALL_TEST:-}" = 1 ]; then
KEY=${OPENCIRCLE_KEY:-$KEY}
echo "OpenCircle install: TEST MODE (key, download place and start are overridden)" >&2
else
OPENCIRCLE_BASE="" OPENCIRCLE_TAG="" OPENCIRCLE_NO_START=""
fi
say() { printf '%s\n' "$*"; }
# Every failure says what happened, what is safe, and what to do next.
die() {
printf 'OpenCircle install: %s\n' "$*" >&2
exit 1
}
usage() {
say "Usage: install.sh [--uninstall]"
say " (no option) install or upgrade OpenCircle in ~/.local/bin and start it"
say " --uninstall remove what this script installed (your settings stay)"
}
HOME_DIR=${HOME:-}
[ -n "$HOME_DIR" ] || die "HOME is not set, so there is no place to install to. Set HOME and run this again."
BIN_DIR="$HOME_DIR/.local/bin"
BIN="$BIN_DIR/opencircle"
DATA="${XDG_DATA_HOME:-$HOME_DIR/.local/share}"
DESKTOP="$DATA/applications/$APP_ID.desktop"
STUDIO="$DATA/applications/$APP_ID.studio.desktop"
ICON="$DATA/icons/hicolor/128x128/apps/$APP_ID.png"
AUTOSTART="${XDG_CONFIG_HOME:-$HOME_DIR/.config}/autostart/$APP_ID.desktop"
MARK="X-OpenCircle-Generated=true"
# Stop a running copy through its own command (no running copy is fine).
stop_app() {
[ -x "$BIN" ] && "$BIN" quit >/dev/null 2>&1 || true
}
# OpenCircle's keys in KDE's shortcut settings (KGlobalAccel, KDE Plasma): removed with
# the app. Nothing happens on other desktops (no KDE service is started for this).
forget_kde_keys() {
command -v busctl >/dev/null 2>&1 || return 0
set -- org.kde.kglobalaccel /kglobalaccel org.kde.KGlobalAccel
busctl --user --auto-start=no call "$@" allActionsForComponent as 1 com.opencircle.app 2>/dev/null |
grep -o '"[^"]*"' | awk 'NR % 4 == 2' | tr -d '"' |
while read -r action; do
busctl --user --auto-start=no call "$@" unregister ss com.opencircle.app "$action" >/dev/null 2>&1
done
}
uninstall() {
found=0
stop_app
forget_kde_keys
for f in "$BIN" "$ICON" "$DESKTOP" "$STUDIO" "$AUTOSTART"; do
[ -e "$f" ] || continue
# A desktop entry we did not write (a package's, or the user's) stays.
case $f in
"$DESKTOP" | "$STUDIO" | "$AUTOSTART") grep -q -e "$MARK" -e "$BIN" "$f" 2>/dev/null || continue ;;
esac
rm -f "$f"
say "Removed $f"
found=1
done
[ "$found" = 1 ] || say "Nothing to remove: OpenCircle is not installed by this script."
say "Your settings in ~/.config/opencircle are kept; delete that folder to remove them too."
}
case ${1:-} in
"") ;;
--uninstall) uninstall; exit 0 ;;
-h | --help) usage; exit 0 ;;
*) usage >&2; exit 2 ;;
esac
[ "$(uname -s)" = Linux ] || die "this installer is for Linux. Windows has an installer on the release page."
case $(uname -m) in
x86_64 | amd64) ARCH=x86_64 ;;
aarch64 | arm64) ARCH=aarch64 ;;
*) die "no OpenCircle build for $(uname -m). Releases: https://github.com/$REPO/releases" ;;
esac
command -v openssl >/dev/null 2>&1 || die "openssl is needed to check the download and is not installed. Install it (for example: sudo apt install openssl) and run this again."
case $(openssl version 2>/dev/null) in
"OpenSSL "[3-9]*) ;;
*) die "this needs OpenSSL 3 or newer to check the signature; found: $(openssl version 2>/dev/null). Nothing was installed." ;;
esac
TMP=$(mktemp -d) || die "could not make a temporary folder."
trap 'rm -rf "$TMP"' EXIT
trap 'exit 1' INT TERM HUP
# fetch URL DEST
fetch() {
case $1 in
file://*)
[ "${OPENCIRCLE_INSTALL_TEST:-}" = 1 ] || die "refusing a non-https download."
cp "${1#file://}" "$2" 2>/dev/null
;;
https://*)
if command -v curl >/dev/null 2>&1; then
curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1"
elif command -v wget >/dev/null 2>&1; then
wget -q --https-only -O "$2" "$1"
else
die "neither curl nor wget is installed. Install one (for example: sudo apt install curl) and run this again."
fi
;;
*) die "refusing a non-https download." ;;
esac
}
download() {
fetch "$1" "$2" || die "could not download $1. Nothing was installed; check your connection and run this again."
}
# slice FILE SKIP COUNT: bytes of a file. hex: stdin as lowercase hex.
slice() { dd if="$1" bs=1 skip="$2" count="$3" 2>/dev/null; }
hex() { od -An -v -tx1 | tr -d ' \n'; }
size() { wc -c <"$1" | tr -d ' '; }
b64() { openssl base64 -d -A 2>/dev/null; }
# verify_sums SUMS SIG TAG: the signature over SUMS is ours, for this release.
verify_sums() {
sums=$1 sig=$2 tag=$3
forged="the signature on this download is not valid, so it is not from the OpenCircle release process. Nothing was installed. Try again later; if it keeps happening, tell us at https://github.com/$REPO/issues"
printf '%s' "$KEY" | b64 >"$TMP/key.bin" || die "internal error: bad release key."
[ "$(size "$TMP/key.bin")" = 42 ] && [ "$(slice "$TMP/key.bin" 0 2 | hex)" = 4564 ] || die "internal error: bad release key."
sed -n 2p "$sig" | tr -d '\r\n' | b64 >"$TMP/sig.bin" || die "$forged"
[ "$(size "$TMP/sig.bin")" = 74 ] || die "$forged"
[ "$(slice "$TMP/sig.bin" 2 8 | hex)" = "$(slice "$TMP/key.bin" 2 8 | hex)" ] || die "$forged"
{ printf '%s' "$SPKI" | b64; slice "$TMP/key.bin" 10 32; } >"$TMP/pk.der"
slice "$TMP/sig.bin" 10 64 >"$TMP/sig64"
case $(slice "$TMP/sig.bin" 0 2 | hex) in
4564) msg=$sums ;; # "Ed": the file itself is signed
4544) # "ED": its BLAKE2b-512 is signed
openssl dgst -blake2b512 -binary "$sums" >"$TMP/msg" 2>/dev/null || die "$forged"
msg="$TMP/msg"
;;
*) die "$forged" ;;
esac
ed25519() { openssl pkeyutl -verify -pubin -inkey "$TMP/pk.der" -keyform DER -rawin -in "$1" -sigfile "$2" >/dev/null 2>&1; }
ed25519 "$msg" "$TMP/sig64" || die "$forged"
# The signed ("trusted") comment names the release; the global signature covers it.
comment=$(sed -n 's/^trusted comment: //p' "$sig" | tr -d '\r' | head -n 1)
sed -n 4p "$sig" | tr -d '\r\n' | b64 >"$TMP/gsig" || die "$forged"
[ "$(size "$TMP/gsig")" = 64 ] || die "$forged"
{ cat "$TMP/sig64"; printf '%s' "$comment"; } >"$TMP/gmsg"
ed25519 "$TMP/gmsg" "$TMP/gsig" || die "$forged"
[ "$comment" = "OpenCircle $tag" ] ||
die "the signature is for \"$comment\", not $tag: an old release is being served as the new one. Nothing was installed."
}
# --- the release -------------------------------------------------------------
TAG=${OPENCIRCLE_TAG:-}
if [ -z "$TAG" ]; then
download "https://api.github.com/repos/$REPO/releases/latest" "$TMP/latest.json"
TAG=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$TMP/latest.json" | head -n 1)
fi
case $TAG in
v[0-9]*.[0-9]*.[0-9]*) ;;
*) die "could not tell which release is the latest. Nothing was installed; try again in a minute, or download it from https://github.com/$REPO/releases" ;;
esac
case $TAG in *[!A-Za-z0-9.+-]*) die "unexpected release name \"$TAG\". Nothing was installed." ;; esac
BASE=${OPENCIRCLE_BASE:-https://github.com/$REPO/releases/download/$TAG}
say "Installing OpenCircle $TAG ..."
download "$BASE/SHA256SUMS.txt" "$TMP/SHA256SUMS.txt"
download "$BASE/SHA256SUMS.txt.minisig" "$TMP/SHA256SUMS.txt.minisig"
verify_sums "$TMP/SHA256SUMS.txt" "$TMP/SHA256SUMS.txt.minisig" "$TAG"
say "Signature OK (release key)."
# The AppImage for this machine, as the signed list names it.
ASSET=$(awk -v s="_$ARCH.AppImage" '{ n = $2; sub(/^\*/, "", n); if (length(n) > length(s) && substr(n, length(n) - length(s) + 1) == s) print n }' "$TMP/SHA256SUMS.txt")
case $ASSET in
"" | *"
"*) die "the release has no single $ARCH AppImage. Nothing was installed. Releases: https://github.com/$REPO/releases" ;;
*[!A-Za-z0-9._-]*) die "unexpected file name in the release. Nothing was installed." ;;
esac
WANT=$(awk -v n="$ASSET" '{ f = $2; sub(/^\*/, "", f); if (f == n) { print $1; exit } }' "$TMP/SHA256SUMS.txt")
case $WANT in
*[!0-9a-f]* | "") die "the signed list has no checksum for $ASSET. Nothing was installed." ;;
esac
[ "${#WANT}" = 64 ] || die "the signed list has no valid checksum for $ASSET. Nothing was installed."
download "$BASE/$ASSET" "$TMP/$ASSET"
GOT=$(openssl dgst -sha256 -r "$TMP/$ASSET" | cut -d ' ' -f 1)
[ "$GOT" = "$WANT" ] || die "the download's checksum does not match the signed list (it may be damaged or tampered with). Nothing was installed. Run this again; if it repeats, tell us at https://github.com/$REPO/issues"
say "Checksum OK."
# --- install -----------------------------------------------------------------
chmod 755 "$TMP/$ASSET"
mkdir -p "$BIN_DIR" "$DATA/applications" "${ICON%/*}" || die "could not create the folders under $HOME_DIR/.local. Nothing was installed."
stop_app
# Same folder, then rename: a running copy and a half-copied file never meet.
if ! { cp "$TMP/$ASSET" "$BIN.new" && mv -f "$BIN.new" "$BIN"; }; then
rm -f "$BIN.new"
die "could not write $BIN. Nothing was changed."
fi
# The icon is inside the AppImage; a missing one only costs the menu picture.
if (cd "$TMP" && "$TMP/$ASSET" --appimage-extract "usr/share/icons/hicolor/128x128/apps/opencircle.png" >/dev/null 2>&1) &&
[ -f "$TMP/squashfs-root/usr/share/icons/hicolor/128x128/apps/opencircle.png" ]; then
cp "$TMP/squashfs-root/usr/share/icons/hicolor/128x128/apps/opencircle.png" "$ICON"
else
say "Note: could not read the icon from the download; the menu entry has no picture."
fi
# Exec path in the desktop-entry quoting: plain characters only here, else refuse
# (the app's own entry writer quotes the rest; this script has no need to).
case $BIN in
*[!A-Za-z0-9/._+-]*) die "the install path $BIN has characters a menu entry cannot hold safely. Set HOME to a plainer path, or move OpenCircle yourself. The app is installed there already." ;;
esac
cat >"$DESKTOP" <<EOF
[Desktop Entry]
# Written by the OpenCircle installer. Remove with: install.sh --uninstall
Type=Application
Name=OpenCircle
Comment=Circle anything on screen to search, read, translate or ask about it
Exec=$BIN
Icon=$APP_ID
StartupWMClass=$APP_ID
Terminal=false
Categories=Utility;
$MARK
EOF
# OpenCircle Studio: an extra menu entry and "Open with" for pictures (never a default app).
cat >"$STUDIO" <<EOF
[Desktop Entry]
$MARK
Type=Application
Name=OpenCircle Studio
Comment=Edit and annotate a picture
Exec=$BIN studio %f
Icon=$APP_ID
Terminal=false
Categories=Graphics;
MimeType=image/png;image/jpeg;image/webp;image/bmp;image/gif;
EOF
command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "${DESKTOP%/*}" >/dev/null 2>&1 || true
say "Installed $BIN"
case ":${PATH:-}:" in
*":$BIN_DIR:"*) ;;
*) say "Note: $BIN_DIR is not on your PATH. Add it to run \"opencircle\" from a terminal; the app menu entry works already." ;;
esac
# --- start -------------------------------------------------------------------
if [ -n "${OPENCIRCLE_NO_START:-}" ]; then
exit 0
fi
if [ -z "${WAYLAND_DISPLAY:-}${DISPLAY:-}" ]; then
say "There is no desktop session here, so it was not started. Start OpenCircle from the app menu."
exit 0
fi
if command -v setsid >/dev/null 2>&1; then
setsid "$BIN" </dev/null >/dev/null 2>&1 &
else
"$BIN" </dev/null >/dev/null 2>&1 &
fi
sleep 2
if kill -0 $! 2>/dev/null; then
say "OpenCircle is running. Press Ctrl+Shift+Space to use it, or open it from the app menu any time."
else
say "It did not stay open. Run \"$BIN\" in a terminal to see why."
fi