Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·168 lines (150 loc) · 6.24 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·168 lines (150 loc) · 6.24 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
#!/bin/bash
# WiFi Optimizer - Decky Plugin Installer
# Usage: curl -fsSL https://github.com/ArcadaLabs-Jason/WifiOptimizer/raw/main/install.sh -o /tmp/wifi-opt-install.sh && sudo bash /tmp/wifi-opt-install.sh
set -e
PLUGIN_NAME="WiFi Optimizer"
# Check for root (needed to write to plugin dir and restart service)
if [ "$(id -u)" -ne 0 ]; then
echo "Error: This script must be run with sudo."
echo "Run: sudo bash $0"
exit 1
fi
# Resolve the real user's home directory (same method as Decky's own installer)
DECK_USER="${SUDO_USER:-$(logname 2>/dev/null || echo deck)}"
USER_HOME="$(getent passwd "$DECK_USER" | cut -d: -f6)"
if [ -z "$USER_HOME" ]; then
USER_HOME="/home/$DECK_USER"
fi
PLUGIN_BASE="$USER_HOME/homebrew/plugins"
PLUGIN_DIR="$PLUGIN_BASE/$PLUGIN_NAME"
# Check Decky is installed
if [ ! -d "$PLUGIN_BASE" ]; then
echo "Error: Decky Loader not found at $PLUGIN_BASE"
echo "Install it first: https://decky.xyz"
exit 1
fi
# The latest release's tag, from where GitHub redirects /releases/latest
# (not the API, which allows 60 lookups an hour per address and is easily
# used up behind a shared address). Without a release tag nothing is
# installed: the main branch can carry unpublished work.
echo "Checking for latest release..."
RELEASES="https://github.com/ArcadaLabs-Jason/WifiOptimizer/releases"
LATEST_URL=$(curl -s -o /dev/null -w '%{redirect_url}' --proto '=https' --connect-timeout 15 --max-time 60 "$RELEASES/latest" || true)
case "$LATEST_URL" in
"$RELEASES/tag/"*) LATEST_TAG="${LATEST_URL#"$RELEASES/tag/"}" ;;
*) LATEST_TAG="" ;;
esac
# Only a plain version tag goes into the download URL and the folder name.
case "$LATEST_TAG" in
v[0-9]*) case "$LATEST_TAG" in *[!A-Za-z0-9.-]*) LATEST_TAG="" ;; esac ;;
*) LATEST_TAG="" ;;
esac
if [ -z "$LATEST_TAG" ]; then
echo "Error: Couldn't find the latest release. Try again in a few minutes."
exit 1
fi
echo "Latest release: $LATEST_TAG"
# The release's own tarball, the one its signed SHA256SUMS lists, so what is
# installed can be checked against the release. GitHub's generated archive
# of the tag is listed nowhere. Both unpack to WifiOptimizer-<version>/.
REPO_URL="$RELEASES/download/${LATEST_TAG}/wifi-optimizer-${LATEST_TAG}.tar.gz"
DIR_NAME="WifiOptimizer-${LATEST_TAG#v}"
TMP_DIR=$(mktemp -d)
# Staging and the previous copy live beside the plugins folder, not in it:
# Decky loads every folder there that has a plugin.json. Same filesystem, so
# each move below is a rename rather than a copy.
STAGE_DIR="$USER_HOME/homebrew/.wifi-optimizer-stage"
OLD_DIR="$USER_HOME/homebrew/.wifi-optimizer-previous"
# Set just before the installed copy is moved aside. If the script is cut
# off (a closed terminal, Ctrl+C) before the new copy is in its place, the
# old one goes back, so the plugin folder is never left missing. A missing
# folder would also make the dispatcher remove itself at the next connect.
MOVED_ASIDE=false
cleanup() {
rm -rf "$TMP_DIR" "$STAGE_DIR"
if [ "$MOVED_ASIDE" = true ] && [ ! -d "$PLUGIN_DIR" ] && [ -d "$OLD_DIR" ]; then
mv "$OLD_DIR" "$PLUGIN_DIR" && echo "Interrupted: the previous version was put back."
fi
}
trap cleanup EXIT
echo "Installing $PLUGIN_NAME..."
# Download and extract
echo "Downloading..."
curl -sSL --fail --proto '=https' --connect-timeout 15 --max-time 300 "$REPO_URL" -o "$TMP_DIR/plugin.tar.gz" ||
{ echo "Error: Couldn't download $LATEST_TAG. Try again in a few minutes."; exit 1; }
tar xzf "$TMP_DIR/plugin.tar.gz" -C "$TMP_DIR" ||
{ echo "Error: Couldn't unpack $LATEST_TAG. Try again."; exit 1; }
SRC="$TMP_DIR/$DIR_NAME"
for f in plugin.json package.json main.py decky.pyi dist/index.js defaults/dispatcher.sh.tmpl; do
if [ ! -f "$SRC/$f" ]; then
echo "Error: The download is incomplete. Try again."
exit 1
fi
done
# The "version" of a package.json, or nothing. Only a plain version is
# printed.
version_of() {
local v
v=$(sed -n 's/^[[:space:]]*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$1" 2>/dev/null | head -n 1)
case "$v" in
[0-9]*) case "$v" in *[!A-Za-z0-9.-]*) v="" ;; esac ;;
*) v="" ;;
esac
echo "$v"
}
# What is installed now and what replaces it. The latest stable release can
# be older than what is installed (a beta, say), so the end says which
# version went in and which was there, never that it was updated.
NEW_VERSION=$(version_of "$SRC/package.json")
[ -n "$NEW_VERSION" ] || NEW_VERSION="${LATEST_TAG#v}"
OLD_VERSION=""
[ -f "$PLUGIN_DIR/package.json" ] && OLD_VERSION=$(version_of "$PLUGIN_DIR/package.json")
# Build the new copy completely before touching the installed one, so a
# failure part way (a full disk, a closed terminal) leaves it working.
rm -rf "$STAGE_DIR"
mkdir -p "$STAGE_DIR/dist" "$STAGE_DIR/defaults" "$STAGE_DIR/py_modules"
cp "$SRC/plugin.json" "$SRC/package.json" "$SRC/main.py" "$SRC/decky.pyi" "$STAGE_DIR/"
cp "$SRC/dist/index.js" "$STAGE_DIR/dist/"
cp "$SRC/dist/index.js.map" "$STAGE_DIR/dist/" 2>/dev/null || true
cp "$SRC/defaults/dispatcher.sh.tmpl" "$STAGE_DIR/defaults/"
touch "$STAGE_DIR/py_modules/.keep"
# Install
if [ -d "$PLUGIN_DIR" ]; then
echo "Replacing the installed copy..."
UPGRADING=true
rm -rf "$OLD_DIR"
MOVED_ASIDE=true
mv "$PLUGIN_DIR" "$OLD_DIR"
else
echo "Installing to $PLUGIN_DIR..."
UPGRADING=false
fi
if ! mv "$STAGE_DIR" "$PLUGIN_DIR"; then
echo "Error: Couldn't move the new version into place."
if [ "$UPGRADING" = true ]; then
mv "$OLD_DIR" "$PLUGIN_DIR" && echo "The previous version was put back."
fi
exit 1
fi
# The new copy is in place; a previous copy that will not delete is only
# clutter, so it must not stop the restart below.
rm -rf "$OLD_DIR" || true
# Restart Decky
echo "Restarting Decky Loader..."
if ! systemctl restart plugin_loader; then
echo ""
echo "Installed, but Decky Loader did not restart. Restart the device to load it."
exit 1
fi
echo ""
if [ "$UPGRADING" = true ]; then
if [ -z "$OLD_VERSION" ]; then
echo "Installed v$NEW_VERSION."
elif [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
echo "Reinstalled v$NEW_VERSION."
else
echo "Installed v$NEW_VERSION (was v$OLD_VERSION)."
fi
else
echo "WiFi Optimizer installed! Open the Quick Access Menu to configure."
fi