#!/usr/bin/env bash set -euo pipefail VERSION="${RALPH_SKILLS_VERSION:-}" SENTINEL_PREFIX="__RALPH" SENTINEL_SUFFIX="_SKILLS_GITHUB_REPO__" REPO_SENTINEL="${SENTINEL_PREFIX}${SENTINEL_SUFFIX}" DEFAULT_GITHUB_REPO="1450Digital/ralph-skills" GITHUB_REPO="${RALPH_SKILLS_GITHUB_REPO:-$DEFAULT_GITHUB_REPO}" INSTALL_DIR="${RALPH_SKILLS_INSTALL_DIR:-}" BINARY_NAME="ralph-skills" say() { printf '%s\n' "$*" } fail() { say "ralph-skills install failed: $*" >&2 exit 1 } need_cmd() { command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1" } download_file() { local url output url="$1" output="$2" say "Downloading ${url}" if [ -t 2 ]; then curl -fL# "$url" -o "$output" else curl -fsSL "$url" -o "$output" fi } resolve_version() { if [ -n "$VERSION" ]; then return fi } detect_platform() { local os arch os="$(uname -s)" arch="$(uname -m)" case "$os" in Darwin) os="macos" ;; Linux) os="linux" ;; MINGW*|MSYS*|CYGWIN*|Windows_NT) os="windows" ;; *) fail "unsupported operating system: $os" ;; esac case "$arch" in arm64|aarch64) arch="arm64" ;; x86_64|amd64) arch="x64" ;; *) fail "unsupported architecture: $arch" ;; esac PLATFORM="${os}" ARCH="${arch}" } select_artifact() { case "${PLATFORM}-${ARCH}" in macos-arm64) ARTIFACT="ralph-skills-macos-arm64" ;; macos-x64) ARTIFACT="ralph-skills-macos-x64" ;; linux-x64) ARTIFACT="ralph-skills-linux-x64" ;; linux-arm64) ARTIFACT="ralph-skills-linux-arm64" ;; windows-x64) ARTIFACT="ralph-skills-windows-x64.exe" ;; *) fail "no release artifact for ${PLATFORM}-${ARCH}" ;; esac } choose_install_dir() { if [ -n "$INSTALL_DIR" ]; then return fi if [ -w "/usr/local/bin" ]; then INSTALL_DIR="/usr/local/bin" else INSTALL_DIR="${HOME}/.local/bin" fi } install_binary() { local url tmp_file target_file if [ -z "$GITHUB_REPO" ] || [ "$GITHUB_REPO" = "$REPO_SENTINEL" ]; then fail "RALPH_SKILLS_GITHUB_REPO is not set. Use the hosted installer or set it to owner/repo before running the installer." fi if [ -n "$VERSION" ]; then url="https://github.com/${GITHUB_REPO}/releases/download/v${VERSION}/${ARTIFACT}" else url="https://github.com/${GITHUB_REPO}/releases/latest/download/${ARTIFACT}" fi tmp_file="$(mktemp "${TMPDIR:-/tmp}/ralph-skills.XXXXXX")" target_file="${INSTALL_DIR}/${BINARY_NAME}" mkdir -p "$INSTALL_DIR" download_file "$url" "$tmp_file" chmod +x "$tmp_file" mv "$tmp_file" "$target_file" say "Installed ${BINARY_NAME} to ${target_file}" case ":$PATH:" in *":${INSTALL_DIR}:"*) ;; *) say "" say "Add ${INSTALL_DIR} to your PATH to run ${BINARY_NAME}:" say " export PATH=\"${INSTALL_DIR}:\$PATH\"" ;; esac } main() { need_cmd curl resolve_version detect_platform select_artifact choose_install_dir install_binary } main "$@"