|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +REPO="${WECHAT_CODEX_REPO:-Arlowen/wechat-codex}" |
| 6 | +VERSION="${WECHAT_CODEX_VERSION:-latest}" |
| 7 | +BASE_URL="${WECHAT_CODEX_BASE_URL:-}" |
| 8 | +BIN_NAME="wechat-codex" |
| 9 | +INSTALL_DIR="${INSTALL_DIR:-}" |
| 10 | +TMP_DIR="" |
| 11 | + |
| 12 | +log() { |
| 13 | + printf '[install] %s\n' "$*" |
| 14 | +} |
| 15 | + |
| 16 | +fail() { |
| 17 | + printf '[install] ERROR: %s\n' "$*" >&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +cleanup() { |
| 22 | + if [ -n "$TMP_DIR" ] && [ -d "$TMP_DIR" ]; then |
| 23 | + rm -rf "$TMP_DIR" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +need_cmd() { |
| 28 | + command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1" |
| 29 | +} |
| 30 | + |
| 31 | +detect_os() { |
| 32 | + case "$(uname -s)" in |
| 33 | + Linux) printf 'linux\n' ;; |
| 34 | + Darwin) printf 'darwin\n' ;; |
| 35 | + *) fail "unsupported operating system: $(uname -s)" ;; |
| 36 | + esac |
| 37 | +} |
| 38 | + |
| 39 | +detect_arch() { |
| 40 | + case "$(uname -m)" in |
| 41 | + x86_64|amd64) printf 'amd64\n' ;; |
| 42 | + arm64|aarch64) printf 'arm64\n' ;; |
| 43 | + *) fail "unsupported architecture: $(uname -m)" ;; |
| 44 | + esac |
| 45 | +} |
| 46 | + |
| 47 | +choose_install_dir() { |
| 48 | + if [ -n "$INSTALL_DIR" ]; then |
| 49 | + mkdir -p "$INSTALL_DIR" |
| 50 | + printf '%s\n' "$INSTALL_DIR" |
| 51 | + return |
| 52 | + fi |
| 53 | + |
| 54 | + if [ -d "/usr/local/bin" ] && [ -w "/usr/local/bin" ]; then |
| 55 | + printf '/usr/local/bin\n' |
| 56 | + return |
| 57 | + fi |
| 58 | + |
| 59 | + if mkdir -p "$HOME/.local/bin" 2>/dev/null; then |
| 60 | + printf '%s\n' "$HOME/.local/bin" |
| 61 | + return |
| 62 | + fi |
| 63 | + |
| 64 | + fail "no writable install directory found, please set INSTALL_DIR=/path/to/bin" |
| 65 | +} |
| 66 | + |
| 67 | +download_base_url() { |
| 68 | + if [ -n "$BASE_URL" ]; then |
| 69 | + printf '%s\n' "$BASE_URL" |
| 70 | + return |
| 71 | + fi |
| 72 | + |
| 73 | + if [ "$VERSION" = "latest" ]; then |
| 74 | + printf 'https://github.com/%s/releases/latest/download\n' "$REPO" |
| 75 | + else |
| 76 | + printf 'https://github.com/%s/releases/download/%s\n' "$REPO" "$VERSION" |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | +verify_checksum() { |
| 81 | + local asset="$1" |
| 82 | + local checksum_url="$2" |
| 83 | + local checksum_file="$TMP_DIR/checksums.txt" |
| 84 | + local checksum_line="$TMP_DIR/checksum.line" |
| 85 | + |
| 86 | + if ! command -v sha256sum >/dev/null 2>&1 && ! command -v shasum >/dev/null 2>&1; then |
| 87 | + log "sha256 tool not found, skip checksum verification" |
| 88 | + return |
| 89 | + fi |
| 90 | + |
| 91 | + if ! curl -fsSL "$checksum_url" -o "$checksum_file"; then |
| 92 | + log "checksums.txt not found, skip checksum verification" |
| 93 | + return |
| 94 | + fi |
| 95 | + |
| 96 | + if ! grep "[[:space:]]$asset\$" "$checksum_file" >"$checksum_line"; then |
| 97 | + log "checksum for $asset not found, skip checksum verification" |
| 98 | + return |
| 99 | + fi |
| 100 | + |
| 101 | + if command -v sha256sum >/dev/null 2>&1; then |
| 102 | + (cd "$TMP_DIR" && sha256sum -c "$(basename "$checksum_line")") |
| 103 | + else |
| 104 | + (cd "$TMP_DIR" && shasum -a 256 -c "$(basename "$checksum_line")") |
| 105 | + fi |
| 106 | +} |
| 107 | + |
| 108 | +main() { |
| 109 | + local os arch install_dir asset base_url archive_url checksum_url archive_path binary_path |
| 110 | + |
| 111 | + trap cleanup EXIT |
| 112 | + |
| 113 | + need_cmd curl |
| 114 | + need_cmd tar |
| 115 | + need_cmd mktemp |
| 116 | + need_cmd uname |
| 117 | + |
| 118 | + os="$(detect_os)" |
| 119 | + arch="$(detect_arch)" |
| 120 | + install_dir="$(choose_install_dir)" |
| 121 | + asset="${BIN_NAME}_${os}_${arch}.tar.gz" |
| 122 | + base_url="$(download_base_url)" |
| 123 | + archive_url="${base_url}/${asset}" |
| 124 | + checksum_url="${base_url}/checksums.txt" |
| 125 | + |
| 126 | + TMP_DIR="$(mktemp -d)" |
| 127 | + archive_path="$TMP_DIR/$asset" |
| 128 | + |
| 129 | + log "downloading ${archive_url}" |
| 130 | + curl -fsSL "$archive_url" -o "$archive_path" |
| 131 | + |
| 132 | + verify_checksum "$asset" "$checksum_url" |
| 133 | + |
| 134 | + tar -xzf "$archive_path" -C "$TMP_DIR" |
| 135 | + binary_path="$(find "$TMP_DIR" -type f -name "$BIN_NAME" | head -n 1)" |
| 136 | + [ -n "$binary_path" ] || fail "binary not found in release archive" |
| 137 | + |
| 138 | + cp "$binary_path" "$install_dir/$BIN_NAME" |
| 139 | + chmod 0755 "$install_dir/$BIN_NAME" |
| 140 | + |
| 141 | + log "installed to $install_dir/$BIN_NAME" |
| 142 | + if ! printf ':%s:' "$PATH" | grep -q ":$install_dir:"; then |
| 143 | + log "$install_dir is not in PATH" |
| 144 | + log "add this line to your shell profile: export PATH=\"$install_dir:\$PATH\"" |
| 145 | + fi |
| 146 | + log "verify with: $BIN_NAME version" |
| 147 | +} |
| 148 | + |
| 149 | +main "$@" |
0 commit comments