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..."
104EXPECTED_CHECKSUM=$(grep -E "[[:space:]]${TARBALL}\$" "$TMP_DIR/checksums.txt" | awk '{print $1}')
105if [ -z "$EXPECTED_CHECKSUM" ]; then
106 echo "Error: Could not find checksum for ${TARBALL} in checksums.txt"
107 exit 1
108fi
109
110# Calculate actual checksum (works on both Linux and macOS)
111if command -v sha256sum >/dev/null 2>&1; then
112 ACTUAL_CHECKSUM=$(sha256sum "$TMP_DIR/$TARBALL" | awk '{print $1}')
113elif command -v shasum >/dev/null 2>&1; then
114 ACTUAL_CHECKSUM=$(shasum -a 256 "$TMP_DIR/$TARBALL" | awk '{print $1}')
115else
116 echo "Error: Neither 'sha256sum' nor 'shasum' was found. Cannot verify checksum."
117 echo ""
118 echo "Please install one of these tools:"
119 echo " Debian/Ubuntu: sudo apt-get install coreutils"
120 echo " Fedora/RHEL: sudo dnf install coreutils"
121 echo " Alpine: apk add coreutils"
122 echo " macOS: shasum should be pre-installed"
123 exit 1
124fi
125
126if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
127 echo "Error: Checksum verification failed!"
128 echo "Expected: $EXPECTED_CHECKSUM"
129 echo "Actual: $ACTUAL_CHECKSUM"
130 echo "The downloaded file may be corrupted or tampered with."
131 exit 1
132fi
133echo "Checksum verified successfully."
134
135echo "Extracting..."
136tar -xzf "$TMP_DIR/$TARBALL" -C "$TMP_DIR"
137
138echo "Installing to ${INSTALL_DIR}..."
139if [ -w "$INSTALL_DIR" ]; then
140 mv "$TMP_DIR/gordon" "$INSTALL_DIR/gordon"
141else
142 echo "sudo required to install to ${INSTALL_DIR}"
143 sudo mv "$TMP_DIR/gordon" "$INSTALL_DIR/gordon"
144fi
145
146# Verify installation
147if command -v gordon >/dev/null 2>&1; then
148 echo ""
149 echo "Gordon installed successfully!"
150 gordon version
151else
152 echo ""
153 echo "Installation complete. You may need to add ${INSTALL_DIR} to your PATH."
154fi