#!/bin/bash set -e CLI_BIN="Hush.Cli/bin/Release/net10.0/linux-x64/publish/Hush.Cli" INSTALL_PREFIX="${INSTALL_PREFIX:-/usr}" BIN_DIR="$INSTALL_PREFIX/bin" COMPLETION_DIR="$INSTALL_PREFIX/share/zsh/site-functions" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- info() { echo "[install] $*"; } success() { echo "[install] $*"; } warn() { echo "[install] WARNING: $*" >&2; } die() { echo "[install] ERROR: $*" >&2; exit 1; } require_root() { if [[ $EUID -ne 0 ]]; then die "This script must be run as root (use sudo)." fi } detect_init() { if command -v systemctl &>/dev/null && systemctl --version &>/dev/null 2>&1; then echo "systemd" elif [[ -f /sbin/openrc-run ]] || [[ -f /usr/sbin/openrc-run ]] || command -v rc-service &>/dev/null; then echo "openrc" else echo "none" fi } # --------------------------------------------------------------------------- # Build # --------------------------------------------------------------------------- build() { info "Building Hush with AOT compilation..." # If running as root (via sudo), build as the real user so that the # output artifacts are not owned by root. if [[ $EUID -eq 0 && -n "$SUDO_USER" ]]; then sudo -u "$SUDO_USER" bash build.sh else bash build.sh fi } # --------------------------------------------------------------------------- # Install binary # --------------------------------------------------------------------------- install_binary() { [[ -f "$CLI_BIN" ]] || die "Binary not found: $CLI_BIN — run build.sh first or let install.sh build it." info "Installing binary to $BIN_DIR/hush..." install -Dm755 "$CLI_BIN" "$BIN_DIR/hush" success "Installed: $BIN_DIR/hush" } # --------------------------------------------------------------------------- # Zsh completion # --------------------------------------------------------------------------- install_completion() { info "Installing zsh completion to $COMPLETION_DIR/_hush..." install -Dm644 completions/_hush "$COMPLETION_DIR/_hush" success "Zsh completion installed." info " Add the following to your .zshrc if not already present:" info " fpath=($COMPLETION_DIR \$fpath)" info " autoload -Uz compinit && compinit" } # --------------------------------------------------------------------------- # systemd user service # --------------------------------------------------------------------------- install_systemd() { info "Installing systemd user service..." local run_user="${SUDO_USER:-$USER}" local user_home uid service_dir service_file user_home=$(getent passwd "$run_user" | cut -d: -f6) uid=$(id -u "$run_user") service_dir="$user_home/.config/systemd/user" service_file="$service_dir/hush.service" mkdir -p "$service_dir" chown "$run_user:" "$service_dir" cat > "$service_file" < "$rc_file" < "$rc_conf" <