|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | + |
| 5 | +GITHUB_REPO="sxwebdev/gcx" |
| 6 | +TARGET_BINARY="gcx" |
| 7 | + |
| 8 | +# Determine the operating system |
| 9 | +OS=$(uname) |
| 10 | +if [ "$OS" == "Darwin" ]; then |
| 11 | + PLATFORM="darwin" |
| 12 | +elif [ "$OS" == "Linux" ]; then |
| 13 | + PLATFORM="linux" |
| 14 | +else |
| 15 | + echo "Unsupported OS: $OS" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +# Determine the architecture |
| 20 | +ARCH=$(uname -m) |
| 21 | +case "$ARCH" in |
| 22 | + x86_64) |
| 23 | + ARCH="amd64" |
| 24 | + ;; |
| 25 | + arm64|aarch64) |
| 26 | + ARCH="arm64" |
| 27 | + ;; |
| 28 | + *) |
| 29 | + echo "Unsupported architecture: $ARCH" |
| 30 | + exit 1 |
| 31 | + ;; |
| 32 | +esac |
| 33 | + |
| 34 | +echo "Detected platform: $PLATFORM, architecture: $ARCH" |
| 35 | + |
| 36 | +# Fetch the latest release information from GitHub API |
| 37 | +API_URL="https://api.github.com/repos/${GITHUB_REPO}/releases/latest" |
| 38 | +RELEASE_INFO=$(curl --silent "$API_URL") |
| 39 | + |
| 40 | +# Ensure jq is installed |
| 41 | +if ! command -v jq &>/dev/null; then |
| 42 | + echo "Error: jq is not installed. Please install jq and try again." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +# Find the asset matching the platform and architecture |
| 47 | +ASSET_NAME=$(echo "$RELEASE_INFO" | jq -r --arg platform "$PLATFORM" --arg arch "$ARCH" ' |
| 48 | + .assets[] | select(.name | test($platform) and test($arch)) | .name |
| 49 | +') |
| 50 | + |
| 51 | +if [ -z "$ASSET_NAME" ]; then |
| 52 | + echo "No archive found for platform $PLATFORM and architecture $ARCH" |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +echo "Found archive: $ASSET_NAME" |
| 57 | + |
| 58 | +# Get the download URL for the asset |
| 59 | +ASSET_URL=$(echo "$RELEASE_INFO" | jq -r --arg asset "$ASSET_NAME" ' |
| 60 | + .assets[] | select(.name == $asset) | .browser_download_url |
| 61 | +') |
| 62 | + |
| 63 | +if [ -z "$ASSET_URL" ]; then |
| 64 | + echo "Failed to obtain download URL." |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +echo "Downloading binary..." |
| 69 | +curl -L --silent -o "$ASSET_NAME" "$ASSET_URL" |
| 70 | + |
| 71 | +# Extract the archive. We assume it contains a single file named like gcx_darwin_amd64. |
| 72 | +echo "Extracting archive..." |
| 73 | +tar -xzf "$ASSET_NAME" |
| 74 | + |
| 75 | +# Find the extracted binary file (assuming only one file is extracted) |
| 76 | +EXTRACTED_BINARY=$(find . -maxdepth 1 -type f -name "gcx_*" | head -n 1) |
| 77 | + |
| 78 | +if [ -z "$EXTRACTED_BINARY" ]; then |
| 79 | + echo "Error: No binary file found after extraction." |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +echo "Renaming $EXTRACTED_BINARY to $TARGET_BINARY..." |
| 84 | +mv "$EXTRACTED_BINARY" "$TARGET_BINARY" |
| 85 | + |
| 86 | +# Directory for installing executables |
| 87 | +INSTALL_DIR="/usr/local/bin" |
| 88 | + |
| 89 | +echo "Moving binary to $INSTALL_DIR..." |
| 90 | +# Use sudo if necessary |
| 91 | +sudo mv "$TARGET_BINARY" "$INSTALL_DIR/$TARGET_BINARY" |
| 92 | + |
| 93 | +echo "Making the binary executable..." |
| 94 | +sudo chmod +x "$INSTALL_DIR/$TARGET_BINARY" |
| 95 | + |
| 96 | +# Clean up the downloaded archive |
| 97 | +rm "$ASSET_NAME" |
| 98 | + |
| 99 | +echo "Installation complete. You can now run $TARGET_BINARY from the terminal." |
0 commit comments