code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(145 lines)
1 #!/usr/bin/env bash
2 set -euo pipefail
4 OWNER="bruce-hill"
5 REPO="tomo"
7 # Fetch latest release tag
8 TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/releases/latest" \
9 | grep -Po '"tag_name": "\K.*?(?=")')
11 if [[ -z "$TAG" ]]; then
12 echo "Failed to get latest release tag"
13 exit 1
14 fi
16 # Detect platform
17 OS="$(uname -s)"
18 ARCH="$(uname -m)"
19 case "$OS" in
20 Linux)
21 case "$ARCH" in
22 x86_64) FILE="tomo-linux-x86_64.tar.gz" ;;
23 aarch64|arm64) FILE="tomo-linux-aarch64.tar.gz" ;;
24 *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
25 esac
26 ;;
27 Darwin)
28 FILE="tomo-macos-universal.tar.gz"
29 ;;
30 *)
31 echo "Unsupported OS: $OS"
32 exit 1
33 ;;
34 esac
36 # Download the artifact (if not present)
37 if ! [ -e "$FILE" ]; then
38 URL="https://github.com/$OWNER/$REPO/releases/download/$TAG/$FILE"
39 echo "Downloading $URL ..."
40 curl -L -o "$FILE" "$URL"
41 echo "Downloaded $FILE"
42 fi
44 # Download checksum (if not present)
45 if ! [ -e "$FILE.sha256" ]; then
46 CHECKSUM_URL="$URL.sha256"
47 echo "Downloading checksum $CHECKSUM_URL ..."
48 curl -L -o "$FILE.sha256" "$CHECKSUM_URL"
49 fi
51 # Verify checksum
52 shasum --check "$FILE.sha256"
53 echo "Verified checksum"
55 # Configure `doas` vs `sudo`
56 if command -v doas >/dev/null 2>&1; then
57 SUDO="doas"
58 elif command -v sudo >/dev/null 2>&1; then
59 SUDO="sudo"
60 else
61 echo "Neither doas nor sudo found." >&2
62 exit 1
63 fi
65 # Autodetect package manager:
66 if [ -z "${PACKAGE_MANAGER:-}" ]; then
67 if command -v dnf >/dev/null 2>&1; then
68 PACKAGE_MANAGER="dnf"
69 elif command -v yay >/dev/null 2>&1; then
70 PACKAGE_MANAGER="yay"
71 elif command -v paru >/dev/null 2>&1; then
72 PACKAGE_MANAGER="paru"
73 elif command -v pacman >/dev/null 2>&1; then
74 PACKAGE_MANAGER="pacman"
75 elif command -v xbps-install >/dev/null 2>&1; then
76 PACKAGE_MANAGER="xbps"
77 elif command -v pkg_add >/dev/null 2>&1; then
78 PACKAGE_MANAGER="pkg_add"
79 elif command -v pkg >/dev/null 2>&1; then
80 PACKAGE_MANAGER="freebsd-pkg"
81 elif command -v brew >/dev/null 2>&1; then
82 PACKAGE_MANAGER="brew"
83 elif command -v port >/dev/null 2>&1; then
84 PACKAGE_MANAGER="macports"
85 elif command -v zypper >/dev/null 2>&1; then
86 PACKAGE_MANAGER="zypper"
87 elif command -v nix-env >/dev/null 2>&1; then
88 PACKAGE_MANAGER="nix"
89 elif command -v spack >/dev/null 2>&1; then
90 PACKAGE_MANAGER="spack"
91 elif command -v conda >/dev/null 2>&1; then
92 PACKAGE_MANAGER="conda"
93 elif command -v apt >/dev/null 2>&1; then
94 PACKAGE_MANAGER="apt"
95 elif command -v apt-get >/dev/null 2>&1; then
96 PACKAGE_MANAGER="apt-get"
97 else
98 echo "Unsupported package manager" >&2
99 exit 1
103 # Install packages
104 echo 'Installing dependencies...'
105 case "$PACKAGE_MANAGER" in
106 apt) $SUDO apt install libgc-dev libunistring-dev binutils libgmp-dev ;;
107 apt-get) $SUDO apt-get install libgc-dev libunistring-dev binutils libgmp-dev ;;
108 dnf) $SUDO dnf install gc-devel libunistring-devel binutils gmp-devel ;;
109 pacman) $SUDO pacman -S gc libunistring binutils gmp ;;
110 yay|paru) $PACKAGE_MANAGER -S gc libunistring binutils gmp ;;
111 xbps) $SUDO xbps-install -S gc libunistring binutils gmp ;;
112 pkg_add) $SUDO pkg_add boehm-gc libunistring binutils gmp ;;
113 freebsd-pkg) $SUDO pkg install boehm-gc libunistring binutils gmp ;;
114 brew) brew install bdw-gc libunistring binutils llvm gmp ;;
115 macports) $SUDO port install boehm-gc libunistring binutils gmp ;;
116 zypper) $SUDO zypper install gc-devel libunistring-devel binutils gmp-devel ;;
117 nix) nix-env -iA nixpkgs.boehmgc.dev nixpkgs.libunistring nixpkgs.binutils nixpkgs.nixpkgs.gmp ;;
118 spack) spack install boehm-gc libunistring binutils gmp ;;
119 conda) conda install boehm-gc libunistring binutils gmp ;;
121 echo "Unknown package manager: $PACKAGE_MANAGER" >&2
122 exit 1
124 esac
126 # Choose installation location
127 default_prefix='/usr/local'
128 if echo "$PATH" | tr ':' '\n' | grep -qx "$HOME/.local/bin"; then
129 default_prefix="~/.local"
131 printf '\033[1mChoose where to install Tomo (default: %s):\033[m ' "$default_prefix"
132 read DEST </dev/tty
133 if [ -z "$DEST" ]; then DEST="$default_prefix"; fi
134 DEST="${DEST/#\~/$HOME}"
136 # Install
137 if ! [ -w "$DEST" ]; then
138 USER="$(ls -ld "$DEST" | awk '{print $$3}')"
139 $(SUDO) -u "$USER" tar -xzf "$FILE" -C "$DEST" --strip-components=1 "tomo@$TAG"
140 else
141 tar -xzf "$FILE" -C "$DEST" --strip-components=1 "tomo@$TAG"
143 echo "Installed to $DEST"
145 rm -f "$FILE" "$FILE.sha256"