-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
79 lines (64 loc) · 2.7 KB
/
Copy pathinstall.sh
File metadata and controls
79 lines (64 loc) · 2.7 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
#!/usr/bin/env bash
set -euo pipefail
REPO="ufuayk/u"
BRANCH="macos"
ARCHIVE_URL="https://github.com/${REPO}/archive/refs/heads/${BRANCH}.tar.gz"
INSTALL_NAME="u"
# Update this after any change to u.rb or libr/:
# ( cd . && find u.rb libr -type f | LC_ALL=C sort | while IFS= read -r f; do shasum -a 256 "$f"; done ) | shasum -a 256 | awk '{print $1}'
EXPECTED_TREE_SHA="63e38c255f8f2311226cb6047ebee1dedf64b5526d80756305a6d638825c93a0"
info() { printf "\033[34m==>\033[0m %s\n" "$1"; }
ok() { printf "\033[32m✓\033[0m %s\n" "$1"; }
fail() { printf "\033[31m✗ %s\033[0m\n" "$1" >&2; exit 1; }
command -v ruby >/dev/null 2>&1 || fail "ruby not found. macOS ships Ruby by default — check your PATH."
command -v curl >/dev/null 2>&1 || fail "curl is required to install u."
command -v tar >/dev/null 2>&1 || fail "tar is required to install u."
USE_SUDO=""
if [ -w "/usr/local/bin" ]; then
DEST_DIR="/usr/local/bin"
elif command -v sudo >/dev/null 2>&1; then
DEST_DIR="/usr/local/bin"
USE_SUDO="sudo"
else
DEST_DIR="$HOME/.local/bin"
mkdir -p "$DEST_DIR"
fi
SHARE_DIR="$(dirname "$DEST_DIR")/share/u"
LAUNCHER="$DEST_DIR/$INSTALL_NAME"
info "Downloading u..."
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
curl -fsSL "$ARCHIVE_URL" -o "$TMP_DIR/u.tar.gz" || fail "download failed."
tar -xzf "$TMP_DIR/u.tar.gz" -C "$TMP_DIR" || fail "could not extract archive."
SRC_DIR="$(find "$TMP_DIR" -maxdepth 1 -type d -name 'u-*' -print -quit)"
if [ -z "$SRC_DIR" ] || [ ! -f "$SRC_DIR/u.rb" ]; then
fail "archive layout unexpected — aborting."
fi
if command -v shasum >/dev/null 2>&1; then
ACTUAL_TREE_SHA="$( ( cd "$SRC_DIR" && find u.rb libr -type f | LC_ALL=C sort | while IFS= read -r f; do shasum -a 256 "$f"; done ) | shasum -a 256 | awk '{print $1}' )"
[ "$ACTUAL_TREE_SHA" = "$EXPECTED_TREE_SHA" ] || fail "checksum mismatch — download may be corrupted or tampered with."
else
head -1 "$SRC_DIR/u.rb" | grep -q '^#!/usr/bin/env ruby' || fail "downloaded file doesn't look right — aborting."
fi
info "Installing to $SHARE_DIR..."
$USE_SUDO mkdir -p "$SHARE_DIR/libr"
$USE_SUDO cp "$SRC_DIR/u.rb" "$SHARE_DIR/u.rb"
$USE_SUDO cp "$SRC_DIR"/libr/*.rb "$SHARE_DIR/libr/"
$USE_SUDO chmod 755 "$SHARE_DIR/u.rb"
printf '#!/bin/sh\nexec ruby "%s" "$@"\n' "$SHARE_DIR/u.rb" > "$TMP_DIR/launcher"
$USE_SUDO chmod 755 "$TMP_DIR/launcher"
$USE_SUDO mv "$TMP_DIR/launcher" "$LAUNCHER"
ok "u installed at $LAUNCHER"
case ":$PATH:" in
*":$DEST_DIR:"*) ;;
*)
echo
printf "\033[33m!\033[0m %s is not on your PATH.\n" "$DEST_DIR"
echo " Add this to your shell profile (~/.zshrc or ~/.bashrc):"
echo
echo " export PATH=\"$DEST_DIR:\$PATH\""
echo
;;
esac
echo
ok "Done. Run 'u' to get started."