|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +REPO="th-nuernberg/usermgmt" |
| 5 | +DEFAULT_INSTALL_DIR="/usr/local/bin" |
| 6 | + |
| 7 | +usage(){ |
| 8 | + cat <<EOF |
| 9 | +Usage: $0 [TAG] [INSTALL_DIR] |
| 10 | +TAG: optional GitHub release tag (e.g. v0.6.3). If omitted, it uses latest. |
| 11 | +INSTALL_DIR: optional install directory (default: ${DEFAULT_INSTALL_DIR}) |
| 12 | +EOF |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then |
| 17 | + usage |
| 18 | + exit 0 |
| 19 | +fi |
| 20 | + |
| 21 | +if [ "$#" -gt 2 ]; then |
| 22 | + usage |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +TAG="${1:-latest}" |
| 27 | +INSTALL_DIR="${2:-$DEFAULT_INSTALL_DIR}" |
| 28 | + |
| 29 | +OS="$(uname -s)" |
| 30 | +case "$OS" in |
| 31 | + Darwin) PLATFORM="mac" ;; |
| 32 | + Linux) PLATFORM="linux" ;; |
| 33 | + *) |
| 34 | + echo "Unsupported OS: $OS" >&2 |
| 35 | + exit 2 |
| 36 | + ;; |
| 37 | +esac |
| 38 | + |
| 39 | +if [ "$PLATFORM" = "mac" ]; then |
| 40 | + ASSET="usermgmt-aarch64-apple-darwin.tar.gz" |
| 41 | +elif [ "$PLATFORM" = "linux" ]; then |
| 42 | + ASSET="usermgmt-x86_64-unknown-linux-gnu.tar.gz" |
| 43 | +fi |
| 44 | + |
| 45 | +# Resolve tag (if latest, follow GitHub redirect to get actual tag) |
| 46 | +if [ "$TAG" = "latest" ]; then |
| 47 | + echo "Resolving latest release tag..." |
| 48 | + final_url=$(curl -fsSL -o /dev/null -w '%{url_effective}' "https://github.com/${REPO}/releases/latest") |
| 49 | + TAG="$(basename "$final_url")" |
| 50 | + if [ -z "$TAG" ]; then |
| 51 | + echo "Failed to resolve latest release tag." >&2 |
| 52 | + exit 3 |
| 53 | + fi |
| 54 | + echo "Latest tag is: $TAG" |
| 55 | +fi |
| 56 | + |
| 57 | +DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${ASSET}" |
| 58 | +echo "Will download: $DOWNLOAD_URL" |
| 59 | + |
| 60 | +for cmd in curl tar; do |
| 61 | + if ! command -v "$cmd" >/dev/null 2>&1; then |
| 62 | + echo "Required tool '$cmd' not found. Install it and re-run." >&2 |
| 63 | + exit 4 |
| 64 | + fi |
| 65 | +done |
| 66 | + |
| 67 | +tmpdir="$(mktemp -d)" |
| 68 | +cleanup(){ rm -rf "$tmpdir"; } |
| 69 | +trap cleanup EXIT |
| 70 | + |
| 71 | +outfile="$tmpdir/$ASSET" |
| 72 | + |
| 73 | +echo "Downloading asset..." |
| 74 | +if ! curl -fL --progress-bar -o "$outfile" "$DOWNLOAD_URL"; then |
| 75 | + echo "Download failed. Check tag ($TAG) and that asset exists: $ASSET" >&2 |
| 76 | + exit 5 |
| 77 | +fi |
| 78 | + |
| 79 | +echo "Extracting..." |
| 80 | +tar -xzf "$outfile" -C "$tmpdir" |
| 81 | + |
| 82 | +# locate executable: prefer file named 'usermgmt', otherwise first executable file found |
| 83 | +found="$(find "$tmpdir" -type f -name 'usermgmt' -perm -111 -print -quit 2>/dev/null || true)" |
| 84 | +if [ -z "$found" ]; then |
| 85 | + found="$(find "$tmpdir" -type f -perm -111 -print -quit 2>/dev/null || true)" |
| 86 | +fi |
| 87 | + |
| 88 | +if [ -z "$found" ]; then |
| 89 | + echo "Could not find an executable inside the archive." >&2 |
| 90 | + echo "Archive contents:" >&2 |
| 91 | + find "$tmpdir" -ls >&2 || true |
| 92 | + exit 6 |
| 93 | +fi |
| 94 | + |
| 95 | +echo "Found executable: $found" |
| 96 | + |
| 97 | +if [ ! -d "$INSTALL_DIR" ]; then |
| 98 | + echo "Creating install dir: $INSTALL_DIR" |
| 99 | + mkdir -p "$INSTALL_DIR" |
| 100 | +fi |
| 101 | + |
| 102 | +dst="$INSTALL_DIR/usermgmt" |
| 103 | +echo "Installing to $dst" |
| 104 | + |
| 105 | +if [ -w "$INSTALL_DIR" ]; then |
| 106 | + cp -f "$found" "$dst" |
| 107 | + chmod +x "$dst" |
| 108 | +else |
| 109 | + echo "Need sudo to write to $INSTALL_DIR" |
| 110 | + sudo cp -f "$found" "$dst" |
| 111 | + sudo chmod +x "$dst" |
| 112 | +fi |
| 113 | + |
| 114 | +if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then |
| 115 | + echo "Warning: $INSTALL_DIR is not in PATH. You may need to add it to run 'usermgmt' directly." |
| 116 | +fi |
| 117 | + |
| 118 | +echo "Verifying installation..." |
| 119 | +if ! "$dst" --version; then |
| 120 | + echo "Verification failed: installed binary did not run successfully." >&2 |
| 121 | + exit 7 |
| 122 | +fi |
| 123 | + |
| 124 | +echo "usermgmt installed successfully from release ${TAG} -> ${dst}" |
| 125 | + |
0 commit comments