#!/usr/bin/env bash # Klaxon bootstrap: private pinned PHP CLI runtime, without APT or system PHP changes. set -euo pipefail umask 077 fail() { printf 'Klaxon installation failed: %s\n' "$*" >&2; exit 1; } if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then printf '%s\n' 'Klaxon Bash installer' \ 'Fresh: curl -fsSL https://distro.parallax.link/install-klaxon.sh | bash' \ 'Upgrade: curl -fsSL https://distro.parallax.link/install-klaxon.sh | bash -s -- --upgrade' \ 'Options: --upgrade --install-dir=/absolute/path --bin-dir=/absolute/path --filename=klaxon' exit 0 fi [[ "$(uname -s)" == Linux ]] || fail 'Linux is required.' for tool in curl sha256sum mkdir mktemp chmod mv rm rmdir stat id; do command -v "$tool" >/dev/null 2>&1 || fail "Required command missing: $tool" done [[ -n "${HOME:-}" && "$HOME" == /* && -d "$HOME" && ! -L "$HOME" ]] || fail 'HOME must be an absolute, real directory.' case "$(uname -m)" in x86_64|amd64) arch=x86_64 runtime_sha=5c8b81c1d093fbc188f4ee09ed9bde6e5ef4f40c9a22b453fb60eeaf8b161767 runtime_bytes=174719968 ;; aarch64|arm64) arch=aarch64 runtime_sha=9cb3a31d10c7e8bf2448d599937bda889bdff8d3934011ee1b6a40e4fab5c5a5 runtime_bytes=168138896 ;; *) fail 'Supported Linux architectures: x86_64 and aarch64.' ;; esac runtime_url="https://github.com/php/frankenphp/releases/download/v1.13.1/frankenphp-linux-$arch" runtime_dir="$HOME/.local/share/parallax/runtimes/frankenphp-1.13.1-$arch" runtime="$runtime_dir/frankenphp" runtime_tmp= bootstrap_dir= cleanup() { if [[ -n "$runtime_tmp" && -f "$runtime_tmp" ]]; then rm -f -- "$runtime_tmp"; fi if [[ -n "$bootstrap_dir" && -d "$bootstrap_dir" ]]; then rm -f -- "$bootstrap_dir/installer.php" rmdir -- "$bootstrap_dir" 2>/dev/null || true fi } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM for directory in "$HOME/.local" "$HOME/.local/share" "$HOME/.local/share/parallax" \ "$HOME/.local/share/parallax/runtimes" "$runtime_dir"; do [[ ! -L "$directory" ]] || fail "Refusing a symlink installation directory: $directory" [[ ! -e "$directory" || -d "$directory" ]] || fail "Not a directory: $directory" mkdir -p -- "$directory" [[ "$(stat -c %u "$directory")" == "$(id -u)" ]] || fail "Installation directory is owned by another account: $directory" [[ $(( 8#$(stat -c %a "$directory") & 8#022 )) == 0 ]] || fail "Installation directory is writable by another account: $directory" done chmod 700 -- "$runtime_dir" verify() { local file=$1 expected=$2 [[ -f "$file" && ! -L "$file" ]] || fail "Not a regular release file: $file" printf '%s %s\n' "$expected" "$file" | sha256sum -c - >/dev/null || fail 'A downloaded file failed SHA-256 verification.' } download() { local url=$1 output=$2 limit=$3 curl --fail --silent --show-error --location --retry 2 --retry-delay 1 \ --connect-timeout 15 --max-time 300 --proto '=https' --proto-redir '=https' \ --max-filesize "$limit" --output "$output" "$url" || fail 'HTTPS download failed; no package repository was changed.' } if [[ -e "$runtime" || -L "$runtime" ]]; then verify "$runtime" "$runtime_sha" else printf '%s\n' 'Downloading the private CLI runtime (one-time download, approximately 175 MB)...' >&2 runtime_tmp=$(mktemp "$runtime_dir/.download.XXXXXXXX") download "$runtime_url" "$runtime_tmp" "$runtime_bytes" [[ "$(stat -c %s "$runtime_tmp")" == "$runtime_bytes" ]] || fail 'The runtime download is incomplete.' verify "$runtime_tmp" "$runtime_sha" chmod 700 -- "$runtime_tmp" mv -n -- "$runtime_tmp" "$runtime" verify "$runtime" "$runtime_sha" fi chmod 700 -- "$runtime" bootstrap_dir=$(mktemp -d "$runtime_dir/.installer.XXXXXXXX") download 'https://distro.parallax.link/downloads/klaxon-cli/0.1.3/installer.php' "$bootstrap_dir/installer.php" 131072 verify "$bootstrap_dir/installer.php" 'd89ae0146ff1f411c5587609dc96299bf2dd1f704069e0749d9c14a5ce382337' export PHP_INI_SCAN_DIR= export KLAXON_INSTALL_PHP_MODE=frankenphp export KLAXON_INSTALL_PHP_BINARY="$runtime" if [[ -f /etc/ssl/certs/ca-certificates.crt ]]; then export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt fi "$runtime" php-cli -n "$bootstrap_dir/installer.php" "$@"