|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +RED='\033[0;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +NC='\033[0m' |
| 9 | + |
| 10 | +REPO="Quozul/PicoLimbo" |
| 11 | +BINARY_NAME="pico_limbo" |
| 12 | + |
| 13 | +print_status() { |
| 14 | + echo -e "${GREEN}[INFO]${NC} $1" >&2 |
| 15 | +} |
| 16 | + |
| 17 | +print_warning() { |
| 18 | + echo -e "${YELLOW}[WARN]${NC} $1" >&2 |
| 19 | +} |
| 20 | + |
| 21 | +print_error() { |
| 22 | + echo -e "${RED}[ERROR]${NC} $1" >&2 |
| 23 | +} |
| 24 | + |
| 25 | +detect_arch() { |
| 26 | + local arch=$(uname -m) |
| 27 | + case $arch in |
| 28 | + x86_64) |
| 29 | + echo "x86_64" |
| 30 | + ;; |
| 31 | + aarch64|arm64) |
| 32 | + echo "aarch64" |
| 33 | + ;; |
| 34 | + *) |
| 35 | + print_error "Unsupported architecture: $arch" |
| 36 | + print_error "Supported architectures: x86_64, aarch64" |
| 37 | + exit 1 |
| 38 | + ;; |
| 39 | + esac |
| 40 | +} |
| 41 | + |
| 42 | +get_install_dir() { |
| 43 | + if [[ $EUID -eq 0 ]]; then |
| 44 | + echo "/usr/local/bin" |
| 45 | + else |
| 46 | + local user_bin="$HOME/.local/bin" |
| 47 | + mkdir -p "$user_bin" |
| 48 | + echo "$user_bin" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +check_path() { |
| 53 | + local dir="$1" |
| 54 | + if [[ ":$PATH:" != *":$dir:"* ]]; then |
| 55 | + print_warning "Directory '$dir' is not in your PATH." |
| 56 | + print_warning "Please add the following line to your shell profile (e.g., ~/.bashrc or ~/.zshrc):" |
| 57 | + print_warning "export PATH=\"\$PATH:$dir\"" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +get_latest_release_url() { |
| 62 | + local arch="$1" |
| 63 | + local api_url="https://api.github.com/repos/$REPO/releases/latest" |
| 64 | + |
| 65 | + local download_url |
| 66 | + if command -v curl >/dev/null 2>&1; then |
| 67 | + download_url=$(curl -s "$api_url" | grep -o "https://[^\"]*linux-${arch}-musl\.tar\.gz" | head -1) |
| 68 | + elif command -v wget >/dev/null 2>&1; then |
| 69 | + download_url=$(wget -qO- "$api_url" | grep -o "https://[^\"]*linux-${arch}-musl\.tar\.gz" | head -1) |
| 70 | + else |
| 71 | + print_error "Neither curl nor wget is available. Please install one of them." |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + |
| 75 | + if [[ -z "$download_url" ]]; then |
| 76 | + print_error "Could not find download URL for architecture: $arch" |
| 77 | + print_error "Please check if a release asset exists for your architecture." |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + |
| 81 | + echo "$download_url" |
| 82 | +} |
| 83 | + |
| 84 | +download_and_install() { |
| 85 | + local url="$1" |
| 86 | + local install_dir="$2" |
| 87 | + local temp_dir=$(mktemp -d) |
| 88 | + |
| 89 | + local filename=$(basename "$url") |
| 90 | + if command -v curl >/dev/null 2>&1; then |
| 91 | + if ! curl --fail --silent --location -o "$temp_dir/$filename" "$url"; then |
| 92 | + print_error "Failed to download the file using curl." |
| 93 | + rm -rf "$temp_dir" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + else |
| 97 | + if ! wget --quiet -O "$temp_dir/$filename" "$url"; then |
| 98 | + print_error "Failed to download the file using wget." |
| 99 | + rm -rf "$temp_dir" |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | + fi |
| 103 | + |
| 104 | + if ! tar -xzf "$temp_dir/$filename" -C "$temp_dir"; then |
| 105 | + print_error "Failed to extract the archive. It may be corrupted." |
| 106 | + rm -rf "$temp_dir" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + local binary_path=$(find "$temp_dir" -name "$BINARY_NAME" -type f | head -1) |
| 111 | + |
| 112 | + if [[ -z "$binary_path" ]]; then |
| 113 | + print_error "Could not find '$BINARY_NAME' in the extracted files." |
| 114 | + print_error "Contents of archive:" |
| 115 | + find "$temp_dir" |
| 116 | + rm -rf "$temp_dir" |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + |
| 120 | + if ! mv "$binary_path" "$install_dir/$BINARY_NAME"; then |
| 121 | + print_error "Failed to move binary to $install_dir. Check permissions." |
| 122 | + rm -rf "$temp_dir" |
| 123 | + exit 1 |
| 124 | + fi |
| 125 | + chmod +x "$install_dir/$BINARY_NAME" |
| 126 | + |
| 127 | + rm -rf "$temp_dir" |
| 128 | +} |
| 129 | + |
| 130 | +main() { |
| 131 | + if [[ "$(uname)" != "Linux" ]]; then |
| 132 | + print_error "This script is designed for Linux systems only." |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + |
| 136 | + local arch=$(detect_arch) |
| 137 | + |
| 138 | + local install_dir=$(get_install_dir) |
| 139 | + |
| 140 | + if [[ ! -w "$install_dir" ]]; then |
| 141 | + print_error "No write permission to $install_dir." |
| 142 | + if [[ $EUID -ne 0 ]]; then |
| 143 | + print_error "Try running with 'sudo' for a global installation." |
| 144 | + fi |
| 145 | + exit 1 |
| 146 | + fi |
| 147 | + |
| 148 | + local download_url=$(get_latest_release_url "$arch") |
| 149 | + |
| 150 | + download_and_install "$download_url" "$install_dir" |
| 151 | + |
| 152 | + if [[ $EUID -ne 0 ]]; then |
| 153 | + check_path "$install_dir" |
| 154 | + fi |
| 155 | + |
| 156 | + local binary_path="$install_dir/$BINARY_NAME" |
| 157 | + print_status "The program has been installed at: $binary_path" |
| 158 | + |
| 159 | + if command -v "$BINARY_NAME" >/dev/null 2>&1; then |
| 160 | + "$BINARY_NAME" --help |
| 161 | + fi |
| 162 | +} |
| 163 | + |
| 164 | +main "$@" |
0 commit comments