Files
2026-05-14 02:27:26 +01:00

96 lines
2.0 KiB
Bash

#!/bin/sh
set -eu
CONFIG_SRC="${CONFIG_SRC:-}"
RELEASE_BASE_URL="${RELEASE_BASE_URL:-https://github.com/klipperanywhere/klipperanywhere/releases/latest/download}"
ARCH_OVERRIDE="${ARCH_OVERRIDE:-}"
usage() {
cat >&2 <<'EOF'
usage: bootstrap-agent.sh --config <agent.json> [--release-base-url <url>] [--arch <arch>]
Downloads the matching agent binary for this host and installs it using the
unified installer. Supported arches: amd64, arm64, armv7, mipsle.
EOF
exit 2
}
while [ $# -gt 0 ]; do
case "$1" in
--config)
shift
CONFIG_SRC="${1:-}"
;;
--release-base-url)
shift
RELEASE_BASE_URL="${1:-}"
;;
--arch)
shift
ARCH_OVERRIDE="${1:-}"
;;
-h|--help)
usage
;;
*)
echo "unknown argument: $1" >&2
usage
;;
esac
shift
done
if [ -z "$CONFIG_SRC" ]; then
echo "missing --config" >&2
usage
fi
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
if [ ! -f "$CONFIG_SRC" ]; then
echo "missing config file: $CONFIG_SRC" >&2
exit 1
fi
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
ARCH="${ARCH_OVERRIDE:-$(detect_arch)}"
ASSET="klipperanywhere-agent-linux-$ARCH"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
download "$RELEASE_BASE_URL/$ASSET" "$TMP_DIR/$ASSET"
chmod 0755 "$TMP_DIR/$ASSET"
exec sh "$SCRIPT_DIR/install-agent.sh" "$TMP_DIR/$ASSET" "$CONFIG_SRC"
detect_arch() {
machine="$(uname -m 2>/dev/null || echo unknown)"
case "$machine" in
x86_64|amd64) echo amd64 ;;
aarch64|arm64) echo arm64 ;;
armv7l|armv7*) echo armv7 ;;
mips64el|mipsle) echo mipsle ;;
mipsel|mips*) echo mipsle ;;
*)
echo "unsupported architecture: $machine" >&2
exit 1
;;
esac
}
download() {
url="$1"
dest="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$dest"
return
fi
if command -v wget >/dev/null 2>&1; then
wget -qO "$dest" "$url"
return
fi
echo "need curl or wget" >&2
exit 1
}