#!/usr/bin/env bash # miaur one-click installer # Usage: # ./install.sh # ./install.sh --user # ./install.sh --uninstall # bash <(curl -fsSL https://raw.githubusercontent.com//miaur/main/install.sh) set -euo pipefail # Override when forking: MIAUR_REPO=https://github.com/you/miaur.git ./install.sh REPO_URL="${MIAUR_REPO:-https://github.com/WHC2006/miaur.git}" BRANCH="${MIAUR_BRANCH:-main}" USER_INSTALL=0 UNINSTALL=0 for arg in "$@"; do case "$arg" in --user) USER_INSTALL=1 ;; --uninstall) UNINSTALL=1 ;; -h | --help) cat <<'EOF' Usage: install.sh [--user] [--uninstall] (default) Install system-wide to /usr/local (needs sudo) --user Install to ~/.local (no sudo for the link) --uninstall Remove installed miaur EOF exit 0 ;; *) echo "Unknown option: $arg" >&2 exit 1 ;; esac done if [[ $USER_INSTALL -eq 1 ]]; then PREFIX="${HOME}/.local" SUDO= else PREFIX=/usr/local SUDO=sudo fi SHARE_DIR="${PREFIX}/share/miaur" BIN_LINK="${PREFIX}/bin/miaur" die() { echo "error: $*" >&2; exit 1; } need_root_ok() { if [[ $USER_INSTALL -eq 0 && ${EUID:-$(id -u)} -ne 0 ]]; then command -v sudo >/dev/null 2>&1 || die "sudo is required" sudo -v || die "sudo authentication failed" fi } uninstall() { need_root_ok echo "==> Uninstalling miaur" $SUDO rm -f "$BIN_LINK" $SUDO rm -rf "$SHARE_DIR" echo "Done: removed $BIN_LINK and $SHARE_DIR" } install_deps() { command -v pacman >/dev/null 2>&1 || die "Arch Linux is required (pacman not found)" echo "==> Installing dependencies" if [[ ${EUID:-$(id -u)} -eq 0 ]]; then pacman -S --needed --noconfirm base-devel git curl jq else sudo pacman -S --needed --noconfirm base-devel git curl jq fi } # Resolve source tree: current repo, or clone to temp resolve_src() { local here here=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd) if [[ -f $here/miaur && -d $here/lib ]]; then SRC=$here return 0 fi echo "==> Cloning ${REPO_URL} (${BRANCH})" TMP=$(mktemp -d) trap 'rm -rf "${TMP:-}"' EXIT git clone --depth 1 --branch "$BRANCH" "$REPO_URL" "$TMP/miaur" \ || die "clone failed; set MIAUR_REPO to the repository URL" SRC=$TMP/miaur } do_install() { need_root_ok install_deps resolve_src [[ -f $SRC/miaur && -d $SRC/lib ]] || die "incomplete source tree: $SRC" echo "==> Installing to ${SHARE_DIR}" $SUDO mkdir -p "$SHARE_DIR" "${PREFIX}/bin" $SUDO rm -rf "$SHARE_DIR" $SUDO mkdir -p "$SHARE_DIR" $SUDO cp -a "$SRC/miaur" "$SRC/lib" "$SHARE_DIR/" if [[ -d $SRC/completions ]]; then $SUDO cp -a "$SRC/completions" "$SHARE_DIR/" fi if [[ -d $SRC/config ]]; then $SUDO cp -a "$SRC/config" "$SHARE_DIR/" fi $SUDO chmod 755 "$SHARE_DIR/miaur" $SUDO ln -sfn "$SHARE_DIR/miaur" "$BIN_LINK" # Optional bash completion (system or user) if [[ -d $SRC/completions ]]; then if [[ $USER_INSTALL -eq 1 ]]; then mkdir -p "${HOME}/.local/share/bash-completion/completions" cp -f "$SRC/completions/miaur.bash" \ "${HOME}/.local/share/bash-completion/completions/miaur" 2>/dev/null || true else if [[ -d /usr/share/bash-completion/completions ]]; then $SUDO cp -f "$SRC/completions/miaur.bash" \ /usr/share/bash-completion/completions/miaur 2>/dev/null || true fi fi fi echo echo "Done: $(command -v miaur 2>/dev/null || echo "$BIN_LINK")" "$BIN_LINK" -V echo echo "Try: miaur -Ss foo" } if [[ $UNINSTALL -eq 1 ]]; then uninstall else do_install fi