#!/usr/bin/env bash INCO_INSTALL_LIBRARY=1 INCO_VERSION=1.2.0 # Install an extracted Inco AppDir into the current user's account. # No root, no autostart, no background process, no network. # The release "Install" download is scripts/download-install.sh, which # fetches the AppImage and then calls this. # # scripts/install-appdir.sh /path/to/IncoBrowser.AppDir # scripts/install-appdir.sh --uninstall # # Writes only under $PREFIX (default $HOME/.local): # lib/inco-browser// the AppDir, then made read-only # lib/inco-browser/current symlink to # bin/inco-browser launcher script (not a symlink) # share/applications/inco-browser.desktop # share/icons/hicolor/256x256/apps/inco-browser.png # update-desktop-database, when it exists, refreshes mimeinfo.cache # in that applications directory and does not edit other desktop files. set -euo pipefail PREFIX="${PREFIX:-$HOME/.local}" stage="" work="" previous="" live_dest="" kept_busy=0 die() { if declare -f ui_fail >/dev/null 2>&1; then ui_fail "$*" fi printf 'install: %s\n' "$*" >&2 exit 1 } cleanup() { if declare -f ui_close >/dev/null 2>&1; then ui_close || true fi if [[ -n $stage && -d $stage ]]; then chmod -R u+w "$stage" 2>/dev/null || true rm -rf "$stage" fi # The live tree was renamed aside and the new one never replaced it. if [[ -n $previous && -d $previous && -n $live_dest && ! -e $live_dest ]]; then mv "$previous" "$live_dest" || true fi if [[ -n $work && -d $work ]]; then rm -rf "$work" fi if [[ -n ${download_work:-} && -d $download_work ]]; then rm -rf "$download_work" fi } trap cleanup EXIT usage() { cat <<'EOF' Install an already unpacked Inco AppDir for this account. scripts/install-appdir.sh /path/to/IncoBrowser.AppDir scripts/install-appdir.sh --uninstall The release Install file is the downloader, scripts/download-install.sh. EOF } # PREFIX must stay inside the home directory. The version we delete is # only the directory named by a dotted number, never a path we were handed. check_prefix() { [[ $(id -u) -eq 0 ]] && die "this installer is for your own account and does not run as root" [[ -n ${HOME:-} ]] || die "HOME is not set" case $PREFIX in "$HOME"/*) ;; *) die "PREFIX must be inside your home directory (got $PREFIX)" ;; esac case $PREFIX in *$'\n'*|*'..'*) die "PREFIX is not a usable path" ;; esac } read_version() { local appdir=$1 f ver for f in "$appdir/inco-browser.desktop" "$appdir/usr/share/applications/inco-browser.desktop"; do [[ -f $f ]] || continue ver=$(sed -n 's/^X-AppImage-Version=//p' "$f" | head -1) ver=${ver//$'\r'/} printf '%s' "$ver" return 0 done return 1 } valid_version() { [[ $1 =~ ^[0-9]+(\.[0-9]+){1,3}$ ]] } # One Exec argument, quoted per the Desktop Entry spec. # The file is unescaped first (\n, \s, \\), then parsed as Exec. # So a backslash the Exec parser must see is written twice. # ", `, $ and \ need that. A literal % is %%. Spaces stay spaces. exec_arg() { local s=$1 i c out="" local -i n=${#s} for ((i = 0; i < n; i++)); do c=${s:i:1} case $c in \\) out+='\\\\' ;; \"|\`|\$) out+="\\\\$c" ;; %) out+='%%' ;; *) out+=$c ;; esac done printf '"%s"' "$out" } # True when /proc says some process was exec'd from a file inside $1. # Only the kernel's executable path counts, not the command line. dir_in_use() { local root=$1 piddir exe [[ -d $root ]] || return 1 root=$(cd "$root" && pwd -P) || return 1 for piddir in /proc/[0-9]*; do [[ -e $piddir/exe ]] || continue exe=$(readlink "$piddir/exe" 2>/dev/null) || continue exe=${exe%' (deleted)'} [[ $exe == "$root" || $exe == "$root"/* ]] && return 0 done return 1 } # $2 is "fail" when skipping should make uninstall report failure. remove_tree() { local d=$1 mark=${2:-} if dir_in_use "$d"; then echo "install: leaving $d (a program from there is still running)" [[ $mark == fail ]] && kept_busy=1 return 1 fi chmod -R u+w "$d" 2>/dev/null || true rm -rf -- "$d" } # Half-finished installs from this script only. Never a version directory. reclaim_side_dirs() { local libroot=$1 d base [[ -d $libroot ]] || return 0 while IFS= read -r d; do base=$(basename "$d") case $base in .previous-*|.staging-*) ;; *) continue ;; esac remove_tree "$d" || true done < <(find "$libroot" -mindepth 1 -maxdepth 1 -type d) } quote_sh() { local s=$1 s=${s//\'/\'\\\'\'} printf "'%s'" "$s" } # Menu installs carry inco-install.txt. Source builds do not. drop_stamped() { local keep=${1:-} libroot=$PREFIX/lib/inco-browser d name [[ -d $libroot ]] || return 0 while IFS= read -r d; do name=$(basename "$d") [[ -n $keep && $name == "$keep" ]] && continue valid_version "$name" || continue [[ -f $d/inco-install.txt ]] || continue grep -q '^kind=user$' "$d/inco-install.txt" || continue remove_tree "$d" fail || true done < <(find "$libroot" -mindepth 1 -maxdepth 1 -type d) } write_launcher() { local binlink=$1 target=$2 q if [[ -L $binlink ]]; then case $(readlink "$binlink") in *lib/inco-browser/*) rm -f "$binlink" ;; *) echo "install: left $binlink alone"; return 0 ;; esac elif [[ -e $binlink ]] && ! grep -q '^# inco-install: user$' "$binlink"; then echo "install: left $binlink alone" return 0 fi rm -f "$binlink" q=$(quote_sh "$target") cat > "$binlink" </dev/null || true fi if [[ -f $stage/usr/bin/tor/tor ]]; then chmod a+x "$stage/usr/bin/tor/tor" fi printf 'version=%s\nkind=user\n' "$ver" > "$stage/inco-install.txt" local launch_rel=AppRun if [[ ! -x $stage/AppRun ]]; then launch_rel=usr/bin/IncoBrowser fi [[ -x $stage/$launch_rel ]] || die "launcher is not executable" echo "install: checking staged Tor bundle" local vlog vlog=$(mktemp) if ! env -u INCO_TOR_DIR "$stage/usr/bin/IncoBrowser" --verify-tor >"$vlog" 2>&1; then cat "$vlog" >&2 || true rm -f "$vlog" die "integrity check of staged tree failed; nothing installed" fi rm -f "$vlog" # Rename the live tree aside, then rename the staged tree into its place. # Both are on the same directory, so each mv is a rename. The old tree # is removed only after the new one is in place. cleanup puts the old # tree back if we exit before that. if [[ -e $dest ]]; then previous=$libroot/.previous-$ver-$$ mv "$dest" "$previous" fi if [[ -n $previous && ${INCO_INSTALL_ABORT:-} == after-rename ]]; then die "aborted before publishing the new tree" fi if ! mv "$stage" "$dest"; then die "could not move the new tree into place" fi stage="" if [[ -n $previous ]]; then local old=$previous previous="" # The running program's executable now lives in this renamed tree. remove_tree "$old" || true fi chmod -R a-w "$dest" ln -sfn "$ver" "$libroot/current" drop_stamped "$ver" local launch=$libroot/current/$launch_rel [[ -x $launch ]] || die "installed launcher is not executable" # AppRun finds apprun-hooks with dirname "$0". A symlink in bin/ would # make that directory ~/.local/bin. A script execs the real path instead. write_launcher "$PREFIX/bin/inco-browser" "$launch" local icon_src=$dest/usr/share/icons/hicolor/256x256/apps/inco-browser.png [[ -f $icon_src ]] || icon_src=$dest/inco-browser.png local icon_dest=$PREFIX/share/icons/hicolor/256x256/apps/inco-browser.png if [[ -f $icon_src ]]; then # cp keeps the source mode, and the tree was just made read-only. # The menu icon is the user's file, so it stays writable. cp -f "$icon_src" "$icon_dest" chmod 644 "$icon_dest" fi local desktop=$PREFIX/share/applications/inco-browser.desktop local exec_field icon_field exec_field=$(exec_arg "$launch") if [[ -f $icon_dest ]]; then icon_field=$icon_dest else icon_field=inco-browser fi cat > "$desktop" </dev/null 2>&1; then update-desktop-database "$PREFIX/share/applications" >/dev/null 2>&1 || true fi echo "Installed Inco Browser $ver" echo " $dest (read-only)" echo " menu name: Inco Browser" echo " $PREFIX/bin/inco-browser" echo "Open it from the application menu. It is already executable." local others others=$(find "$libroot" -mindepth 1 -maxdepth 1 -type d \ ! -name "$ver" ! -name '.staging-*' ! -name '.previous-*' -printf '%f ' 2>/dev/null || true) if [[ -n $others ]]; then echo " other versions kept: $others" fi } uninstall_tree() { local libroot=$PREFIX/lib/inco-browser kept_busy=0 reclaim_side_dirs "$libroot" local names=() d name if [[ -d $libroot ]]; then while IFS= read -r d; do name=$(basename "$d") valid_version "$name" || continue [[ -f $d/inco-install.txt ]] || continue grep -q '^kind=user$' "$d/inco-install.txt" || continue names+=("$name") done < <(find "$libroot" -mindepth 1 -maxdepth 1 -type d | sort) fi [[ ${#names[@]} -gt 0 ]] || die "no menu install to remove" local busy_any=0 for name in "${names[@]}"; do if dir_in_use "$libroot/$name"; then busy_any=1 fi done # Leave the menu entry while a copy we installed is still running. if [[ $busy_any -eq 0 ]]; then local desktop=$PREFIX/share/applications/inco-browser.desktop if [[ -f $desktop ]] && grep -q '^X-Inco-Install=user$' "$desktop" \ && grep -q 'lib/inco-browser/' "$desktop"; then rm -f "$desktop" fi local icon_dest=$PREFIX/share/icons/hicolor/256x256/apps/inco-browser.png local icon_src for name in "${names[@]}"; do icon_src=$libroot/$name/usr/share/icons/hicolor/256x256/apps/inco-browser.png [[ -f $icon_src ]] || icon_src=$libroot/$name/inco-browser.png if [[ -f $icon_dest && -f $icon_src ]] && cmp -s "$icon_dest" "$icon_src"; then rm -f "$icon_dest" break fi done local binlink=$PREFIX/bin/inco-browser if [[ -L $binlink ]]; then case $(readlink "$binlink") in *lib/inco-browser/*) rm -f "$binlink" ;; esac elif [[ -f $binlink ]] && grep -q '^# inco-install: user$' "$binlink"; then rm -f "$binlink" fi fi drop_stamped "" if [[ -L $libroot/current ]]; then local tgt tgt=$(readlink "$libroot/current") if [[ ! -d $libroot/$tgt ]]; then rm -f "$libroot/current" fi fi if command -v update-desktop-database >/dev/null 2>&1; then update-desktop-database "$PREFIX/share/applications" >/dev/null 2>&1 || true fi if [[ $kept_busy -eq 1 ]]; then echo "install: quit Inco and run uninstall again to remove what is still running" return 1 fi echo "Removed Inco Browser ${names[*]}" offer_remove_config } # Ask before deleting ~/.config/Inco. A pipe or a test run is not a yes. offer_remove_config() { local data="${XDG_CONFIG_HOME:-$HOME/.config}/Inco" [[ -e $data ]] || return 0 local answer=n if [[ ${INCO_UI:-} != text && -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; then if command -v kdialog >/dev/null 2>&1; then if kdialog --title "Inco Browser" --yesno "$(printf 'Remove Inco'\''s saved data as well?\n\n%s\n\nThis deletes the vault, bookmarks and settings. Downloads stay where they are.' "$data")"; then answer=y fi elif command -v zenity >/dev/null 2>&1; then if zenity --question --title="Inco Browser" --text="$(printf 'Remove Inco'\''s saved data as well?\n\n%s\n\nThis deletes the vault, bookmarks and settings. Downloads stay where they are.' "$data")"; then answer=y fi fi elif [[ -t 0 ]]; then printf 'Remove saved data in %s (vault, bookmarks, settings)? [y/N] ' "$data" read -r answer || answer=n fi case ${answer,,} in y|yes) rm -rf "$data" echo "Removed $data" ;; esac } main() { local cmd=${1:-} case $cmd in -h|--help) usage; exit 0 ;; esac check_prefix if [[ $cmd == --uninstall ]]; then uninstall_tree exit 0 fi [[ -n $cmd ]] || die "usage: install-appdir.sh | --uninstall" install_tree "$cmd" } # The release downloader sources this file. Only run main when executed. if [[ ${INCO_INSTALL_LIBRARY:-} != 1 && ${BASH_SOURCE[0]} == "$0" ]]; then main "$@" exit $? fi # Install download for Inco Browser. # Downloads the portable AppImage and SHA256SUMS from inco-project.com, # checks the file, then installs it into the account menu. # It does not stay running, start at login, or check for updates later. # # A separate graphical program would need GTK or Qt. Those are large, and # the AppImage exists so the browser does not depend on them. This script # uses kdialog (KDE) or zenity (GNOME) for a progress window when one of # those is already installed, and the terminal otherwise. set -euo pipefail INCO_VERSION="${INCO_VERSION:-1.2.0}" INCO_ASSET="Inco_Browser-${INCO_VERSION}-x86_64.AppImage" INCO_RELEASE_BASE="${INCO_RELEASE_BASE:-https://inco-project.com/downloads/v${INCO_VERSION}}" if ! declare -f install_tree >/dev/null 2>&1; then lib="$(cd "$(dirname "$0")" && pwd)/install-appdir.sh" if [[ ! -f $lib ]]; then printf 'install: this copy is incomplete (missing install-appdir.sh)\n' >&2 exit 1 fi INCO_INSTALL_LIBRARY=1 # shellcheck disable=SC1090 source "$lib" fi ui_kind=text ui_ref="" ui_pid="" ui_fifo="" have_display() { [[ -n ${DISPLAY:-}${WAYLAND_DISPLAY:-} ]]; } qdbus_bin() { command -v qdbus6 || command -v qdbus || true; } ui_open() { ui_kind=text [[ ${INCO_UI:-} == text ]] && return 0 if have_display && command -v kdialog >/dev/null 2>&1; then ui_ref=$(kdialog --title "Inco Browser" --progressbar "Starting" 100 2>/dev/null || true) [[ -n $ui_ref ]] && ui_kind=kdialog && return 0 fi if have_display && command -v zenity >/dev/null 2>&1; then ui_fifo=$(mktemp -u) mkfifo "$ui_fifo" zenity --progress --title="Inco Browser" --text="Starting" --percentage=0 --auto-close <"$ui_fifo" & ui_pid=$! exec 9>"$ui_fifo" ui_kind=zenity fi } ui_step() { local pct=$1 msg=$2 q printf '%s\n' "$msg" case $ui_kind in kdialog) q=$(qdbus_bin) if [[ -n $q ]]; then "$q" $ui_ref Set "" value "$pct" >/dev/null 2>&1 || true "$q" $ui_ref setLabelText "$msg" >/dev/null 2>&1 || true fi ;; zenity) printf '%s\n# %s\n' "$pct" "$msg" >&9 || true ;; esac } ui_close() { local q case $ui_kind in kdialog) q=$(qdbus_bin) [[ -n $q && -n $ui_ref ]] && "$q" $ui_ref close >/dev/null 2>&1 || true ui_ref="" ui_kind=text ;; zenity) exec 9>&- 2>/dev/null || true [[ -n $ui_pid ]] && wait "$ui_pid" 2>/dev/null || true rm -f "$ui_fifo" ui_pid="" ui_kind=text ;; esac } ui_fail() { local msg=$1 ui_close printf 'install: %s\n' "$msg" >&2 if [[ ${INCO_UI:-} != text ]] && have_display; then if command -v kdialog >/dev/null 2>&1; then kdialog --title "Inco Browser" --error "$msg" >/dev/null 2>&1 || true elif command -v zenity >/dev/null 2>&1; then zenity --error --title="Inco Browser" --text="$msg" >/dev/null 2>&1 || true fi fi exit 1 } # Graphical installers get a license page. Text mode does not block on it. accept_license() { local appdir=$1 lic [[ $ui_kind == text ]] && return 0 for lic in \ "$appdir/usr/share/licenses/inco-browser/LICENSE" \ "$appdir/LICENSE"; do [[ -f $lic ]] && break lic="" done [[ -n $lic ]] || return 0 if command -v kdialog >/dev/null 2>&1; then kdialog --title "Inco Browser" --textbox "$lic" 720 480 >/dev/null 2>&1 || true if ! kdialog --title "Inco Browser" --yesno "Continue and accept this license?"; then ui_fail "License not accepted. Nothing was installed." fi elif command -v zenity >/dev/null 2>&1; then if ! zenity --text-info --title="Inco Browser license" --filename="$lic" \ --checkbox="I accept the license" --width=720 --height=480; then ui_fail "License not accepted. Nothing was installed." fi fi } ui_done() { local msg=$1 ui_step 100 "$msg" ui_close if [[ ${INCO_UI:-} != text ]] && have_display; then if command -v kdialog >/dev/null 2>&1; then kdialog --title "Inco Browser" --msgbox "$msg" >/dev/null 2>&1 || true elif command -v zenity >/dev/null 2>&1; then zenity --info --title="Inco Browser" --text="$msg" >/dev/null 2>&1 || true fi fi } usage() { cat <"$dest.err") rc=$? set -e if [[ $rc -ne 0 && ( -z ${code:-} || $code == 000 ) ]]; then printf '000' return 0 fi printf '%s' "${code:-000}" } explain_http() { local code=$1 name=$2 if [[ $code == 000 ]]; then ui_fail "Could not reach inco-project.com to download ${name}. Check the network and run this again. Nothing was installed." fi if [[ $code == 404 ]]; then ui_fail "inco-project.com has no ${name} for v${INCO_VERSION} (HTTP 404). Check the version and run this again. Nothing was installed." fi if [[ $code != 200 ]]; then ui_fail "inco-project.com returned HTTP ${code} for ${name}. Nothing was installed." fi } download_and_install() { local work sums app hash got appdir command -v curl >/dev/null 2>&1 || ui_fail "This installer needs curl. Fedora: sudo dnf install curl. Debian: sudo apt install curl." command -v sha256sum >/dev/null 2>&1 || ui_fail "This installer needs sha256sum from coreutils." download_work=$(mktemp -d) ui_open ui_step 10 "Downloading the checksum list" sums=$download_work/SHA256SUMS explain_http "$(http_get "$INCO_RELEASE_BASE/SHA256SUMS" "$sums")" "SHA256SUMS" ui_step 30 "Downloading Inco Browser ${INCO_VERSION}" app=$download_work/$INCO_ASSET explain_http "$(http_get "$INCO_RELEASE_BASE/$INCO_ASSET" "$app")" "$INCO_ASSET" ui_step 75 "Checking the download" hash=$(awk -v name="$INCO_ASSET" '$2 == name { print $1; exit }' "$sums") if [[ -z $hash ]]; then ui_fail "SHA256SUMS from the v${INCO_VERSION} release does not list ${INCO_ASSET}. Nothing was installed." fi got=$(sha256sum "$app" | awk '{ print $1 }') if [[ ${got,,} != ${hash,,} ]]; then ui_fail "The download does not match the published SHA256SUMS. Nothing was installed." fi ui_step 85 "Unpacking" chmod +x "$app" if ! (cd "$download_work" && "$app" --appimage-extract >/dev/null 2>&1); then ui_fail "The AppImage did not unpack. Nothing was installed." fi appdir=$download_work/squashfs-root [[ -d $appdir ]] || ui_fail "The AppImage did not unpack. Nothing was installed." ui_step 92 "Installing into the application menu" accept_license "$appdir" install_tree "$appdir" ui_done "Inco Browser ${INCO_VERSION} is in the application menu." } case ${1:-} in -h|--help) usage; exit 0 ;; --uninstall) check_prefix uninstall_tree exit $? ;; "") check_prefix download_and_install ;; *) printf 'install: unknown argument %s\n' "$1" >&2 usage >&2 exit 2 ;; esac