#!/bin/sh # Installer for asksh: downloads the latest release binary, verifies its # SHA-256 checksum, runs initial setup, and configures shell integration. set -eu REPO="askshell/asksh" RELEASES_URL="https://github.com/${REPO}/releases" TMP_DIR="" TMP_BIN="" cleanup() { if [ -n "${TMP_BIN}" ] && [ -f "${TMP_BIN}" ]; then rm -f "${TMP_BIN}" fi if [ -n "${TMP_DIR}" ] && [ -d "${TMP_DIR}" ]; then rm -rf "${TMP_DIR}" fi } trap cleanup EXIT INT TERM log() { printf '%s\n' "$*" } warn() { printf 'asksh-install: warning: %s\n' "$*" >&2 } die() { printf 'asksh-install: error: %s\n' "$*" >&2 exit 1 } is_truthy() { case "${1:-}" in ""|0|false|FALSE|no|NO) return 1 ;; *) return 0 ;; esac } require_cmd() { if ! command -v "$1" >/dev/null 2>&1; then die "required command not found: $1" fi } check_prerequisites() { for cmd in curl tar mktemp uname mv chmod mkdir cp awk grep; do require_cmd "${cmd}" done } detect_os() { raw_os=$(uname -s) case "${raw_os}" in Linux) printf 'linux' ;; Darwin) printf 'darwin' ;; MINGW*|MSYS*|CYGWIN*) die "native Windows shells are not supported by this script; download the .zip archive from ${RELEASES_URL}" ;; *) die "unsupported operating system: ${raw_os}" ;; esac } detect_arch() { os_name="$1" raw_arch=$(uname -m) case "${raw_arch}" in x86_64|amd64) if [ "${os_name}" = "darwin" ]; then translated=$(sysctl -n sysctl.proc_translated 2>/dev/null || true) if [ "${translated}" = "1" ]; then printf 'arm64' return fi fi printf 'amd64' ;; arm64|aarch64) printf 'arm64' ;; *) die "unsupported architecture: ${raw_arch}" ;; esac } resolve_version() { if [ -n "${ASKSH_VERSION:-}" ]; then ver="${ASKSH_VERSION#v}" case "${ver}" in ""|*/*|*[[:space:]]*|*..*|*\\*) die "invalid ASKSH_VERSION: ${ASKSH_VERSION}" ;; esac printf '%s' "${ver}" return fi effective_url=$(curl -fsSL -o /dev/null -w '%{url_effective}' \ "${RELEASES_URL}/latest") || die "failed to resolve latest release URL" tag="${effective_url##*/}" case "${tag}" in v?*) printf '%s' "${tag#v}" ;; *) die "could not determine latest release version from ${effective_url}" ;; esac } compute_sha256() { target_file="$1" if command -v sha256sum >/dev/null 2>&1; then sha256sum "${target_file}" | awk '{ print $1 }' elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "${target_file}" | awk '{ print $1 }' else die "neither sha256sum nor shasum is available to verify download integrity" fi } verify_checksum() { archive_path="$1" archive_name="$2" checksums_path="$3" expected=$(awk -v file="${archive_name}" \ '$2 == file { print $1; exit }' "${checksums_path}") if [ -z "${expected}" ]; then die "checksum entry for ${archive_name} not found in checksums.txt" fi actual=$(compute_sha256 "${archive_path}") if [ "${actual}" != "${expected}" ]; then die "checksum mismatch for ${archive_name} (expected ${expected}, got ${actual})" fi } config_file_path() { if [ -n "${ASKSH_CONFIG_DIR:-}" ]; then printf '%s/config.toml' "${ASKSH_CONFIG_DIR%/}" elif [ -n "${XDG_CONFIG_HOME:-}" ]; then printf '%s/asksh/config.toml' "${XDG_CONFIG_HOME%/}" else printf '%s/.config/asksh/config.toml' "${HOME%/}" fi } has_tty() { [ -t 1 ] && (exec /dev/tty) 2>/dev/null } run_setup_wizard() { bin_path="$1" if is_truthy "${ASKSH_NO_SETUP:-}"; then log "Skipping setup (ASKSH_NO_SETUP is set)." return fi cfg_file=$(config_file_path) if [ -f "${cfg_file}" ]; then log "Existing config found at ${cfg_file}; skipping setup." log "Run 'asksh setup' anytime to reconfigure." return fi if ! has_tty; then log "No interactive terminal detected; skipping setup." log "Run '${bin_path} setup' to configure your provider and model." return fi log "Running asksh setup..." if ! ASKSH_FROM_INSTALLER=1 "${bin_path}" setup /dev/null || true) ;; esac if [ -z "${bash_cmd}" ] || [ ! -x "${bash_cmd}" ]; then return fi major=$("${bash_cmd}" -c "printf '%s' \"\${BASH_VERSINFO[0]:-0}\"" \ 2>/dev/null || printf '0') case "${major}" in ''|*[!0-9]*) major=0 ;; esac if [ "${major}" -lt 4 ]; then warn "detected Bash ${major}.x; asksh '?' keybinding requires Bash 4.0+" fi } rc_has_path() { rc_file="$1" install_dir="$2" path_expr="$3" if [ ! -f "${rc_file}" ]; then return 1 fi grep -F -q "${install_dir}" "${rc_file}" \ || grep -F -q "${path_expr}" "${rc_file}" } configure_shell() { os_name="$1" install_dir="$2" if [ "${install_dir}" = "${HOME%/}/.local/bin" ]; then path_expr="\$HOME/.local/bin" else path_expr="${install_dir}" fi if is_truthy "${ASKSH_NO_MODIFY_SHELL:-}"; then log "Skipping shell configuration (ASKSH_NO_MODIFY_SHELL is set)." log "Add ${path_expr} to PATH and add the asksh init hook to your shell rc." return fi user_shell=$(detect_shell) case "${user_shell}" in zsh) rc_file="${ZDOTDIR:-${HOME%/}}/.zshrc" path_line="export PATH=\"${path_expr}:\$PATH\"" init_line="eval \"\$(asksh init zsh)\"" ;; bash) check_bash_version if [ "${os_name}" = "darwin" ]; then if [ -f "${HOME%/}/.bash_profile" ] || [ ! -f "${HOME%/}/.bashrc" ]; then rc_file="${HOME%/}/.bash_profile" else rc_file="${HOME%/}/.bashrc" fi else rc_file="${HOME%/}/.bashrc" fi path_line="export PATH=\"${path_expr}:\$PATH\"" init_line="eval \"\$(asksh init bash)\"" ;; fish) rc_file="${XDG_CONFIG_HOME:-${HOME%/}/.config}/fish/config.fish" path_line="fish_add_path \"${path_expr}\"" init_line='asksh init fish | source' ;; *) log "Shell '${user_shell}' is not auto-configured by this installer." log "Ensure ${path_expr} is in your PATH." log "Supported shell hooks: zsh, bash (4.0+), and fish ('asksh init ')." return ;; esac need_path=0 case ":${PATH:-}:" in *":${install_dir}:"*) ;; *) if ! rc_has_path "${rc_file}" "${install_dir}" "${path_expr}"; then need_path=1 fi ;; esac need_init=0 if [ ! -f "${rc_file}" ] || ! grep -F -q "asksh init " "${rc_file}"; then need_init=1 fi if [ "${need_path}" -eq 1 ] || [ "${need_init}" -eq 1 ]; then mkdir -p "$(dirname "${rc_file}")" if [ -s "${rc_file}" ]; then printf '\n' >> "${rc_file}" fi printf '# asksh\n' >> "${rc_file}" if [ "${need_path}" -eq 1 ]; then printf '%s\n' "${path_line}" >> "${rc_file}" fi if [ "${need_init}" -eq 1 ]; then printf '%s\n' "${init_line}" >> "${rc_file}" fi log "Updated ${rc_file}" else log "Shell integration already configured in ${rc_file}" fi log "" log "Restart your shell or run:" log " source \"${rc_file}\"" log "Press '?' at an empty prompt to get started." } main() { if [ -z "${HOME:-}" ]; then die "HOME is not set" fi check_prerequisites os_name=$(detect_os) arch_name=$(detect_arch "${os_name}") version=$(resolve_version) install_dir="${ASKSH_INSTALL_DIR:-${HOME%/}/.local/bin}" if [ "${install_dir}" != "/" ]; then install_dir="${install_dir%/}" fi archive="asksh_${version}_${os_name}_${arch_name}.tar.gz" base_url="${RELEASES_URL}/download/v${version}" TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/asksh-install.XXXXXX") log "Downloading asksh v${version} (${os_name}/${arch_name})..." curl -fsSL --retry 3 -o "${TMP_DIR}/${archive}" "${base_url}/${archive}" \ || die "failed to download ${base_url}/${archive}" curl -fsSL --retry 3 -o "${TMP_DIR}/checksums.txt" "${base_url}/checksums.txt" \ || die "failed to download ${base_url}/checksums.txt" verify_checksum "${TMP_DIR}/${archive}" "${archive}" "${TMP_DIR}/checksums.txt" tar -xzf "${TMP_DIR}/${archive}" -C "${TMP_DIR}" if [ ! -f "${TMP_DIR}/asksh" ]; then die "archive did not contain asksh binary" fi mkdir -p "${install_dir}" TMP_BIN="${install_dir}/.asksh.tmp.$$" cp "${TMP_DIR}/asksh" "${TMP_BIN}" chmod 0755 "${TMP_BIN}" mv -f "${TMP_BIN}" "${install_dir}/asksh" TMP_BIN="" log "Installed asksh v${version} to ${install_dir}/asksh" run_setup_wizard "${install_dir}/asksh" configure_shell "${os_name}" "${install_dir}" } main "$@"