-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·318 lines (260 loc) · 8.74 KB
/
install.sh
File metadata and controls
executable file
·318 lines (260 loc) · 8.74 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/sh
# Stenographer install / update script
# Usage:
# Install: curl -fsSL https://raw.githubusercontent.com/nbitslabs/stenographer/main/install.sh | sh
# Update: curl -fsSL https://raw.githubusercontent.com/nbitslabs/stenographer/main/install.sh | sh -s -- --update
#
# Environment variables for non-interactive install:
# STENOGRAPHER_APP_ID - Telegram App ID (numeric)
# STENOGRAPHER_APP_HASH - Telegram App Hash (alphanumeric)
# STENOGRAPHER_PHONE - Phone number in E.164 format (+1234567890)
# STENOGRAPHER_CONFIG_DIR - Custom config directory (default: ~/.config/stenographer)
set -e
REPO="nbitslabs/stenographer"
BINARY_NAME="stenographer"
UPDATE_ONLY=false
# Parse arguments
for arg in "$@"; do
case "$arg" in
--update) UPDATE_ONLY=true ;;
esac
done
# --- Helpers ---
info() { printf " %s\n" "$@"; }
ok() { printf " ✓ %s\n" "$@"; }
warn() { printf " ⚠ %s\n" "$@" >&2; }
fatal() { printf " ✗ %s\n" "$@" >&2; exit 1; }
# --- Platform detection ---
detect_platform() {
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Linux) OS="linux" ;;
Darwin) OS="darwin" ;;
*) fatal "Unsupported operating system: $OS" ;;
esac
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) fatal "Unsupported architecture: $ARCH" ;;
esac
info "Detected platform: ${OS}/${ARCH}"
}
# --- Version detection ---
get_latest_version() {
VERSION=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null | \
grep '"tag_name"' | sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/')
if [ -z "$VERSION" ]; then
fatal "Could not determine latest release version. Check https://github.com/${REPO}/releases"
fi
info "Latest version: ${VERSION}"
}
# --- Installed version check ---
check_installed_version() {
EXISTING=$(command -v "$BINARY_NAME" 2>/dev/null || true)
if [ -z "$EXISTING" ]; then
if [ "$UPDATE_ONLY" = true ]; then
fatal "Stenographer is not installed. Run without --update to install."
fi
return
fi
CURRENT=$("$EXISTING" version 2>/dev/null || echo "unknown")
info "Installed version: ${CURRENT}"
if [ "$CURRENT" = "$VERSION" ]; then
ok "Already up to date (${VERSION})"
exit 0
fi
info "Upgrading ${CURRENT} → ${VERSION}"
}
# --- Binary download ---
download_binary() {
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${BINARY_NAME}-${OS}-${ARCH}"
TMPFILE=$(mktemp)
info "Downloading ${DOWNLOAD_URL}..."
if ! curl -fsSL -o "$TMPFILE" "$DOWNLOAD_URL"; then
rm -f "$TMPFILE"
fatal "Download failed. Check that a release exists for ${OS}-${ARCH} at ${DOWNLOAD_URL}"
fi
chmod +x "$TMPFILE"
ok "Downloaded binary"
}
# --- Binary installation ---
install_binary() {
# If updating and we know where the existing binary lives, replace it there.
if [ -n "$EXISTING" ]; then
INSTALL_DIR=$(dirname "$EXISTING")
INSTALL_PATH="$EXISTING"
else
INSTALL_DIR="/usr/local/bin"
INSTALL_PATH="${INSTALL_DIR}/${BINARY_NAME}"
fi
if [ -w "$INSTALL_DIR" ]; then
mv "$TMPFILE" "$INSTALL_PATH"
chmod +x "$INSTALL_PATH"
else
printf " Need sudo access to install to %s. Continue? [Y/n] " "$INSTALL_DIR"
if [ -n "$STENOGRAPHER_APP_ID" ] || [ "$UPDATE_ONLY" = true ]; then
# Non-interactive mode: assume yes
printf "y (non-interactive)\n"
CONFIRM="y"
else
read -r CONFIRM
fi
case "$CONFIRM" in
[nN]*)
INSTALL_DIR="$HOME/.local/bin"
INSTALL_PATH="${INSTALL_DIR}/${BINARY_NAME}"
mkdir -p "$INSTALL_DIR"
mv "$TMPFILE" "$INSTALL_PATH"
chmod +x "$INSTALL_PATH"
case ":$PATH:" in
*":${INSTALL_DIR}:"*) ;;
*) warn "${INSTALL_DIR} is not in your PATH. Add it with: export PATH=\"${INSTALL_DIR}:\$PATH\"" ;;
esac
;;
*)
sudo mv "$TMPFILE" "$INSTALL_PATH"
sudo chmod +x "$INSTALL_PATH"
;;
esac
fi
ok "Installed to ${INSTALL_PATH}"
}
# --- Directory setup ---
setup_directories() {
CONFIG_DIR="${STENOGRAPHER_CONFIG_DIR:-$HOME/.config/stenographer}"
mkdir -p "$CONFIG_DIR"
mkdir -p "${CONFIG_DIR}/logs"
chmod 700 "$CONFIG_DIR"
chmod 700 "${CONFIG_DIR}/logs"
if [ -d "$CONFIG_DIR" ]; then
PERMS=$(stat -c '%a' "$CONFIG_DIR" 2>/dev/null || stat -f '%A' "$CONFIG_DIR" 2>/dev/null)
if [ "$PERMS" != "700" ]; then
warn "Config directory permissions are ${PERMS} (recommended: 700)"
fi
fi
ok "Config directory: ${CONFIG_DIR}"
}
# --- Credential collection ---
validate_app_id() {
case "$1" in
''|*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
validate_app_hash() {
case "$1" in
'') return 1 ;;
*[!a-zA-Z0-9]*) return 1 ;;
*) return 0 ;;
esac
}
validate_phone() {
case "$1" in
+[0-9]*) return 0 ;;
*) return 1 ;;
esac
}
mask_hash() {
LEN=${#1}
if [ "$LEN" -le 4 ]; then
printf '%s' "$1"
else
SHOW=$(printf '%s' "$1" | cut -c1-4)
printf '%s%s' "$SHOW" "$(printf '%*s' $((LEN - 4)) '' | tr ' ' '*')"
fi
}
collect_credentials() {
# Non-interactive mode
if [ -n "$STENOGRAPHER_APP_ID" ] && [ -n "$STENOGRAPHER_APP_HASH" ] && [ -n "$STENOGRAPHER_PHONE" ]; then
APP_ID="$STENOGRAPHER_APP_ID"
APP_HASH="$STENOGRAPHER_APP_HASH"
PHONE="$STENOGRAPHER_PHONE"
validate_app_id "$APP_ID" || fatal "Invalid STENOGRAPHER_APP_ID: must be numeric"
validate_app_hash "$APP_HASH" || fatal "Invalid STENOGRAPHER_APP_HASH: must be alphanumeric"
validate_phone "$PHONE" || fatal "Invalid STENOGRAPHER_PHONE: must be E.164 format (e.g., +1234567890)"
info "Using credentials from environment variables"
return
fi
# Interactive mode
while true; do
printf " Enter your Telegram App ID: "
read -r APP_ID
validate_app_id "$APP_ID" && break
warn "Invalid App ID: must be numeric and non-empty"
done
while true; do
printf " Enter your Telegram App Hash: "
read -r APP_HASH
validate_app_hash "$APP_HASH" && break
warn "Invalid App Hash: must be alphanumeric and non-empty"
done
while true; do
printf " Enter your phone number (E.164 format, e.g., +1234567890): "
read -r PHONE
validate_phone "$PHONE" && break
warn "Invalid phone: must start with + followed by digits"
done
# Confirmation
printf "\n Credentials summary:\n"
printf " App ID: %s\n" "$APP_ID"
printf " App Hash: %s\n" "$(mask_hash "$APP_HASH")"
printf " Phone: %s\n" "$PHONE"
printf " Confirm? [Y/n] "
read -r CONFIRM
case "$CONFIRM" in
[nN]*) fatal "Aborted by user" ;;
esac
}
# --- Config generation ---
generate_config() {
CONFIG_FILE="${CONFIG_DIR}/config.toml"
"$INSTALL_PATH" config init \
--app-id "$APP_ID" \
--app-hash "$APP_HASH" \
--phone "$PHONE" > "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
ok "Config written to ${CONFIG_FILE}"
}
# --- Verification ---
verify_install() {
INSTALLED_VERSION=$("$INSTALL_PATH" version 2>&1 || true)
if [ -n "$INSTALLED_VERSION" ]; then
ok "Verified: ${INSTALLED_VERSION}"
else
ok "Binary installed"
fi
}
# --- Main ---
main() {
if [ "$UPDATE_ONLY" = true ]; then
printf "\n Stenographer Updater\n"
printf " ====================\n\n"
else
printf "\n Stenographer Installer\n"
printf " ======================\n\n"
fi
detect_platform
get_latest_version
check_installed_version
download_binary
install_binary
if [ "$UPDATE_ONLY" = true ]; then
verify_install
printf "\n ✓ Stenographer updated successfully!\n\n"
else
setup_directories
collect_credentials
generate_config
verify_install
printf "\n ✓ Stenographer installed successfully!\n"
printf " Installed to: %s\n" "$INSTALL_PATH"
printf " Config: %s\n\n" "${CONFIG_DIR}/config.toml"
printf " Next steps:\n"
printf " 1. Authenticate: stenographer run\n"
printf " (complete the login flow, then Ctrl-C)\n"
printf " 2. Set up background service: stenographer service install && stenographer service start\n"
printf "\n Documentation: https://github.com/${REPO}#readme\n\n"
fi
}
main