install.sh RawHistoryFolder
1#!/bin/sh
2set -e
3
4# Gordon installer script
5# Usage: curl -fsSL https://gordon.bnema.dev/install | bash
6# Usage with pre-release: curl -fsSL https://gordon.bnema.dev/install | GORDON_PRERELEASE=1 bash
7
8REPO="bnema/gordon"
9INSTALL_DIR="/usr/local/bin"
10VERSION="${GORDON_VERSION:-latest}"
11
12echo "Installing Gordon..."
13
14# Detect OS
15OS=$(uname -s | tr '[:upper:]' '[:lower:]')
16case "$OS" in
17 linux)
18 OS="linux"
19 ;;
20 darwin)
21 OS="darwin"
22 ;;
23 *)
24 echo "Error: Unsupported operating system: $OS"
25 exit 1
26 ;;
27esac
28
29# Detect architecture
30ARCH=$(uname -m)
31case "$ARCH" in
32 x86_64|amd64)
33 ARCH="amd64"
34 ;;
35 aarch64|arm64)
36 ARCH="arm64"
37 ;;
38 *)
39 echo "Error: Unsupported architecture: $ARCH"
40 exit 1
41 ;;
42esac
43
44echo "Detected: ${OS}/${ARCH}"
45
46# Construct download URL
47TARBALL="gordon_${OS}_${ARCH}.tar.gz"
48
49# Determine version to download
50if [ "$VERSION" = "latest" ] && [ -n "$GORDON_PRERELEASE" ]; then
51 echo "Finding latest pre-release..."
52 # Get latest release (including pre-releases) from GitHub API
53 RELEASE_DATA=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases?per_page=1" 2>/dev/null || echo "")
54 if [ -z "$RELEASE_DATA" ]; then
55 echo "Error: Failed to fetch release information from GitHub API"
56 exit 1
57 fi
58 VERSION=$(echo "$RELEASE_DATA" | grep -m1 '"tag_name"' | sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/')
59 if [ -z "$VERSION" ]; then
60 echo "Error: Could not determine latest pre-release version"
61 exit 1
62 fi
63 echo "Using pre-release version: ${VERSION}"
64elif [ "$VERSION" = "latest" ]; then
65 echo "Using latest stable release"
66 # Resolve "latest" to actual tag name since GitHub download URLs require exact tags
67 RELEASE_DATA=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null || echo "")
68 if [ -z "$RELEASE_DATA" ]; then
69 echo "Error: Failed to fetch latest release information from GitHub API"
70 exit 1
71 fi
72 VERSION=$(echo "$RELEASE_DATA" | grep -m1 '"tag_name"' | sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/')
73 if [ -z "$VERSION" ]; then
74 echo "Error: Could not determine latest stable version"
75 exit 1
76 fi
77 echo "Resolved version: ${VERSION}"
78else
79 echo "Using version: ${VERSION}"
80fi
81
82DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${TARBALL}"
83
84# Create temporary directory
85TMP_DIR=$(mktemp -d)
86trap 'rm -rf "$TMP_DIR"' EXIT
87
88# Download checksums file
89CHECKSUMS_URL="https://github.com/${REPO}/releases/download/${VERSION}/checksums.txt"
90echo "Downloading checksums..."
91if ! curl -fsSL "$CHECKSUMS_URL" -o "$TMP_DIR/checksums.txt"; then
92 echo "Error: Failed to download checksums file"
93 exit 1
94fi
95
96echo "Downloading ${TARBALL}..."
97if ! curl -fsSL "$DOWNLOAD_URL" -o "$TMP_DIR/$TARBALL"; then
98 echo "Error: Failed to download ${TARBALL}"
99 exit 1
100fi
101
102# Verify checksum
103echo "Verifying checksum..."
104TARBALL_ESCAPED=$(printf '%s\n' "$TARBALL" | sed 's/[.[\*^$()+?{|]/\\&/g')
105CHECKSUM_LINES=$(grep -E "^[0-9a-fA-F]{64}[[:space:]]+\*?${TARBALL_ESCAPED}\$" "$TMP_DIR/checksums.txt" || true)
106CHECKSUM_COUNT=$(printf '%s\n' "$CHECKSUM_LINES" | sed '/^$/d' | wc -l | tr -d ' ')
107if [ "$CHECKSUM_COUNT" != "1" ]; then
108 echo "Error: Expected exactly one checksum for ${TARBALL} in checksums.txt"
109 exit 1
110fi
111EXPECTED_CHECKSUM=$(printf '%s\n' "$CHECKSUM_LINES" | awk '{print $1}')
112
113# Calculate actual checksum (works on both Linux and macOS)
114if command -v sha256sum >/dev/null 2>&1; then
115 ACTUAL_CHECKSUM=$(sha256sum "$TMP_DIR/$TARBALL" | awk '{print $1}')
116elif command -v shasum >/dev/null 2>&1; then
117 ACTUAL_CHECKSUM=$(shasum -a 256 "$TMP_DIR/$TARBALL" | awk '{print $1}')
118else
119 echo "Error: Neither 'sha256sum' nor 'shasum' was found. Cannot verify checksum."
120 echo ""
121 echo "Please install one of these tools:"
122 echo " Debian/Ubuntu: sudo apt-get install coreutils"
123 echo " Fedora/RHEL: sudo dnf install coreutils"
124 echo " Alpine: apk add coreutils"
125 echo " macOS: shasum should be pre-installed"
126 exit 1
127fi
128
129if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
130 echo "Error: Checksum verification failed!"
131 echo "Expected: $EXPECTED_CHECKSUM"
132 echo "Actual: $ACTUAL_CHECKSUM"
133 echo "The downloaded file may be corrupted or tampered with."
134 exit 1
135fi
136echo "Checksum verified successfully."
137
138echo "Extracting Gordon binary..."
139EXTRACT_DIR="$TMP_DIR/extract"
140mkdir -p "$EXTRACT_DIR"
141tar -xzf "$TMP_DIR/$TARBALL" -C "$EXTRACT_DIR" gordon
142
143BINARY="$EXTRACT_DIR/gordon"
144if [ ! -f "$BINARY" ] || [ -L "$BINARY" ]; then
145 echo "Error: Archive did not contain a regular Gordon binary"
146 exit 1
147fi
148
149echo "Installing to ${INSTALL_DIR}..."
150if [ -w "$INSTALL_DIR" ]; then
151 install -m 0755 "$BINARY" "$INSTALL_DIR/gordon"
152else
153 echo "sudo required to install to ${INSTALL_DIR}"
154 sudo install -m 0755 "$BINARY" "$INSTALL_DIR/gordon"
155fi
156
157# Verify installation
158if command -v gordon >/dev/null 2>&1; then
159 echo ""
160 echo "Gordon installed successfully!"
161 gordon version
162else
163 echo ""
164 echo "Installation complete. You may need to add ${INSTALL_DIR} to your PATH."
165fi