-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·166 lines (144 loc) · 4.85 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·166 lines (144 loc) · 4.85 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
#!/usr/bin/env bash
set -euo pipefail
REPO="athrvk/vayu"
APP_NAME="Vayu"
INSTALL_DIR="/Applications"
APP_PATH="${INSTALL_DIR}/${APP_NAME}.app"
SIDECAR_REL="Contents/Resources/bin/vayu-engine"
MODE="install"
PURGE=0
# Run a command, or just print it when VAYU_DRYRUN=1.
run() {
if [ "${VAYU_DRYRUN:-0}" = "1" ]; then
printf '[dry-run] %s\n' "$*"
else
"$@"
fi
}
parse_args() {
MODE="install"
PURGE=0
while [ "$#" -gt 0 ]; do
case "$1" in
--uninstall) MODE="uninstall" ;;
--purge) PURGE=1 ;;
--help|-h) MODE="help" ;;
*) printf 'Unknown option: %s\n' "$1" >&2; return 2 ;;
esac
shift
done
}
resolve_version() {
if [ -n "${VAYU_VERSION:-}" ]; then
printf '%s' "$VAYU_VERSION"
return 0
fi
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' \
| head -1 \
| sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"v?([^"]+)".*/\1/'
}
download_url() {
local version="$1"
printf 'https://github.com/%s/releases/download/v%s/%s-%s-universal.zip' \
"$REPO" "$version" "$APP_NAME" "$version"
}
require_macos() {
[ "${VAYU_DRYRUN:-0}" = "1" ] && return 0
[ "$(uname -s)" = "Darwin" ] || { printf 'Vayu installer supports macOS only.\n' >&2; exit 1; }
for tool in curl unzip codesign xattr shasum; do
command -v "$tool" >/dev/null 2>&1 || { printf 'Required tool missing: %s\n' "$tool" >&2; exit 1; }
done
}
do_install() {
require_macos
(
local version url workdir zip expected actual staged keepalive_pid=""
version="$(resolve_version)"
[ -n "$version" ] || { printf 'Could not determine version to install.\n' >&2; exit 1; }
url="$(download_url "$version")"
printf 'Installing Vayu %s...\n' "$version"
workdir="$(mktemp -d)"
# Authorize the privileged copy into /Applications up front, so the
# password prompt appears immediately instead of after the download.
if [ "${VAYU_DRYRUN:-0}" != "1" ]; then
printf 'Vayu installs to %s and needs administrator access.\n' "$INSTALL_DIR"
sudo -v || { printf 'Authorization failed - aborting.\n' >&2; exit 1; }
# Refresh the sudo timestamp in the background so it does not expire
# during a slow download; the loop stops once sudo can no longer
# refresh non-interactively (i.e. after the script exits).
( while true; do sleep 60; sudo -n true 2>/dev/null || exit; done ) &
keepalive_pid=$!
fi
trap 'rm -rf "$workdir"; [ -n "$keepalive_pid" ] && kill "$keepalive_pid" 2>/dev/null' EXIT
zip="$workdir/vayu.zip"
printf 'Downloading %s\n' "$url"
# Default curl meter (drop -s) shows %, size, speed and ETA on stderr.
run curl -fL "$url" -o "$zip"
# Optional integrity check if the release publishes a .sha256
if [ "${VAYU_DRYRUN:-0}" != "1" ] && curl -fsSL "$url.sha256" -o "$zip.sha256" 2>/dev/null; then
expected="$(awk '{print $1}' "$zip.sha256")"
actual="$(shasum -a 256 "$zip" | awk '{print $1}')"
[ "$expected" = "$actual" ] || { printf 'Checksum mismatch - aborting.\n' >&2; exit 1; }
printf 'Checksum verified.\n'
fi
printf 'Extracting...\n'
run unzip -q -o "$zip" -d "$workdir"
staged="$workdir/${APP_NAME}.app"
# Ad-hoc sign + de-quarantine in the temp dir first, so a failure
# never leaves a broken bundle in /Applications. No sudo needed here.
printf 'Signing (ad-hoc) and removing quarantine...\n'
run codesign --force --sign - "$staged/$SIDECAR_REL"
run codesign --force --deep --sign - "$staged"
run xattr -cr "$staged"
printf 'Installing to %s...\n' "$INSTALL_DIR"
run sudo rm -rf "$APP_PATH"
run sudo ditto "$staged" "$APP_PATH"
printf 'Done. Launch Vayu from Launchpad/Spotlight, or run: open "%s"\n' "$APP_PATH"
)
}
do_uninstall() {
require_macos
local support prefs logs caches savedstate
support="$HOME/Library/Application Support/vayu-client"
prefs="$HOME/Library/Preferences/com.vayu.client.plist"
logs="$HOME/Library/Logs/vayu-client"
caches="$HOME/Library/Caches/com.vayu.client"
savedstate="$HOME/Library/Saved Application State/com.vayu.client.savedState"
printf 'Removing %s (you may be prompted for your password)...\n' "$APP_PATH"
run sudo rm -rf "$APP_PATH"
if [ "${PURGE:-0}" = "1" ]; then
printf 'Purging user data...\n'
run rm -rf "$support"
run rm -f "$prefs"
run rm -rf "$logs"
run rm -rf "$caches"
run rm -rf "$savedstate"
printf 'Vayu and its data have been removed.\n'
else
printf 'Vayu removed. User data was kept at:\n'
printf ' %s\n' "$support"
printf ' %s\n' "$prefs"
printf ' %s\n' "$logs"
printf 'Re-run with --purge to remove these too.\n'
fi
}
main() {
parse_args "$@"
case "$MODE" in
help) usage ;;
install) do_install ;;
uninstall) do_uninstall ;;
esac
}
usage() {
cat <<EOF
Vayu installer
install: bash -c "\$(curl -fsSL <url>)"
pin version: VAYU_VERSION=x.y.z bash -c "\$(curl -fsSL <url>)"
uninstall: bash -c "\$(curl -fsSL <url>)" -- --uninstall [--purge]
EOF
}
if [ "${VAYU_TEST:-0}" != "1" ]; then
main "$@"
fi