Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
70 lines
2.3 KiB
Bash
Executable File
70 lines
2.3 KiB
Bash
Executable File
#!/bin/sh
|
|
# Install zag from a release and (optionally) register it as a service.
|
|
#
|
|
# curl -fsSL https://gitea.samson.media/samson-media/zag/raw/branch/main/install.sh | sh
|
|
#
|
|
# Environment:
|
|
# ZAG_VERSION release tag to install (default: latest)
|
|
# ZAG_TOKEN Gitea access token (the repository is private)
|
|
# ZAG_PREFIX install directory (default: /usr/local/bin as root, ~/.local/bin otherwise)
|
|
# ZAG_NO_SERVICE=1 skip `zag service install`
|
|
set -eu
|
|
|
|
BASE="https://gitea.samson.media"
|
|
REPO="samson-media/zag"
|
|
|
|
info() { printf '==> %s\n' "$*"; }
|
|
die() { printf 'error: %s\n' "$*" >&2; exit 1; }
|
|
|
|
case "$(uname -s)" in
|
|
Linux) os=linux ;;
|
|
Darwin) os=macos ;;
|
|
*) die "unsupported OS: $(uname -s) (Windows is not supported yet)" ;;
|
|
esac
|
|
case "$(uname -m)" in
|
|
x86_64 | amd64) arch=x64 ;;
|
|
aarch64 | arm64) arch=arm64 ;;
|
|
*) die "unsupported architecture: $(uname -m)" ;;
|
|
esac
|
|
|
|
auth=""
|
|
[ -n "${ZAG_TOKEN:-}" ] && auth="Authorization: token $ZAG_TOKEN"
|
|
fetch() { # fetch <url> <output>
|
|
if [ -n "$auth" ]; then curl -fsSL -H "$auth" -o "$2" "$1"; else curl -fsSL -o "$2" "$1"; fi
|
|
}
|
|
|
|
version="${ZAG_VERSION:-}"
|
|
if [ -z "$version" ]; then
|
|
info "Looking up the latest release"
|
|
tmp_json=$(mktemp)
|
|
fetch "$BASE/api/v1/repos/$REPO/releases/latest" "$tmp_json" || die "could not query releases (is ZAG_TOKEN set?)"
|
|
version=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$tmp_json" | head -1)
|
|
rm -f "$tmp_json"
|
|
[ -n "$version" ] || die "could not determine the latest version"
|
|
fi
|
|
|
|
if [ -n "${ZAG_PREFIX:-}" ]; then
|
|
prefix=$ZAG_PREFIX
|
|
elif [ "$(id -u)" -eq 0 ]; then
|
|
prefix=/usr/local/bin
|
|
else
|
|
prefix="$HOME/.local/bin"
|
|
fi
|
|
mkdir -p "$prefix"
|
|
|
|
asset="zag-$os-$arch"
|
|
tmp=$(mktemp)
|
|
info "Downloading $asset $version"
|
|
fetch "$BASE/$REPO/releases/download/$version/$asset" "$tmp" || die "download failed"
|
|
chmod +x "$tmp"
|
|
"$tmp" --version >/dev/null 2>&1 || die "downloaded binary does not run on this machine"
|
|
mv "$tmp" "$prefix/zag"
|
|
info "Installed $("$prefix/zag" --version) to $prefix/zag"
|
|
|
|
case ":$PATH:" in *":$prefix:"*) ;; *) info "note: $prefix is not on your PATH" ;; esac
|
|
|
|
if [ -z "${ZAG_NO_SERVICE:-}" ]; then
|
|
info "Registering the service"
|
|
"$prefix/zag" service install --binary "$prefix/zag" || info "service install failed; run 'zag service install' manually"
|
|
fi
|