#!/usr/bin/env bash # # AppInstall — one-command installer (stable releases, native pacman, APT and Fedora DNF packages). # # Public command: # curl -fsSL https://raw.githubusercontent.com/aboodmohd/-AppInstall/main/install.sh | bash # # What this script does: # 1. Detects distribution family, architecture and desktop environment. # 2. Resolves the latest GitHub *stable* release (drafts and prereleases are # never used). # 3. Downloads the package and its SHA256SUMS manifest and verifies the # checksum before anything else happens. # 4. Installs with the native package manager (pacman, APT or Fedora DNF). The script runs as # the invoking user; only package-manager commands are elevated, with their # stdin handed back to the terminal so `curl | bash` keeps working. # 5. Offers to install missing optional backends via pacman. # 6. Offers ONE clear confirmation to make AppInstall the default handler # for every MIME type advertised in appinstall.desktop. Previous per-user # defaults are backed up first and a restore script is written. # # What this script NEVER does: # - never executes AppInstall itself, and never runs it as root; # - never auto-creates Distrobox containers or runs third-party packages; # - never touches system-wide MIME configuration (per-user only); # - never silently replaces an existing installation without asking; # - never installs an Arch package with another distro's package manager; # - never pipes remote content into sudo. # # Exit codes: 0 ok, 1 generic error, 2 unsupported environment or refused # conflict, 3 checksum verification failure, 4 network/API failure, # 5 package installation failure, 130 interrupted. # Propagate strictness when sourced into a test harness too. Function bodies # assume errexit/unset-nounset exist (this file is bash >= 4 only). set -euo pipefail REPO_SLUG="aboodmohd/-AppInstall" API_BASE="https://api.github.com" RAW_URL="https://raw.githubusercontent.com/${REPO_SLUG}/main/install.sh" DESKTOP_ID="appinstall.desktop" APP_BIN="/usr/bin/appinstall" APP_LOCAL_BIN="$HOME/.local/bin/appinstall" # Every MIME type advertised in appinstall.desktop (handlers + aliases). MIME_TYPES=( "application/vnd.debian.binary-package" "application/x-deb" "application/x-rpm" "application/vnd.flatpak" "application/vnd.flatpak.ref" "application/vnd.flatpak.repo" "application/vnd.appimage" "application/x-iso9660-appimage" "application/x-quickdrop-appimage" ) # Distribution IDs enabled without extra consent (tested systems only). SUPPORTED_IDS="arch cachyos fedora" # Optional backends offered after core installation (pacman package names). OPTIONAL_PKGS=(flatpak podman distrobox squashfs-tools fuse2 kdialog zenity libnotify) INSTALLER_VERSION="1.8.0" # --------------------------------------------------------------------------- # Flags / state. Defaults respect pre-existing environment values so the test # harness (and CI) can inject flags via env vars; parse_flags() overrides them # from real command-line arguments. # --------------------------------------------------------------------------- DRY_RUN="${DRY_RUN:-0}" ASSUME_YES="${ASSUME_YES:-0}" SET_DEFAULTS="${SET_DEFAULTS:-0}" NO_DEFAULTS="${NO_DEFAULTS:-0}" WITH_OPTIONAL="${WITH_OPTIONAL:-0}" NO_OPTIONAL="${NO_OPTIONAL:-0}" ALLOW_ARCH_LIKE="${ALLOW_ARCH_LIKE:-0}" REMOVE_SOURCE_INSTALL="${REMOVE_SOURCE_INSTALL:-0}" UNINSTALL="${UNINSTALL:-0}" # Test hooks (only used by tests/test_install_sh.py and CI; undocumented on # purpose so they don't become false compatibility claims in real usage). TEST_OS_RELEASE="${APPINSTALL_TEST_OS_RELEASE:-}" TEST_RELEASES_JSON="${APPINSTALL_TEST_RELEASES_JSON:-}" STATE_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/appinstall/installer" LOG_FILE="" TMP_DIR="" TTY_OK=0 DESKTOP_CHECK=0 DESKTOP_UPDATE="" # --------------------------------------------------------------------------- # Output helpers # --------------------------------------------------------------------------- info() { printf '==> %s\n' "$*"; } warn() { printf 'WARN: %s\n' "$*" >&2; } bad() { printf 'ERROR: %s\n' "$*" >&2; } die() { local code="$1"; shift; bad "$*"; exit "$code"; } log_write() { if [[ $DRY_RUN -eq 0 && -n "$LOG_FILE" ]]; then printf '%s %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$*" >>"$LOG_FILE" 2>/dev/null || true fi } cleanup() { [[ -n "$TMP_DIR" && -d "$TMP_DIR" ]] && rm -rf -- "$TMP_DIR" || true; } trap cleanup EXIT trap 'bad "Interrupted."; exit 130' INT TERM # --------------------------------------------------------------------------- # Basic tool checks # --------------------------------------------------------------------------- have_cmd() { command -v "$1" >/dev/null 2>&1; } need_cmds() { local missing=() c for c in "$@"; do have_cmd "$c" || missing+=("$c") done if ((${#missing[@]})); then bad "Missing required tools: ${missing[*]}" return 2 fi return 0 } # --------------------------------------------------------------------------- # Distribution detection (pure bash against os-release) # --------------------------------------------------------------------------- os_release_file() { printf '%s\n' "${TEST_OS_RELEASE:-/etc/os-release}"; } read_os_release() { # -> "id|id_like" local file id="" id_like="" file="$(os_release_file)" [[ -r $file ]] || return 1 local line while IFS= read -r line; do case $line in ID=\'*|ID=*) id="${line#ID=}"; id="${id#\'}"; id="${id%\'}"; id="${id#\"}"; id="${id%\"}" ;; ID_LIKE=\'*|ID_LIKE=*) id_like="${line#ID_LIKE=}"; id_like="${id_like#\'}"; id_like="${id_like%\'}"; id_like="${id_like#\"}"; id_like="${id_like%\"}" ;; esac done <"$file" [[ -n "$id" ]] || return 1 printf '%s|%s\n' "$id" "$id_like" } # Group distributions by packaging ecosystem. detect_family() { local fields id rest if ! fields="$(read_os_release)"; then printf 'unknown\n'; return 0; fi id="${fields%%|*}" rest="${fields#*|}" case "$id" in arch|cachyos|endeavouros|manjaro|artix|garuda) printf 'arch\n'; return 0 ;; esac if [[ "$rest" == *arch* ]]; then printf 'arch\n'; return 0; fi case "$id" in debian|ubuntu|linuxmint|pop|elementary|zorin|kali|neon|mx) printf 'debian\n'; return 0 ;; fedora|rhel|centos|rocky|alma|nobara|bazzite) printf 'fedora\n'; return 0 ;; opensuse*|sles) printf 'opensuse\n'; return 0 ;; esac if [[ "$rest" == *debian* || "$rest" == *ubuntu* ]]; then printf 'debian\n'; return 0; fi if [[ "$rest" == *fedora* || "$rest" == *rhel* || "$rest" == *suse* ]]; then printf 'fedora\n'; return 0 fi printf 'unknown\n' } detect_distro_id() { local fields if ! fields="$(read_os_release)"; then printf 'unknown\n'; return 0; fi printf '%s\n' "${fields%%|*}" } # Treat os-release as inert data, never source or evaluate it as shell code. detect_distro_version() { local file="${TEST_OS_RELEASE:-/etc/os-release}" line value="" if [[ -r "$file" ]]; then while IFS= read -r line || [[ -n "$line" ]]; do case "$line" in VERSION_ID=*) value="${line#VERSION_ID=}" value="${value#\"}"; value="${value%\"}" value="${value#\'}"; value="${value%\'}" break ;; esac done <"$file" fi printf '%s\n' "$value" } detect_arch() { uname -m; } detect_de() { local de="${XDG_CURRENT_DESKTOP:-}" case "$de" in *KDE*|*Plasma*) printf 'kde\n' ;; *GNOME*) printf 'gnome\n' ;; *XFCE*) printf 'xfce\n' ;; "") printf 'unknown\n' ;; *) printf '%s\n' "$(printf '%s' "$de" | tr 'A-Z:' 'a-z-')" ;; esac } # --------------------------------------------------------------------------- # Interactive prompts (never read stdin — always /dev/tty) # --------------------------------------------------------------------------- prepare_tty() { if [[ -r /dev/tty ]] && ( : /dev/null; then TTY_OK=1 TTY_IN="/dev/tty" TTY_OUT="/dev/tty" else TTY_OK=0 TTY_IN="" TTY_OUT="" fi } tty_read_line() { # prompt -> prints answer local prompt="$1" line="" printf '%s' "$prompt" >"$TTY_OUT" 2>/dev/null || return 1 IFS= read -r line <"$TTY_IN" || return 1 printf '%s\n' "$line" } # ask "question" default(y|n): returns 0 for yes, 1 for no. ask() { if [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] would ask: %s\n' "$1" return 1 fi if [[ $ASSUME_YES -eq 1 ]]; then printf '%s [y] (auto-yes)\n' "$1" return 0 fi if [[ $TTY_OK -eq 0 ]]; then printf 'noninteractive: action requires consent: %s\n' "$1" >&2 return 1 fi local answer if [[ "$2" == "y" ]]; then answer="$(tty_read_line "$1 [Y/n] ")" || answer="n" else answer="$(tty_read_line "$1 [y/N] ")" || answer="n" fi case "$answer" in [Yy]|[Yy][Ee][Ss]) return 0 ;; "") [[ "$2" == "y" ]] && return 0; return 1 ;; *) return 1 ;; esac } # --------------------------------------------------------------------------- # Privileged execution (least privilege; stdin handback to terminal) # --------------------------------------------------------------------------- run_root() { if [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] (root) %s\n' "$*" return 0 fi if ((EUID == 0)); then "$@" elif [[ -n "$DESKTOP_UPDATE" ]]; then local executable case "$1" in pacman|apt-get|dnf) executable="/usr/bin/$1" ;; *) return 2 ;; esac shift /usr/bin/pkexec --disable-internal-agent "$executable" "$@" elif have_cmd sudo; then if [[ $TTY_OK -eq 1 ]]; then sudo -- "$@" <"$TTY_IN" elif sudo -n true >/dev/null 2>&1; then # Non-interactive context where passwordless (NOPASSWD) sudo is # configured (CI containers). Never blocks on a password prompt. sudo -n -- "$@" else bad "No interactive terminal for the sudo password prompt, and passwordless sudo is not configured." bad "Run this installer from a real terminal, or configure NOPASSWD sudo for automation." return 2 fi else bad "Neither root (EUID=0) nor sudo was available; cannot run: $*" return 2 fi } # --------------------------------------------------------------------------- # Release discovery (GitHub stable releases only) # --------------------------------------------------------------------------- api_get() { # path -> stdout local token="${GITHUB_TOKEN:-${GH_TOKEN:-}}" if [[ -n "$TEST_RELEASES_JSON" ]]; then cat -- "$TEST_RELEASES_JSON" return 0 fi if [[ -n "$token" ]]; then # GitHub token formats may include punctuation. Reject header injection, # and pass the header over stdin so it never appears in curl arguments. [[ "$token" != *$'\n'* && "$token" != *$'\r'* ]] || return 4 printf 'Authorization: Bearer %s\n' "$token" | curl -q -fsSL --proto '=https' --proto-redir '=https' --connect-timeout 15 --max-time 120 -H @- -H "Accept: application/vnd.github+json" "${API_BASE}$1" else curl -q -fsSL --proto "=https" --proto-redir "=https" --connect-timeout 15 --max-time 120 -H "Accept: application/vnd.github+json" "${API_BASE}$1" fi } # resolve_release -> "tag|pkg_url|sums_url|pkg_name" resolve_release() { local json local family="${1:-arch}" if [[ $DESKTOP_CHECK -eq 1 || -n "$DESKTOP_UPDATE" ]]; then json="$(curl -q -fsS --proto '=https' --connect-timeout 8 --max-time 20 \ "https://api.github.com/repos/${REPO_SLUG}/releases/latest")" || return 4 else json="$(api_get "/repos/${REPO_SLUG}/releases/latest")" || return 4 fi python3 -c ' import json, re, sys from urllib.parse import urlparse try: data = json.load(sys.stdin) tag = data["tag_name"] if data.get("draft") is not False or data.get("prerelease") is not False: raise ValueError("not a stable release") if not re.fullmatch(r"v[0-9][A-Za-z0-9.+_-]*", tag): raise ValueError("invalid tag") package_pattern = { "arch": r"appinstall-[0-9][A-Za-z0-9.+_-]*-any\.pkg\.tar\.zst", "debian": r"appinstall_[0-9][A-Za-z0-9.+~-]*_all\.deb", "fedora": r"appinstall-[0-9][A-Za-z0-9.+_~-]*-[0-9][A-Za-z0-9._+~]*\.noarch\.rpm", }[sys.argv[2]] found = {} for asset in data["assets"]: name, url = asset["name"], asset["browser_download_url"] if name == "SHA256SUMS" or re.fullmatch(package_pattern, name): expected = "https://github.com/aboodmohd/-AppInstall/releases/download/" + tag + "/" + name local_fixture = bool(sys.argv[1]) and urlparse(url).scheme == "file" and url.endswith("/" + name) if url != expected and not local_fixture: raise ValueError("unexpected asset URL") key = "sums" if name == "SHA256SUMS" else "pkg" if key in found: raise ValueError("ambiguous release assets") found[key] = (name, url) pkg, sums = found["pkg"], found["sums"] if sys.argv[2] == "debian" and not pkg[0].startswith("appinstall_" + tag[1:] + "-"): raise ValueError("Debian package version does not match release") if sys.argv[2] == "fedora" and not pkg[0].startswith("appinstall-" + tag[1:] + "-"): raise ValueError("Fedora RPM version does not match release") print("|".join((tag, pkg[1], sums[1], pkg[0]))) except (ValueError, KeyError, TypeError): sys.exit(4) ' "$TEST_RELEASES_JSON" "$family" <<<"$json" || return 4 } # --------------------------------------------------------------------------- # Download + checksum verification # --------------------------------------------------------------------------- download_to() { # url dest local protocols='=https' [[ -n "$TEST_RELEASES_JSON" && "$1" == file://* ]] && protocols='=file' curl -q -fsSL --proto "$protocols" --proto-redir '=https' --connect-timeout 15 --max-time 600 --retry 2 -o "$2" -- "$1" || { rm -f -- "$2"; return 4; } chmod 0644 "$2" 2>/dev/null || true } # verify_checksums dir pkg_name — exactly one matching line must validate. verify_checksums() { local dir="$1" pkg="$2" line # Release manifests can list other assets. Validate only the selected # basename, without letting manifest paths cause reads outside the temp dir. line="$(python3 - "$dir/SHA256SUMS" "$pkg" <<'CHECKSUM' import re, sys lines = [] for line in open(sys.argv[1], encoding="utf-8"): match = re.fullmatch(r"([0-9a-fA-F]{64}) [ *](.+)\n?", line) if match and match[2].rstrip("\n") == sys.argv[2]: lines.append(match[1] + " " + sys.argv[2]) if len(lines) != 1: sys.exit(3) print(lines[0]) CHECKSUM )" || return 3 (cd "$dir" && printf '%s\n' "$line" | sha256sum --check --strict - >/dev/null 2>&1) || return 3 return 0 } # --------------------------------------------------------------------------- # Existing installation and conflict handling # --------------------------------------------------------------------------- installed_version() { # -> version string, empty when absent if [[ "${1:-arch}" == fedora ]]; then rpm -q --qf '%{VERSION}-%{RELEASE}\n' appinstall 2>/dev/null || true return 0 fi if [[ "${1:-arch}" == debian ]]; then local status version IFS='|' read -r status version < <(dpkg-query -W -f='${Status}|${Version}\n' appinstall 2>/dev/null) || return 0 [[ "$status" == 'install ok installed' || "$status" == 'hold ok installed' ]] && printf '%s\n' "$version" return 0 fi pacman -Q appinstall 2>/dev/null | awk '{print $2}' } version_relation() { # a b -> a>b:1 a==b:0 a/dev/null)" || result="?" case "$result" in -1) printf 'lt\n' ;; 0) printf 'eq\n' ;; 1) printf 'gt\n' ;; *) printf 'unk\n' ;; esac return 0 fi if [[ "${3:-arch}" == debian ]]; then if dpkg --compare-versions "$1" eq "$2"; then printf 'eq\n' elif dpkg --compare-versions "$1" gt "$2"; then printf 'gt\n' elif dpkg --compare-versions "$1" lt "$2"; then printf 'lt\n' else printf 'unk\n' fi return 0 fi if have_cmd vercmp; then local r r="$(vercmp "$1" "$2")" || r="?" case "$r" in 1) printf 'gt\n';; 0) printf 'eq\n';; -1) printf 'lt\n';; *) printf 'unk\n';; esac return 0 fi [[ "$1" == "$2" ]] && { printf 'eq\n'; return 0; } printf 'unk\n' } # Returns 0 when no blocking conflict remains; may remove with consent, else exits. check_shadow_conflicts() { local found=0 [[ -e "$APP_LOCAL_BIN" ]] && found=$((found+1)) [[ -e "/usr/local/bin/appinstall" ]] && found=$((found+1)) if ((found == 0)); then return 0 fi warn "A user-local AppInstall installation was found that would shadow /usr/bin/appinstall:" [[ -e "$APP_LOCAL_BIN" ]] && warn " $APP_LOCAL_BIN" [[ -e "/usr/local/bin/appinstall" ]] && warn " /usr/local/bin/appinstall" info "Records, containers and settings under ~ are never touched by cleanup." if [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] would ask to remove the conflicting source install first\n' return 0 fi local consented=0 if [[ $REMOVE_SOURCE_INSTALL -eq 1 ]]; then # Explicit opt-in flag: the only noninteractive way to authorize removal. consented=1 elif [[ $TTY_OK -eq 1 && $DRY_RUN -eq 0 ]]; then # Interactive terminal only. --yes / ASSUME_YES does NOT authorize source # removal: removing an existing installation is a destructive decision that # always needs either --remove-source-install or a live confirmation. ASSUME_YES=0 ask "Remove the conflicting user-local AppInstall now (runs its uninstaller; your records, containers and settings are preserved)?" n && consented=1 fi if ((consented == 0)); then bad "Refusing to continue while a user-local AppInstall shadows the package. See INSTALL.md." bad "Re-run with --remove-source-install if you want it removed, or remove it manually first." exit 2 fi if [[ -e "$APP_LOCAL_BIN" && -x "$HOME/.local/bin/appinstall-uninstall" ]]; then "$HOME/.local/bin/appinstall-uninstall" || die 2 "Source uninstaller failed; resolve it manually before continuing." fi [[ ! -e "$APP_LOCAL_BIN" ]] || { rm -f -- "$APP_LOCAL_BIN"; warn "Leftover $APP_LOCAL_BIN removed."; } if [[ -e "/usr/local/bin/appinstall" ]]; then bad "/usr/local/bin/appinstall is outside this installer's scope — remove it manually (usually a leftover of a manual copy), then re-run." exit 2 fi log_write "shadow-conflict-cleared" return 0 } # --------------------------------------------------------------------------- # Per-user MIME default management (backup → set → restore) # --------------------------------------------------------------------------- STATE_BACKUP_TSV="${STATE_DIR}/previous-mime-defaults.tsv" STATE_RESTORE_SCRIPT="${STATE_DIR}/restore-mime-defaults.sh" mimeapps_path() { printf '%s/mimeapps.list\n' "${XDG_CONFIG_HOME:-$HOME/.config}" } xdg_query_default() { xdg-mime query default "$1" 2>/dev/null || printf '\n'; } backup_mime_defaults() { # idempotent: first backup wins; returns backup path mkdir -p "$STATE_DIR" if [[ -f "$STATE_BACKUP_TSV" ]]; then log_write "backup-kept path=$STATE_BACKUP_TSV" printf '%s\n' "$STATE_BACKUP_TSV" return 0 fi local mime prev staging staging="$(mktemp "$STATE_DIR/.mime-backup-XXXXXX")" for mime in "${MIME_TYPES[@]}"; do prev="$(xdg_query_default "$mime")" printf '%s\t%s\n' "$mime" "$prev" >>"$staging" done mv -- "$staging" "$STATE_BACKUP_TSV" if [[ -f "$(mimeapps_path)" ]]; then cp -p -- "$(mimeapps_path)" "${STATE_DIR}/previous-mimeapps.list.bak" || true fi chmod 600 "$STATE_BACKUP_TSV" 2>/dev/null || true log_write "backup-created path=$STATE_BACKUP_TSV" printf '%s\n' "$STATE_BACKUP_TSV" } write_restore_script() { mkdir -p "$STATE_DIR" cat >"$STATE_RESTORE_SCRIPT" <<'RESTORE' #!/usr/bin/env bash # Restores the per-user MIME defaults that were active before AppInstall was # made the default handler for supported package formats. Written by install.sh. # Per-user only; never touches system-wide settings; safe to run repeatedly. set -euo pipefail TSV="${XDG_DATA_HOME:-$HOME/.local/share}/appinstall/installer/previous-mime-defaults.tsv" MIMEAPPS="${XDG_CONFIG_HOME:-$HOME/.config}/mimeapps.list" if [[ ! -r "$TSV" ]]; then echo "No saved MIME default backup found at: $TSV" >&2 echo "Nothing to restore." >&2 exit 0 fi if [[ ! -f "$MIMEAPPS" ]]; then mkdir -p -- "${MIMEAPPS%/*}" 2>/dev/null || true fi if ! touch "$MIMEAPPS" 2>/dev/null; then echo "Cannot write $MIMEAPPS" >&2 exit 1 fi DESKTOP_ID="appinstall.desktop" python3 - "$TSV" "$MIMEAPPS" <<'PY' import os import sys tsv_path, mimeapps_path = sys.argv[1], sys.argv[2] DESKTOP_ID = "appinstall.desktop" ours = set() # every MIME the installer may have set a default for prev = {} # only those that had a non-empty previous default for raw in open(tsv_path, encoding="utf-8"): line = raw.rstrip("\n") if not line or "\t" not in line: continue mime, val = line.split("\t", 1) ours.add(mime) if val.strip(): prev[mime] = val.strip() try: text = open(mimeapps_path, encoding="utf-8").read() except FileNotFoundError: text = "" out = [] section = None for line in text.splitlines(keepends=True): stripped = line.strip() if stripped.startswith("[") and stripped.endswith("]"): section = stripped[1:-1] if section in ("Default Applications", "Added Associations") and "=" in stripped: key, value = stripped.split("=", 1) mime = key.strip() handlers = [item for item in value.split(";") if item] if mime in ours and DESKTOP_ID in handlers: if section == "Default Applications" and handlers[0] == DESKTOP_ID: saved = prev.get(mime, "") handlers = [item for item in saved.split(";") if item and item != DESKTOP_ID] else: handlers = [item for item in handlers if item != DESKTOP_ID] if not handlers: continue line = mime + "=" + ";".join(handlers) + ";\n" out.append(line) # Replace atomically so a failed write leaves the original configuration intact. import tempfile fd, staging = tempfile.mkstemp(prefix=".appinstall-mime-", dir=os.path.dirname(mimeapps_path)) try: with os.fdopen(fd, "w", encoding="utf-8") as stream: stream.writelines(out) os.chmod(staging, os.stat(mimeapps_path).st_mode & 0o777) os.replace(staging, mimeapps_path) finally: if os.path.exists(staging): os.unlink(staging) print("Restored AppInstall defaults; later user choices were preserved.") PY if command -v kbuildsycoca6 >/dev/null 2>&1; then kbuildsycoca6 --noincremental >/dev/null 2>&1 || true fi if command -v update-desktop-database >/dev/null 2>&1; then update-desktop-database "${XDG_DATA_HOME:-$HOME/.local/share}/applications" >/dev/null 2>&1 || true fi echo "Per-user MIME defaults restoration attempted." RESTORE chmod 0700 "$STATE_RESTORE_SCRIPT" } set_mime_defaults() { local mime got ok=0 fail=0 for mime in "${MIME_TYPES[@]}"; do if xdg-mime default "$DESKTOP_ID" "$mime" >/dev/null 2>&1; then got="$(xdg_query_default "$mime")" if [[ "$got" == "$DESKTOP_ID" ]]; then ok=$((ok + 1)) else fail=$((fail + 1)) warn "Default for $mime did not resolve to $DESKTOP_ID (got '${got}')." fi else fail=$((fail + 1)) warn "xdg-mime failed for $mime." fi done if ((ok == ${#MIME_TYPES[@]} && fail == 0)); then info "Default handler set for all ${#MIME_TYPES[@]} supported package MIME types (per-user only)." log_write "set-mime-defaults ok=$ok" else warn "Applied $ok of ${#MIME_TYPES[@]} defaults ($fail unresolved). Reset any time:" warn " $STATE_RESTORE_SCRIPT" log_write "set-mime-defaults ok=$ok fail=$fail" fi } maybe_set_defaults() { if [[ $DRY_RUN -eq 1 ]]; then info "[dry-run] would offer per-user MIME defaults; no associations changed." return 0 fi if [[ $NO_DEFAULTS -eq 1 ]]; then info "Skipping file-association changes (--no-defaults)." return 0 fi if ! have_cmd xdg-mime; then warn "xdg-mime not found (xdg-utils). Cannot manage per-user defaults; skipping." return 0 fi local want=0 if [[ $SET_DEFAULTS -eq 1 ]]; then want=1 elif [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] would ask: make AppInstall the default handler for AppImage/Flatpak/DEB/RPM files (9 MIME types)?\n' return 0 elif [[ $ASSUME_YES -eq 0 ]] && ask "Make AppInstall the default application for AppImage, Flatpak, DEB and RPM files (all 9 advertised MIME types)? Your previous per-user defaults are backed up and can be restored." n; then want=1 fi if ((want == 0)); then info "Keeping existing file associations. You can set defaults later with:" info " xdg-mime default $DESKTOP_ID application/vnd.appimage (repeat for other types)" return 0 fi backup_mime_defaults >/dev/null write_restore_script set_mime_defaults info "Backup of previous defaults: $STATE_BACKUP_TSV" info "Restore script (safe): $STATE_RESTORE_SCRIPT" } # --------------------------------------------------------------------------- # Optional backends and container readiness # --------------------------------------------------------------------------- missing_optional_pkgs() { # prints missing, one per line local p for p in "${OPTIONAL_PKGS[@]}"; do pacman -Q -- "$p" >/dev/null 2>&1 || printf '%s\n' "$p" done } audit_optional_backends() { if [[ $DRY_RUN -eq 1 ]]; then info "[dry-run] would audit optional backends; no packages installed." return 0 fi local missing missing="$(missing_optional_pkgs)" if [[ -z "$missing" ]]; then info "Optional backends: all present (flatpak, podman, distrobox, squashfs-tools, fuse2, kdialog/zenity, libnotify)." return 0 fi local display display="$(tr '\n' ', ' <<<"$missing")" display="${display%%,}" info "Optional backends not installed: $display" local want=0 if [[ $WITH_OPTIONAL -eq 1 ]]; then want=1 elif [[ $NO_OPTIONAL -eq 0 && $DRY_RUN -eq 0 ]]; then ask "Install the optional backends above with pacman? Recommended for Flatpak/AppImage preview and DEB/RPM via Distrobox." n && want=1 elif [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] would ask to install optional backends\n' fi if ((want == 0)); then info "Skipping optional backends. Add later with: sudo pacman -S --needed flatpak podman distrobox squashfs-tools fuse2" return 0 fi mapfile -t to_install <<<"$missing" info "Installing optional backends: ${to_install[*]}" local pacman_flags=(--needed) [[ $ASSUME_YES -eq 1 ]] && pacman_flags+=(--noconfirm) if ! run_root pacman -S "${pacman_flags[@]}" -- "${to_install[@]}"; then warn "Optional backend installation had errors — AppInstall core still works; review pacman's output." return 0 fi log_write "optional-backends-installed $(echo "$missing" | tr '\n' ' ')" } rootless_podman_report() { if [[ $DRY_RUN -eq 1 ]]; then info "[dry-run] skipping Podman runtime probe." return 0 fi if ! have_cmd podman; then info "Podman not present — DEB/RPM via Distrobox stays unavailable until installed." return 0 fi if podman unshare true >/dev/null 2>&1; then info "Rootless Podman verified working (user namespaces OK)." if have_cmd distrobox; then info "Distrobox present. Containers are NOT created by setup — only when you install your first .deb/.rpm with AppInstall." else warn "Distrobox missing; install 'distrobox' for DEB/RPM support." fi else warn "Podman present but rootless user namespaces do not work yet (podman unshare failed)." warn "Often fixed by logging out/in once (user session restart) or enabling user namespaces." warn "AppInstall itself works; only DEB/RPM-through-Distrobox needs this." log_write "rootless-podman-unshare-failed" fi } # --------------------------------------------------------------------------- # Package install # --------------------------------------------------------------------------- package_version_from_asset() { # native package filename -> version-release local base="${1##*/}" if [[ "$base" == appinstall-*.noarch.rpm ]]; then base="${base%.noarch.rpm}" printf '%s\n' "${base#appinstall-}" return 0 fi if [[ "$base" == appinstall_*_all.deb ]]; then base="${base#appinstall_}" printf '%s\n' "${base%_all.deb}" return 0 fi base="${base%.pkg.tar.zst}" base="${base%-any}" printf '%s\n' "${base#appinstall-}" } pkg_installed_state_hint() { # installed_version target_version -> words for dry-run if [[ -z "$1" ]]; then printf 'fresh install\n'; return 0; fi case "$(version_relation "$1" "$2" "${3:-arch}")" in eq) printf 'skip (already this version)\n' ;; gt) printf 'skip (installed %s is newer)\n' "$1" ;; *) printf "upgrade %s -> %s\n" "$1" "$2" ;; esac } install_package_file() { # verified pkg path local pkg="$1" if [[ "${2:-arch}" == fedora ]]; then local flags=() [[ $ASSUME_YES -eq 1 ]] && flags+=(-y) info "Installing package with Fedora DNF: $(basename "$pkg")" run_root dnf "${flags[@]}" install "$pkg" || die 5 "DNF installation failed. Review its output." [[ -e "$APP_BIN" ]] || die 5 "DNF finished but $APP_BIN is missing." return 0 fi if [[ "${2:-arch}" == debian ]]; then local flags=(--no-install-recommends) [[ $ASSUME_YES -eq 1 ]] && flags+=(-y) info "Installing package with APT: $(basename "$pkg")" run_root apt-get "${flags[@]}" install "$pkg" || die 5 "APT installation failed. Review the package manager output." [[ -e "$APP_BIN" ]] || die 5 "APT finished but $APP_BIN is missing." return 0 fi info "Installing package: pacman -U $(basename "$pkg")" if [[ $TTY_OK -eq 1 && -z "$DESKTOP_UPDATE" ]]; then run_root pacman -U --needed -- "$pkg" || die 5 "pacman reported an error and the package was not installed. Review pacman's message; nothing else was changed by this script." else if [[ $ASSUME_YES -ne 1 ]]; then die 2 "Package install needs explicit consent in a noninteractive context. Re-run with --yes (or from a terminal)." fi run_root pacman -U --needed --noconfirm -- "$pkg" || die 5 "pacman reported an error and the package was not installed." fi [[ -e "$APP_BIN" ]] || die 5 "pacman finished but $APP_BIN is missing — treat the install as failed and re-run." info "AppInstall installed: $APP_BIN exists." log_write "pacman-U-ok pkg=$(basename "$pkg")" } # --------------------------------------------------------------------------- # Uninstall path (--uninstall) # --------------------------------------------------------------------------- uninstall_flow() { local family="${1:-arch}" local remove_command=(pacman -R) [[ "$family" == debian ]] && remove_command=(apt-get remove) [[ "$family" == fedora ]] && remove_command=(dnf remove) if [[ $DRY_RUN -eq 1 ]]; then info "[dry-run] would offer package removal and association restoration." return 0 fi info "Removing AppInstall package and optionally restoring previous file associations." info "Your application records, containers, launchers, logs and settings under ~ are always preserved." if ! ask "Remove the system package now (${remove_command[*]} appinstall)? AppInstall windows should be closed first." n; then info "Keeping the package; nothing was changed." return 0 fi if [[ -z "$(installed_version "$family")" ]]; then info "AppInstall does not appear to be installed through the native package manager; nothing to remove." else if [[ "$family" == fedora ]]; then local dnf_flags=() [[ $ASSUME_YES -eq 1 ]] && dnf_flags+=(-y) run_root dnf "${dnf_flags[@]}" remove appinstall || die 5 "DNF removal failed." elif [[ "$family" == debian ]]; then local apt_flags=() [[ $ASSUME_YES -eq 1 ]] && apt_flags+=(-y) run_root apt-get "${apt_flags[@]}" remove appinstall || die 5 "APT removal failed." elif [[ $ASSUME_YES -eq 1 ]]; then run_root pacman -R --noconfirm "appinstall" || die 5 "pacman -R failed; see pacman's output above." else run_root pacman -R "appinstall" || die 5 "pacman -R failed; see pacman's output above." fi info "Package removed." fi if [[ -f "$STATE_BACKUP_TSV" ]] && ask "Restore your previous file associations (defaults that were active before AppInstall)?" n; then write_restore_script "$STATE_RESTORE_SCRIPT" || warn "Restore script had errors; backup remains at $STATE_BACKUP_TSV." else [[ -f "$STATE_BACKUP_TSV" ]] && info "Backup kept at $STATE_BACKUP_TSV — run this any time to restore defaults:" [[ -f "$STATE_BACKUP_TSV" ]] && info " $STATE_RESTORE_SCRIPT" fi log_write "appinstall-uninstalled" info "Uninstall complete." } # --------------------------------------------------------------------------- # CLI # --------------------------------------------------------------------------- usage() { cat <&2; exit 2 ;; esac shift done } # --------------------------------------------------------------------------- # Main # --------------------------------------------------------------------------- main() { info "AppInstall one-command installer ${INSTALLER_VERSION}" info "Stable GitHub releases only." # --- 1. environment ------------------------------------------------ local family distro_id arch de family="$(detect_family)" distro_id="$(detect_distro_id)" arch="$(detect_arch)" de="$(detect_de)" info "Detected: distro=${distro_id} family=${family} arch=${arch} desktop=${de}" log_write "start flags=$* family=${family} id=${distro_id} arch=${arch} de=${de}" # --- 2. support gate (STRICT: no untested installs by default) ------- local install_command="pacman -U" local package_tools=(pacman) case "$distro_id" in debian|ubuntu|linuxmint) family=debian install_command="apt-get --no-install-recommends install" package_tools=(apt-get dpkg dpkg-query dpkg-deb) if [[ $WITH_OPTIONAL -eq 1 ]]; then die 2 "--with-optional is available on Arch and CachyOS. Install any optional backends through APT." fi ;; fedora) [[ "$(detect_distro_version)" == "44" ]] || die 2 "Only Fedora 44 is verified for the candidate RPM. No changes made." family=fedora install_command="dnf install" package_tools=(dnf rpm) if [[ $WITH_OPTIONAL -eq 1 ]]; then die 2 "--with-optional is available only on Arch and CachyOS. Install Fedora optional backends through DNF." fi ;; *) if [[ "$family" != arch ]]; then die 2 "Unsupported distribution '$distro_id'. No package was downloaded and nothing was changed." fi ;; esac local is_supported=0 [[ " $SUPPORTED_IDS " == *" $distro_id "* ]] && is_supported=1 if [[ "$family" == arch ]] && ((is_supported == 0)); then if [[ $ALLOW_ARCH_LIKE -eq 1 ]]; then warn "Distro '${distro_id}' is arch-like but untested — continuing because --allow-arch-like was used. Install at your own risk." else bad "Arch-like distro '${distro_id}' is not in the tested list (${SUPPORTED_IDS})." bad "You can force installation with --allow-arch-like, but there is no compatibility claim." exit 2 fi fi if [[ "$arch" != "x86_64" ]]; then bad "Architecture '${arch}' is not supported by the tested stable release (x86_64)." bad "No untested builds will be installed." exit 2 fi # --- 3. working tools (after the gate, so unsupported systems still get # a helpful refusal even if a tool is missing) ------------------- if ((EUID == 0)); then die 2 "Run as your regular desktop user; only package-manager commands use sudo." fi if ! need_cmds curl sha256sum "${package_tools[@]}"; then exit 2 fi if [[ $DESKTOP_CHECK -eq 1 || -n "$DESKTOP_UPDATE" ]]; then [[ -z "$TEST_OS_RELEASE$TEST_RELEASES_JSON" ]] || die 2 "Desktop updates require the live release service." if [[ -n "$DESKTOP_UPDATE" ]]; then [[ -x /usr/bin/pkexec ]] || die 2 "Desktop authentication is unavailable. Update AppInstall in a terminal." have_cmd pkexec || die 2 "Install polkit and start a desktop authentication agent, or update in a terminal." ASSUME_YES=1 NO_DEFAULTS=1 NO_OPTIONAL=1 fi fi prepare_tty if [[ $DESKTOP_CHECK -eq 0 && $TTY_OK -eq 0 && $ASSUME_YES -eq 0 && $DRY_RUN -eq 0 ]]; then die 2 "Noninteractive run without --yes: nothing was installed. Run from a terminal to answer prompts." fi if ! have_cmd python3; then if [[ "$family" == debian || "$family" == fedora || $DESKTOP_CHECK -eq 1 || -n "$DESKTOP_UPDATE" ]]; then die 2 "Install the distribution's python3 package, then retry." fi if [[ $DRY_RUN -eq 1 ]]; then info "[dry-run] Python is required to validate release metadata; would offer pacman -S --needed python." return 0 fi ask "Install Python from the configured pacman repositories to validate release metadata?" n || die 2 "Python is required; nothing was installed." local python_flags=(--needed) [[ $ASSUME_YES -eq 1 ]] && python_flags+=(--noconfirm) run_root pacman -S "${python_flags[@]}" -- python || die 5 "Python dependency installation failed." have_cmd python3 || die 5 "Python is still unavailable after dependency installation." fi # --- 4. state cleanup (first-run hygiene) ---------------------------- if [[ $DRY_RUN -eq 0 && $DESKTOP_CHECK -eq 0 && -z "$DESKTOP_UPDATE" ]]; then mkdir -p "$STATE_DIR" LOG_FILE="${STATE_DIR}/install-log.txt" fi log_write "start family=${family} id=${distro_id} arch=${arch} de=${de} dry_run=${DRY_RUN} assume_yes=${ASSUME_YES} set_defaults=${SET_DEFAULTS} no_defaults=${NO_DEFAULTS} with_optional=${WITH_OPTIONAL} no_optional=${NO_OPTIONAL} allow_arch_like=${ALLOW_ARCH_LIKE}" # --- 5. uninstall path ---------------------------------------------- if [[ $UNINSTALL -eq 1 ]]; then prepare_tty uninstall_flow "$family" info "Done." return 0 fi prepare_tty # --- 6. conflict / shadow install handling -------------------------- if [[ $DESKTOP_CHECK -eq 0 ]]; then check_shadow_conflicts fi # --- 7. resolve the latest stable release --------------------------- local installed_rel="" installed="0" installed_rel="$(installed_version "$family")" || true [[ -n "$installed_rel" ]] && installed=1 info "Resolving the latest stable release from GitHub (drafts and prereleases are never used)…" local release tag pkg_url sums_url pkg_name if [[ "$family" == debian || "$family" == fedora ]]; then release="$(resolve_release "$family")" || die 4 "No verified native package for $family in latest stable release. The published v1.5.0 does not include an RPM; never install another distro's package." else release="$(resolve_release "$family")" || die 4 "GitHub API query for the latest stable release failed (network issue or rate limit). Optionally export GITHUB_TOKEN= to raise the API rate limit, then retry." fi IFS='|' read -r tag pkg_url sums_url pkg_name <<<"$release" local pkg_ver pkg_ver="$(package_version_from_asset "$pkg_name")" info "Latest stable: ${tag} — asset: ${pkg_name}" if [[ $DESKTOP_CHECK -eq 1 || -n "$DESKTOP_UPDATE" ]]; then [[ -n "$installed_rel" ]] || die 2 "A native AppInstall package is required for desktop updates." [[ "$pkg_ver" == "${tag#v}-"* ]] || die 4 "Package version does not match the selected release." local relation relation="$(version_relation "$installed_rel" "$pkg_ver" "$family")" if [[ $DESKTOP_CHECK -eq 1 ]]; then if [[ "$relation" == lt ]]; then printf 'APPINSTALL_UPDATE=%s\n' "$tag"; fi return 0 fi [[ "$tag" == "$DESKTOP_UPDATE" ]] || die 4 "The latest release changed. Check for updates again." [[ "$relation" == lt || "$relation" == eq ]] || die 5 "The selected update cannot be installed over this version." fi # --- 8. dry-run: show the complete plan, change nothing ---------------- if [[ $DRY_RUN -eq 1 ]]; then printf '[dry-run] would download: %s\n' "$pkg_url" printf '[dry-run] would download: %s\n' "$sums_url" printf '[dry-run] then: verify SHA256SUMS; %s\n' "install with $install_command $(pkg_installed_state_hint "$installed_rel" "$pkg_ver" "$family")" if [[ "$family" == arch ]]; then audit_optional_backends rootless_podman_report fi maybe_set_defaults printf '[dry-run] done — no changes were made.\n' return 0 fi # --- 9. install decision ----------------------------------------------- local already_current=0 if ((installed == 1)); then case "$(version_relation "$installed_rel" "$pkg_ver" "$family")" in eq) info "AppInstall ${pkg_ver} is already installed and is the latest stable. Nothing to install." already_current=1 ;; gt) info "Installed AppInstall ${installed_rel} appears newer than stable ${pkg_ver}. Not downgrading." already_current=1 ;; lt|unk) if ask "Upgrade the installed AppInstall (${installed_rel} → ${pkg_ver})? Close any running AppInstall window first." y; then info "Upgrading: ${installed_rel} → ${pkg_ver}." else info "Keeping existing version ${installed_rel}; nothing was changed." return 0 fi ;; esac else # First-time install consent (transparent, one clear confirmation). if ! ask "Download and install AppInstall ${pkg_ver} from the verified stable release now? Checksum is verified before anything is installed." y; then if [[ $TTY_OK -eq 0 && $ASSUME_YES -eq 0 ]]; then die 2 "Noninteractive run without --yes: nothing was installed. Re-run with --yes for unattended installation, or from a terminal to answer prompts." fi info "Not installing. Nothing was changed." return 0 fi fi if ((already_current == 0)); then TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/appinstall-install-XXXXXX")" info "Downloading package and checksum manifest to a private temp directory…" download_to "$pkg_url" "${TMP_DIR}/${pkg_name}" || die 4 "Package download failed; nothing was installed." download_to "$sums_url" "${TMP_DIR}/SHA256SUMS" || die 4 "Manifest download failed; nothing was installed." info "Verifying checksum (sha256sum -c SHA256SUMS)…" if ! verify_checksums "$TMP_DIR" "$pkg_name"; then bad "Checksum verification FAILED (or manifest mismatch)." bad "The downloaded package has been deleted. Nothing was installed." bad "Re-run this installer, or investigate your network/proxy if this repeats." log_write "verify-failed pkg=${pkg_name}" exit 3 fi info "Checksum verified." if [[ "$family" == debian ]]; then local pkg_path="${TMP_DIR}/${pkg_name}" [[ "$(dpkg-deb -f "$pkg_path" Package)" == appinstall && "$(dpkg-deb -f "$pkg_path" Version)" == "$pkg_ver" && "$(dpkg-deb -f "$pkg_path" Architecture)" == all ]] || die 3 "Debian package metadata did not match the selected release." elif [[ "$family" == fedora ]]; then local rpm_metadata="" rpm_metadata="$(rpm -qp --qf '%{NAME}|%{VERSION}-%{RELEASE}|%{ARCH}' "${TMP_DIR}/${pkg_name}")" || die 3 "Could not read RPM package metadata." [[ "$rpm_metadata" == "appinstall|$pkg_ver|noarch" ]] || die 3 "RPM metadata does not match the selected release." fi install_package_file "${TMP_DIR}/${pkg_name}" "$family" fi if [[ -n "$DESKTOP_UPDATE" ]]; then [[ "$(installed_version "$family")" == "$pkg_ver" ]] || die 5 "The installed package version could not be verified." printf 'APPINSTALL_UPDATED=%s\n' "$tag" return 0 fi # --- 10. optional backends ------------------------------------------ if [[ "$family" == arch ]]; then audit_optional_backends rootless_podman_report fi # --- 12. desktop integration / file associations --------------------- maybe_set_defaults # --- 13. summary ----------------------------------------------------- local current="${installed_rel}" if ((already_current == 0)); then current="$pkg_ver" fi info "All done." info "AppInstall version active: ${current}" info "Launcher: application menu → 'AppInstall'." info "Open a supported file (.AppImage, .flatpak, .flatpakref, .flatpakrepo, .deb, .rpm) to preview it." info "Executable .AppImage files: some file managers may run them directly instead of opening the preview; right-click → 'Open With → AppInstall' when in doubt." info "To undo file-association changes at any time: $STATE_RESTORE_SCRIPT" info "To remove AppInstall entirely (records, containers and settings preserved): curl -fsSL https://raw.githubusercontent.com/aboodmohd/-AppInstall/main/install.sh | bash -s -- --uninstall" log_write "done" return 0 } # When piped into `bash`, BASH_SOURCE[0] is unset under `set -u`. # Treat stdin execution like direct file execution, but not like `source`. if [[ "${BASH_SOURCE[0]:-$0}" == "$0" ]]; then parse_flags "$@" if [[ $DESKTOP_CHECK -eq 1 || -n "$DESKTOP_UPDATE" ]]; then [[ $# -eq 1 && $DESKTOP_CHECK -eq 1 || $# -eq 2 && -n "$DESKTOP_UPDATE" ]] || die 2 "Desktop update modes must be used on their own." DRY_RUN=0 ASSUME_YES=0 SET_DEFAULTS=0 NO_DEFAULTS=0 WITH_OPTIONAL=0 NO_OPTIONAL=0 ALLOW_ARCH_LIKE=0 REMOVE_SOURCE_INSTALL=0 UNINSTALL=0 fi if [[ $SET_DEFAULTS -eq 1 && $NO_DEFAULTS -eq 1 ]]; then bad "--set-defaults and --no-defaults are mutually exclusive." exit 2 fi if [[ $WITH_OPTIONAL -eq 1 && $NO_OPTIONAL -eq 1 ]]; then bad "--with-optional and --no-optional are mutually exclusive." exit 2 fi main fi