#!/usr/bin/env bash set -euo pipefail platform="$(uname -ms)" base_url="${RDQ_BASE_URL:-https://rdq.sh}" install_env="RDQ_INSTALL" install_dir="${!install_env:-$HOME/.rdq}" bin_dir="$install_dir/bin" exe="$bin_dir/rdq" if [[ -t 1 ]]; then c_reset=$'\033[0m' c_red=$'\033[0;31m' c_green=$'\033[0;32m' c_dim=$'\033[0;2m' c_bold=$'\033[1m' else c_reset='' c_red='' c_green='' c_dim='' c_bold='' fi error() { echo "${c_red}error${c_reset}: $*" >&2 exit 1 } info() { echo "${c_dim}$*${c_reset}" } success() { echo "${c_green}$*${c_reset}" } info_bold() { echo "${c_bold}$*${c_reset}" } command -v curl >/dev/null 2>&1 || error "curl is required" command -v tar >/dev/null 2>&1 || error "tar is required" if [[ $# -gt 1 ]]; then error "too many arguments; pass at most one version label" fi case "$platform" in "Darwin arm64") target="darwin-aarch64" ;; "Darwin x86_64") target="darwin-x64" if [[ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" == "1" ]]; then target="darwin-aarch64" info "Rosetta shell detected, preferring $target" fi ;; "Linux aarch64"|"Linux arm64") target="linux-aarch64" ;; "Linux x86_64") target="linux-x64" ;; *) error "unsupported platform: $platform" ;; esac archive_version="${1:-}" if [[ -n "$archive_version" ]]; then archive_url="$base_url/dist/$archive_version/rdq-$target.tar.gz" else archive_url="$base_url/dist/rdq-$target.tar.gz" fi tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/rdq-install.XXXXXX")" trap 'rm -rf "$tmp_dir"' EXIT mkdir -p "$bin_dir" if ! curl --fail --location --progress-bar --output "$tmp_dir/rdq.tar.gz" "$archive_url"; then if command -v nix >/dev/null 2>&1; then info "prebuilt bundle unavailable, falling back to nix profile install" nix profile install "github:conradev/rdq#rdq-cli" success "rdq installed via nix profile" exit 0 fi error "failed to download $archive_url" fi tar -xzf "$tmp_dir/rdq.tar.gz" -C "$tmp_dir" mv "$tmp_dir/rdq-$target/rdq" "$exe" chmod +x "$exe" tildify() { if [[ "$1" == "$HOME"/* ]]; then printf '%s/%s\n' "~" "${1#"$HOME"/}" else echo "$1" fi } success "rdq was installed to ${c_bold}$(tildify "$exe")${c_reset}" if command -v rdq >/dev/null 2>&1; then echo "Run 'rdq --help' to get started" exit 0 fi shell_name="$(basename "${SHELL:-sh}")" quoted_install_dir="\"${install_dir//\"/\\\"}\"" if [[ "$quoted_install_dir" == "\"$HOME/"* ]]; then quoted_install_dir="${quoted_install_dir/#\"$HOME\//\"\$HOME/}" fi tilde_bin_dir="$(tildify "$bin_dir")" echo case "$shell_name" in fish) config_file="$HOME/.config/fish/config.fish" commands=( "set --export RDQ_INSTALL $quoted_install_dir" "set --export PATH \$RDQ_INSTALL/bin \$PATH" ) ;; zsh) config_file="$HOME/.zshrc" commands=( "export RDQ_INSTALL=$quoted_install_dir" "export PATH=\"\$RDQ_INSTALL/bin:\$PATH\"" ) ;; *) config_file="$HOME/.bashrc" commands=( "export RDQ_INSTALL=$quoted_install_dir" "export PATH=\"\$RDQ_INSTALL/bin:\$PATH\"" ) ;; esac if [[ -w "$config_file" || ! -e "$config_file" ]]; then mkdir -p "$(dirname "$config_file")" { echo echo "# rdq" for command in "${commands[@]}"; do echo "$command" done } >> "$config_file" info "Added $tilde_bin_dir to PATH in $(tildify "$config_file")" else echo "Add $tilde_bin_dir to PATH manually:" for command in "${commands[@]}"; do info_bold " $command" done fi echo "Run 'rdq --help' to get started"