Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·421 lines (391 loc) · 11.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·421 lines (391 loc) · 11.4 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env bash
# Copyright 2026ff novatechflow (Alexander Alten)
# SPDX-License-Identifier: PolyForm-Shield-1.0.0
# Check/install Go, build polyDisplay, write the Polymarket wallet, and
# install a login service on macOS or Linux.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LABEL="com.novatechflow.polydisplay"
GO_MIN="$(awk '/^go /{print $2; exit}' "$DIR/go.mod")"
GO_MIN="${GO_MIN:-1.26}"
die() { echo "error: $*" >&2; exit 1; }
py() {
if command -v python3 >/dev/null 2>&1; then python3 "$@"
elif command -v python >/dev/null 2>&1; then python "$@"
else die "python3 is required to write config.json"
fi
}
os_arch() {
case "$(uname -s)" in
Darwin) OS=darwin ;;
Linux) OS=linux ;;
*) die "unsupported OS: $(uname -s) (macOS and Linux only)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) ARCH=amd64 ;;
arm64|aarch64) ARCH=arm64 ;;
*) die "unsupported arch: $(uname -m)" ;;
esac
BIN="$DIR/polydisplayd"
}
ver_ge() {
local a="$1" b="$2"
a="${a#go}"; b="${b#go}"
local a1 a2 b1 b2
IFS=. read -r a1 a2 _ <<<"${a}.0.0"
IFS=. read -r b1 b2 _ <<<"${b}.0.0"
a1=${a1:-0}; a2=${a2:-0}; b1=${b1:-0}; b2=${b2:-0}
[ "$a1" -gt "$b1" ] || { [ "$a1" -eq "$b1" ] && [ "$a2" -ge "$b2" ]; }
}
have_go() {
command -v go >/dev/null 2>&1 || return 1
local v
v="$(go env GOVERSION 2>/dev/null || go version | awk '{print $3}')"
ver_ge "$v" "$GO_MIN"
}
fetch_go() {
local ver url tmp dest
dest="$DIR/.go"
echo "==> Go $GO_MIN+ not on PATH; downloading a toolchain into $dest"
ver="$(curl -fsSL "https://go.dev/VERSION?m=text" 2>/dev/null | head -1 || true)"
[ -n "$ver" ] || ver="go${GO_MIN}.0"
url="https://go.dev/dl/${ver}.${OS}-${ARCH}.tar.gz"
tmp="$(mktemp /tmp/go-XXXX.tgz)"
curl -fL --progress-bar -o "$tmp" "$url"
rm -rf "$dest"
mkdir -p "$dest"
tar -C "$dest" -xzf "$tmp"
rm -f "$tmp"
export GOROOT="$dest/go"
export PATH="$GOROOT/bin:$PATH"
have_go || die "downloaded Go still below $GO_MIN"
echo " using $(go version)"
}
ensure_go() {
if have_go; then
echo "==> Using $(go version)"
return
fi
if [ -x "$DIR/.go/go/bin/go" ]; then
export GOROOT="$DIR/.go/go"
export PATH="$GOROOT/bin:$PATH"
if have_go; then
echo "==> Using bundled $(go version)"
return
fi
fi
command -v curl >/dev/null 2>&1 || die "curl is required to download Go"
fetch_go
}
is_wallet() { [[ "$1" =~ ^0x[a-fA-F0-9]{40}$ ]]; }
existing_wallet() {
py - "$DIR/config.json" <<'PY' 2>/dev/null || true
import json, sys
try:
print(json.load(open(sys.argv[1])).get("wallet") or "")
except Exception:
pass
PY
}
ask_wallet() {
local current w
current="$(existing_wallet)"
if [ -n "${POLYMARKET_WALLET:-}" ]; then
w="$POLYMARKET_WALLET"
elif [ -t 0 ]; then
if [ -n "$current" ]; then
echo "Current Polymarket wallet: $current"
read -r -p "Polymarket public address (0x..., empty to keep): " w
[ -n "$w" ] || w="$current"
else
read -r -p "Polymarket public address (0x..., optional; empty for assets only): " w
fi
else
w="$current"
fi
[ -z "$w" ] || is_wallet "$w" || die "invalid Polymarket public address 0x + 40 hex (or set POLYMARKET_WALLET)"
WALLET="$w"
}
ensure_env() {
if [ ! -f "$DIR/.env" ]; then
[ -f "$DIR/.env.example" ] || die "missing .env.example"
echo "==> Creating .env from .env.example (BTC ETH SOL XRP)"
cp "$DIR/.env.example" "$DIR/.env"
fi
}
env_get() {
py - "$DIR/.env" "$1" <<'PY'
import re, sys
path, key = sys.argv[1], sys.argv[2]
try:
text = open(path).read()
except FileNotFoundError:
sys.exit(0)
m = re.search(r'(?m)^%s=(.*)$' % re.escape(key), text)
if not m:
sys.exit(0)
print(m.group(1).strip().strip('"').strip("'"))
PY
}
env_set() {
py - "$DIR/.env" "$1" "$2" <<'PY'
import re, sys
path, key, val = sys.argv[1], sys.argv[2], sys.argv[3]
try:
text = open(path).read()
except FileNotFoundError:
text = ""
line = "%s=%s" % (key, val)
m = re.search(r'(?m)^%s=.*$' % re.escape(key), text)
if m:
text = text[:m.start()] + line + text[m.end():]
elif text and not text.endswith("\n"):
text += "\n" + line + "\n"
else:
text += line + "\n"
open(path, "w").write(text)
PY
}
ensure_auth() {
local pin sec
pin="$(env_get POLYDISPLAY_PIN || true)"
if [ -z "$pin" ]; then
pin="$(py -c 'import secrets; print("%06d" % secrets.randbelow(1000000))')"
env_set POLYDISPLAY_PIN "$pin"
fi
sec="$(env_get POLYDISPLAY_TOKEN_SECRET || true)"
if [ -z "$sec" ]; then
sec="$(openssl rand -hex 32 2>/dev/null || py -c 'import secrets; print(secrets.token_hex(32))')"
env_set POLYDISPLAY_TOKEN_SECRET "$sec"
fi
echo
echo "Pad PIN (also POLYDISPLAY_PIN in .env — look it up any time):"
echo " $pin"
echo "Type it once on the pad. After that the browser keeps a bearer token."
echo "Clearing site data on the pad means typing the PIN again."
}
# CoinGecko search supplies a stable id and display name. Runtime market data
# comes from Kraken with Coinbase fallback.
resolve_asset() {
py - "$1" <<'PY'
import json, sys, urllib.parse, urllib.request
q = sys.argv[1].strip()
if not q:
sys.exit(2)
ua = {"User-Agent": "polyDisplay-install"}
def get(url):
req = urllib.request.Request(url, headers=ua)
with urllib.request.urlopen(req, timeout=15) as r:
return json.load(r)
try:
raw = get("https://api.coingecko.com/api/v3/search?query=" + urllib.parse.quote(q))
except Exception as e:
print("lookup failed: %s" % e, file=sys.stderr)
sys.exit(3)
coins = raw.get("coins") or []
if not coins:
sys.exit(4)
ql = q.lower()
pick = None
for c in coins:
if (c.get("symbol") or "").lower() == ql or (c.get("id") or "").lower() == ql:
pick = c
break
if pick is None:
pick = coins[0]
sym = (pick.get("symbol") or q).upper()
name = pick.get("name") or sym
cid = pick.get("id") or ""
if not cid:
sys.exit(4)
print("%s:%s:%s" % (sym, name, cid))
PY
}
ask_assets() {
local extra raw spec line
extra="${POLYDISPLAY_EXTRA_ASSETS:-}"
if [ -z "$extra" ] && [ -t 0 ]; then
echo "Watchlist starts with BTC, ETH, SOL, XRP (Polymarket)."
echo "Extra names are looked up on CoinGecko; charts use Kraken then Coinbase."
read -r -p "Additional assets to chart (tickers or names, comma-separated, empty to skip): " extra
fi
[ -n "$extra" ] || return 0
echo "==> Resolving extra assets..."
local -a specs=()
IFS=',' read -r -a raw <<<"$extra"
for line in "${raw[@]}"; do
line="$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
[ -n "$line" ] || continue
if spec="$(resolve_asset "$line")"; then
echo " $spec"
specs+=("$spec")
else
echo " skip $line (not found on CoinGecko)"
fi
done
if [ ${#specs[@]} -gt 0 ]; then
py - "$DIR/.env" "${specs[@]}" <<'PY'
import re, sys
path, extras = sys.argv[1], sys.argv[2:]
try:
text = open(path).read()
except FileNotFoundError:
text = ""
m = re.search(r'(?m)^POLYDISPLAY_ASSETS=(.*)$', text)
cur = ""
if m:
cur = m.group(1).strip().strip('"').strip("'")
have = set()
parts = []
for item in cur.split(","):
item = item.strip()
if not item:
continue
fields = item.split(":")
key = (fields[2] if len(fields) >= 3 else fields[0]).strip().lower()
if key in have:
continue
have.add(key)
parts.append(item)
for spec in extras:
fields = spec.split(":")
key = (fields[2] if len(fields) >= 3 else fields[0]).strip().lower()
if key in have:
continue
have.add(key)
parts.append(spec)
line = 'POLYDISPLAY_ASSETS="%s"' % ",".join(parts)
if m:
text = text[:m.start()] + line + text[m.end():]
elif text and not text.endswith("\n"):
text += "\n" + line + "\n"
else:
text += line + "\n"
open(path, "w").write(text)
PY
fi
}
write_config() {
echo "==> Writing wallet into config.json"
py - "$DIR/config.json" "$WALLET" <<'PY'
import json, os, sys
path, wallet = sys.argv[1], sys.argv[2]
if os.path.exists(path):
try:
cfg = json.load(open(path))
except Exception:
cfg = {}
else:
cfg = {}
cfg["wallet"] = wallet
cfg.setdefault("candleDays", 1)
cfg.setdefault("sort", "trades")
if not cfg.get("port"):
cfg.pop("port", None)
# coins stay out of config unless already set — POLYDISPLAY_ASSETS in .env is the list
if not cfg.get("coins"):
cfg.pop("coins", None)
with open(path, "w") as f:
json.dump(cfg, f, indent=2)
f.write("\n")
PY
}
build() {
echo "==> Building server..."
( cd "$DIR" && go build -o "$BIN" . )
if [ "$OS" = darwin ]; then
echo "==> Ad-hoc codesigning $BIN"
codesign --force --sign - "$BIN" 2>/dev/null || echo " (codesign skipped)"
fi
}
install_macos() {
local plist="$HOME/Library/LaunchAgents/$LABEL.plist"
echo "==> Writing LaunchAgent to $plist"
mkdir -p "$HOME/Library/LaunchAgents"
sed -e "s#__BIN__#$BIN#g" -e "s#__DIR__#$DIR#g" \
"$DIR/com.novatechflow.polydisplay.plist" > "$plist"
local fw=/usr/libexec/ApplicationFirewall/socketfilterfw
if [ -x "$fw" ] && "$fw" --getglobalstate 2>/dev/null | grep -qi "enabled"; then
echo "==> Firewall is enabled; adding an exception for polydisplayd (needs sudo)"
sudo "$fw" --add "$BIN" >/dev/null 2>&1 || true
sudo "$fw" --unblockapp "$BIN" >/dev/null 2>&1 || true
fi
echo "==> (Re)loading service"
launchctl unload "$plist" 2>/dev/null || true
launchctl load "$plist"
STOP="launchctl unload $plist"
}
install_linux() {
local unit_dir="$HOME/.config/systemd/user"
local unit="$unit_dir/polydisplay.service"
echo "==> Writing systemd user unit to $unit"
mkdir -p "$unit_dir"
cat > "$unit" <<EOF
[Unit]
Description=polyDisplay
After=network.target
[Service]
WorkingDirectory=$DIR
ExecStart=$BIN
Restart=always
RestartSec=3
[Install]
WantedBy=default.target
EOF
if command -v systemctl >/dev/null 2>&1 && systemctl --user list-units >/dev/null 2>&1; then
systemctl --user daemon-reload
systemctl --user enable --now polydisplay.service
STOP="systemctl --user disable --now polydisplay.service"
else
echo " (no systemd --user; start with: $BIN)"
nohup "$BIN" >/dev/null 2>&1 &
STOP="kill the polydisplayd process"
fi
}
lan_ip() {
case "$OS" in
darwin) ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "<host-ip>" ;;
linux) hostname -I 2>/dev/null | awk '{print $1}' || echo "<host-ip>" ;;
esac
}
port() {
if [ -n "${POLYDISPLAY_PORT:-}" ]; then echo "$POLYDISPLAY_PORT"; return; fi
py - "$DIR/.env" "$DIR/config.json" <<'PY' 2>/dev/null || echo 8080
import os, json, sys
env_path, cfg_path = sys.argv[1], sys.argv[2]
if os.path.exists(env_path):
for line in open(env_path):
line = line.strip()
if line.startswith("POLYDISPLAY_PORT="):
v = line.split("=", 1)[1].strip().strip('"').strip("'")
if v.isdigit() and int(v) > 0:
print(v)
raise SystemExit
try:
p = json.load(open(cfg_path)).get("port") or 0
print(int(p) if p else 8080)
except Exception:
print(8080)
PY
}
os_arch
ensure_go
ensure_env
ensure_auth
ask_wallet
ask_assets
write_config
build
case "$OS" in
darwin) install_macos ;;
linux) install_linux ;;
esac
IP="$(lan_ip)"
PORT="$(port)"
echo
echo "polyDisplay is running and will auto-start at login."
echo " On a pad or browser: http://$IP:$PORT"
echo " Pad PIN: $(env_get POLYDISPLAY_PIN || true) (POLYDISPLAY_PIN in .env)"
echo " Logs: $DIR/polydisplay.log"
echo " Stop: $STOP"
echo " Update after edits: re-run $0"