#!/usr/bin/env bash # Installs Nori, and nothing else. # # curl -fsSL /nori/install.sh | bash # ... | bash -s -- --dry-run look at this computer, change nothing # ... | bash -s -- --yes no questions (administrator steps only if sudo needs no password) # ... | bash -s -- --no-admin skip everything that needs the administrator password # ... | bash -s -- --no-engines the extension only # # The extension goes to ~/.local/share/gnome-shell/extensions and its engine, if it has one, # to ~/.local, as you. The administrator steps are listed before anything asks for your password. # The whole collection has its own script: /install.sh set -uo pipefail GNOME_WANTED=51 # the newest GNOME these were run on; nothing newer was tried # The oldest GNOME each one supports, as its own metadata.json declares. One that is not # listed needs GNOME_WANTED. SINCE="nori|51" GH=https://github.com/d101c EGO=https://extensions.gnome.org/download-extension MIRROR=${GX_MIRROR:-} # test bench only: a base URL tried before GitHub # id | title | uuid | engine | what it is MINE=' ' # My settings, written only on a first install: never over yours, and only those # that make sense on another machine (no microphone name, no Wi-Fi history). SETTINGS=" " ALL_IDS="nori" # ---------------------------------------------------------------- options DRY=0; YES=0; ADMIN=1; ENGINES=1; FORCE=0; PICK=() for a in "$@"; do case "$a" in --dry-run) DRY=1 ;; --yes|-y) YES=1 ;; --no-admin) ADMIN=0 ;; --no-engines) ENGINES=0 ;; --force) FORCE=1 ;; -*) echo "Unknown option: $a" >&2; exit 2 ;; *) case " $ALL_IDS " in *" $a "*) PICK+=("$a") ;; *) echo "Unknown name: $a (known: $ALL_IDS)" >&2; exit 2 ;; esac ;; esac done [ ${#PICK[@]} -eq 0 ] && read -r -a PICK <<< "$ALL_IDS" && PICKED_ALL=1 || PICKED_ALL=0 want() { local c; for c in "${PICK[@]}"; do [ "$c" = "$1" ] && return 0; done; return 1; } # ---------------------------------------------------------------- the look if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then LIVE=1; B=$'\033[1m'; D=$'\033[2m'; I=$'\033[7m'; R=$'\033[0m'; HIDE=$'\033[?25l'; SHOW=$'\033[?25h' else LIVE=0; B=; D=; I=; R=; HIDE=; SHOW= fi TTY=0; if [ "$YES" = 0 ] && [ "$DRY" = 0 ] && { exec 3/dev/null; then TTY=1; fi SPIN=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏); TICK=0 CACHE=${XDG_CACHE_HOME:-$HOME/.cache}; mkdir -p "$CACHE"; LOG=$CACHE/gnome-extensions-install.log; : > "$LOG" TMP=$(mktemp -d); trap 'printf "%s" "$SHOW"; rm -rf "$TMP"' EXIT; trap 'printf "\n"; exit 130' INT printf '%s' "$HIDE" title() { printf '\n %s%s%s %s\n\n' "$B" "$1" "$R" "$D${2:-}$R"; } # One line of the list: sign, name, version, what is happening. paint() { printf '\r\033[K %s %-21.21s %-9.9s %s' "$1" "$2" "$3" "$4"; } busy() { [ "$LIVE" = 1 ] && paint "${SPIN[TICK++ % 10]}" "$1" "$2" "$D$3$R"; return 0; } row() { if [ "$LIVE" = 1 ]; then paint "$1" "$2" "$3" "$4"; printf '\n'; else printf ' %s %-21.21s %-9.9s %s\n' "$1" "$2" "$3" "$4"; fi; } bar() { local n=$(( $1 * 14 / 100 )) s=; local i; for ((i=0;i<14;i++)); do [ $i -lt $n ] && s+=█ || s+=░; done; printf '%s %3d%%' "$s" "$1"; } OK="✓"; UP="↑"; SAME="•"; NO="○"; WARN="!"; BAD="✗" # Runs a command in the background with the spinner on the current line. work() { # name version note cmd... local n=$1 v=$2 t=$3; shift 3 "$@" >>"$LOG" 2>&1 & local pid=$! while kill -0 "$pid" 2>/dev/null; do busy "$n" "$v" "$t"; sleep 0.08; done wait "$pid" } # ---------------------------------------------------------------- facts EXT_DIR=$HOME/.local/share/gnome-shell/extensions version_of() { # uuid -> installed version, "" when absent local f=$EXT_DIR/$1/metadata.json v [ -f "$f" ] || return 0 v=$(sed -n 's/.*"version-name"[^"]*"\([^"]*\)".*/\1/p' "$f" | head -1) [ -n "$v" ] || v=$(sed -n 's/.*"version"[^0-9]*\([0-9.]*\).*/\1/p' "$f" | head -1) printf '%s' "${v:-?}" } nori_dir=${XDG_DATA_HOME:-$HOME/.local/share}/sushi/plugins-1/nori has_typelib() { compgen -G "/usr/lib/*/girepository-1.0/$1-*.typelib" >/dev/null || compgen -G "/usr/lib64/girepository-1.0/$1-*.typelib" >/dev/null; } # ---------------------------------------------------------------- the machine printf '\n %s GNOME extensions %s\n' "$I" "$R" command -v gnome-extensions >/dev/null || { printf '\n GNOME Shell was not found. These extensions only install on a GNOME desktop.\n\n'; exit 1; } SHELLV=$(gnome-shell --version 2>/dev/null | grep -oE '[0-9]+' | head -1) case "$SHELLV" in ''|*[!0-9]*) printf '\n The version of GNOME Shell could not be read. Nothing was changed.\n\n'; exit 1 ;; esac # Each extension installs on the GNOME versions it declares. What does not fit this # computer is left out and said; --force tries everything anyway. since() { local s; for s in $SINCE; do [ "${s%%|*}" = "$1" ] && { printf '%s' "${s##*|}"; return; }; done; printf '%s' "$GNOME_WANTED"; } title_of() { local id name rest; while IFS='|' read -r id name rest; do [ "$id" = "$1" ] && { printf '%s' "$name"; return; }; done <<< "$MINE"; [ "$1" = nori ] && printf 'Nori' || printf '%s' "$1"; } LEFT_OUT= if [ "$FORCE" = 0 ]; then if [ "$SHELLV" -gt "$GNOME_WANTED" ]; then printf '\n This computer runs GNOME %s. These extensions were run up to GNOME %s, not on yours yet.\n Nothing was changed. Add --force to try anyway.\n\n' "$SHELLV" "$GNOME_WANTED"; exit 1 fi KEEP=(); MINE_KEPT=0 for c in "${PICK[@]}"; do if [ "$SHELLV" -lt "$(since "$c")" ]; then LEFT_OUT="$LEFT_OUT${LEFT_OUT:+, }$(title_of "$c") (GNOME $(since "$c"))" else KEEP+=("$c"); MINE_KEPT=$((MINE_KEPT+1)); fi done if [ -n "$LEFT_OUT" ] && [ "$MINE_KEPT" = 0 ]; then printf '\n This computer runs GNOME %s, and what you asked for needs a newer one:\n %s.\n Nothing was changed. Add --force to try anyway.\n\n' "$SHELLV" "$LEFT_OUT"; exit 1 fi PICK=("${KEEP[@]}") fi DISTRO=$( . /etc/os-release 2>/dev/null; printf '%s' "${PRETTY_NAME:-Linux}") printf ' %s%s, GNOME %s%s\n' "$D" "$DISTRO" "$SHELLV" "$R" [ -n "$LEFT_OUT" ] && printf ' %sLeft out, they need a newer GNOME than %s: %s.%s\n' "$D" "$SHELLV" "$LEFT_OUT" "$R" # ---------------------------------------------------------------- what is here now scan() { title "On this computer" "nothing is changed yet" local id name uuid eng what v es while IFS='|' read -r id name uuid eng what; do [ -n "$id" ] || continue; want "$id" || continue busy "$name" "" "looking" if [ -L "$EXT_DIR/$uuid" ]; then row "$SAME" "$name" "$(version_of "$uuid")" "development copy, will be left alone" elif v=$(version_of "$uuid"); [ -n "$v" ]; then row "$SAME" "$name" "$v" "installed, will be checked for an update" else row "$NO" "$name" "" "${D}not installed$R $what"; fi if [ "$eng" != - ] && [ "$ENGINES" = 1 ]; then busy " its engine" "" "looking" es=$(engine_state "$eng" "$uuid"); set -- $es case "${1:-absent}" in present) row "$SAME" " its engine" "${2:-}" "installed" ;; outdated) row "$UP" " its engine" "${2:-}" "outdated, will be updated" ;; *) row "$NO" " its engine" "" "${D}not installed$R" ;; esac fi done <<< "$MINE" if want nori; then if [ -L "$nori_dir" ]; then row "$SAME" "Nori" "" "development copy, will be left alone" elif [ -e "$nori_dir" ]; then row "$SAME" "Nori" "" "installed, will be refreshed" else row "$NO" "Nori" "" "${D}not installed$R select text in file previews"; fi fi } scan # ---------------------------------------------------------------- choose choose() { local ids=($ALL_IDS) labels=("Nori") local on=() i cur=0 key rest n=${#ids[@]} for i in "${!ids[@]}"; do want "${ids[i]}" && on[i]=1 || on[i]=0; done printf '\n %sChoose%s %s↑ ↓ move space toggle a all enter install%s\n\n' "$B" "$R" "$D" "$R" while :; do for i in "${!ids[@]}"; do printf '\r\033[K %s %s %s%s\n' "$([ $i -eq $cur ] && printf '›' || printf ' ')" "$([ "${on[i]}" = 1 ] && printf '◉' || printf '○')" "$([ $i -eq $cur ] && printf %s "$B")" "${labels[i]}$R" done IFS= read -rsn1 -u 3 key || break case "$key" in $'\033') read -rsn2 -t 0.05 -u 3 rest || rest=; case "$rest" in '[A') cur=$(( (cur+n-1)%n )) ;; '[B') cur=$(( (cur+1)%n )) ;; esac ;; k) cur=$(( (cur+n-1)%n )) ;; j) cur=$(( (cur+1)%n )) ;; ' ') on[cur]=$((1-on[cur])) ;; a) local all=1; for i in "${!on[@]}"; do [ "${on[i]}" = 0 ] && all=0; done; for i in "${!on[@]}"; do on[i]=$((1-all)); done ;; '') break ;; q) printf '\n Nothing was changed.\n\n'; exit 0 ;; esac printf '\033[%dA' "$n" done PICK=(); for i in "${!ids[@]}"; do [ "${on[i]}" = 1 ] && PICK+=("${ids[i]}"); done [ ${#PICK[@]} -gt 0 ] || { printf '\n Nothing selected, nothing changed.\n\n'; exit 0; } } if [ "$DRY" = 1 ]; then printf '\n %sDry run: nothing was changed.%s\n\n' "$D" "$R"; exit 0; fi if [ "$TTY" = 1 ]; then printf '\n %s enter %s install everything listed %s c %s choose %s q %s quit ' "$I" "$R" "$I" "$R" "$I" "$R" while IFS= read -rsn1 -u 3 key; do case "$key" in '') printf '\n'; break ;; c|C) printf '\n'; choose; break ;; q|Q) printf '\n\n Nothing was changed.\n\n'; exit 0 ;; esac done fi # ---------------------------------------------------------------- extensions DONE=0; UPDATED=0; KEPT=0; FAILED=(); SKIPPED=(); TODO=(); ADMIN_NOTES=(); ADMIN_CMDS=(); FRESH=() # A running Wayland shell only discovers new extensions at the next login, so # "gnome-extensions enable" refuses them: write the list the shell reads at login. turn_on() { local cur; cur=$(gsettings get org.gnome.shell enabled-extensions) case "$cur" in *"'$1'"*) return ;; "@as []"|"[]") cur="['$1']" ;; *) cur="${cur%]}, '$1']" ;; esac gsettings set org.gnome.shell enabled-extensions "$cur" } fetch() { # url dest ; the mirror first when the bench set one if [ -n "$MIRROR" ] && curl -fsS --connect-timeout 5 -o "$2" "$MIRROR/$(basename "$2")" 2>/dev/null; then return 0; fi curl -fsSL --retry 3 --connect-timeout 15 -o "$2" "$1" } # Where an extension's archive is: in the latest release, else in the newest # release that carries it (a repository also publishes its engine as releases). find_zip() { # id uuid -> url, empty when none is published local f=$2.shell-extension.zip u if [ -n "$MIRROR" ] && curl -fsSI --connect-timeout 5 "$MIRROR/$f" >/dev/null 2>&1; then printf '%s' "$MIRROR/$f"; return; fi u=$GH/$1/releases/latest/download/$f if curl -fsSIL --connect-timeout 15 "$u" >/dev/null 2>&1; then printf '%s' "$u"; return; fi curl -fsS --connect-timeout 15 "https://api.github.com/repos/d101c/$1/releases" 2>/dev/null | sed -n 's/.*"browser_download_url": *"\([^"]*\)".*/\1/p' | grep -F "/$f" | head -1 } put() { # name uuid url local name=$1 uuid=$2 url=$3 old new zip=$TMP/$2.shell-extension.zip if [ -L "$EXT_DIR/$uuid" ]; then row "$SAME" "$name" "$(version_of "$uuid")" "development copy, left alone"; KEPT=$((KEPT+1)); return 0; fi old=$(version_of "$uuid") if ! work "$name" "$old" "downloading" fetch "$url" "$zip"; then row "$BAD" "$name" "$old" "download failed"; FAILED+=("$name: download failed, run the line again"); return 1 fi if ! work "$name" "$old" "installing" gnome-extensions install --force "$zip"; then row "$BAD" "$name" "$old" "could not be unpacked"; FAILED+=("$name: could not be unpacked"); return 1 fi turn_on "$uuid"; new=$(version_of "$uuid") if [ -z "$old" ]; then row "$OK" "$name" "$new" "installed"; DONE=$((DONE+1)); FRESH+=("$uuid") elif [ "$old" != "$new" ]; then row "$UP" "$name" "$new" "updated from $old"; UPDATED=$((UPDATED+1)) else row "$SAME" "$name" "$new" "already up to date"; KEPT=$((KEPT+1)); fi } fresh() { local u; for u in ${FRESH[@]+"${FRESH[@]}"}; do [ "$u" = "$1" ] && return 0; done; return 1; } title "Extensions" while IFS='|' read -r id name uuid eng what; do [ -n "$id" ] || continue; want "$id" || continue if [ ! -L "$EXT_DIR/$uuid" ]; then busy "$name" "$(version_of "$uuid")" "looking for the latest version" url=$(find_zip "$id" "$uuid") if [ -z "$url" ]; then row "$NO" "$name" "$(version_of "$uuid")" "not published yet, skipped"; SKIPPED+=("$name"); continue; fi fi put "$name" "$uuid" "${url:-}" || continue if fresh "$uuid"; then while IFS='|' read -r sid key value; do [ "$sid" = "$id" ] && dconf write "/org/gnome/shell/extensions/$sid/$key" "$value" 2>>"$LOG" done <<< "$SETTINGS" fi done <<< "$MINE" if want nori; then was=; [ -e "$nori_dir" ] && was=1 busy "Nori" "" "looking for the latest version" ntag=$(curl -fsSI --connect-timeout 15 "$GH/nori/releases/latest" 2>/dev/null | tr -d '\r' | sed -n 's#^[Ll]ocation: .*/tag/##p') nver=${ntag#v}; nref=heads/master; [ -n "$ntag" ] && nref=tags/$ntag if [ -L "$nori_dir" ]; then row "$SAME" "Nori" "" "development copy, left alone"; KEPT=$((KEPT+1)) elif { work "Nori" "$nver" "downloading" curl -fsSL --retry 3 --connect-timeout 15 -o "$TMP/nori.tar.gz" "$GH/nori/releases/latest/download/nori.tar.gz" || work "Nori" "$nver" "downloading" fetch "$GH/nori/archive/refs/$nref.tar.gz" "$TMP/nori.tar.gz"; } && mkdir -p "$TMP/nori" && tar -xzf "$TMP/nori.tar.gz" -C "$TMP/nori" --strip-components=1 2>>"$LOG" && work "Nori" "" "installing" bash "$TMP/nori/install.sh"; then [ -n "$was" ] && { row "$SAME" "Nori" "$nver" "refreshed"; KEPT=$((KEPT+1)); } || { row "$OK" "Nori" "$nver" "installed"; DONE=$((DONE+1)); } else row "$BAD" "Nori" "" "could not be installed"; FAILED+=("Nori: see $LOG"); fi fi # ---------------------------------------------------------------- administrator installed() { [ -f "$EXT_DIR/$1/metadata.json" ]; } PKGS=() if command -v apt-get >/dev/null; then PM=apt; elif command -v dnf >/dev/null; then PM=dnf; else PM=; fi need_pkg() { # apt name, dnf name [ "$PM" = apt ] && PKGS+=("$1"); [ "$PM" = dnf ] && PKGS+=("$2"); return 0 } if want nori; then n=${#PKGS[@]}; lang=${LANG%%_*}; t3= case "$lang" in fr) t3=fra ;; de) t3=deu ;; es) t3=spa ;; it) t3=ita ;; pt) t3=por ;; nl) t3=nld ;; esac command -v sushi >/dev/null || [ -x /usr/libexec/sushi ] || [ -x /usr/bin/sushi ] || need_pkg gnome-sushi sushi command -v tesseract >/dev/null || { need_pkg tesseract-ocr tesseract; [ -n "$t3" ] && need_pkg "tesseract-ocr-$t3" "tesseract-langpack-$t3"; } command -v pdftoppm >/dev/null || need_pkg poppler-utils poppler-utils command -v qpdf >/dev/null || need_pkg qpdf qpdf [ ${#PKGS[@]} -gt $n ] && ADMIN_NOTES+=("text recognition for Nori (Tesseract and PDF tools)") fi if [ ${#ADMIN_NOTES[@]} -gt 0 ]; then title "Administrator" "the only part that needs your password" for n in "${ADMIN_NOTES[@]}"; do printf ' %s·%s %s\n' "$D" "$R" "$n"; done [ ${#PKGS[@]} -gt 0 ] && printf ' %spackages: %s%s\n' "$D" "${PKGS[*]}" "$R" printf '\n' go=0 if [ "$ADMIN" = 0 ]; then : elif sudo -n true 2>/dev/null; then go=1 elif [ "$TTY" = 1 ]; then printf ' %s enter %s continue %s s %s skip ' "$I" "$R" "$I" "$R" while IFS= read -rsn1 -u 3 key; do case "$key" in '') go=1; break ;; s|S) break ;; esac; done printf '\r\033[K' if [ "$go" = 1 ]; then printf '%s' "$SHOW"; sudo -v <&3 || go=0; printf '%s' "$HIDE"; fi fi if [ "$go" = 1 ]; then if [ ${#PKGS[@]} -gt 0 ]; then if [ "$PM" = apt ]; then work "System packages" "" "refreshing the package list" sudo -n apt-get update -q work "System packages" "" "installing ${#PKGS[@]} packages" sudo -n env DEBIAN_FRONTEND=noninteractive apt-get install -y -q "${PKGS[@]}" else work "System packages" "" "installing ${#PKGS[@]} packages" sudo -n dnf install -y -q "${PKGS[@]}" fi && row "$OK" "System packages" "" "installed" || { row "$BAD" "System packages" "" "failed"; FAILED+=("System packages: sudo $PM install ${PKGS[*]}"); } fi else row "$WARN" "Skipped" "" "nothing was run as administrator" [ ${#PKGS[@]} -gt 0 ] && TODO+=("Install the system packages: sudo $PM install ${PKGS[*]}") fi fi # ---------------------------------------------------------------- engines drive() { # name kind uuid local name=$1 line tag a b c rc last="starting" p=-1 result= exec 4< <(gjs -m "$TMP/engine.js" "$2" install "$EXT_DIR/$3" 2>>"$LOG") while :; do if IFS= read -r -t 0.1 -u 4 line; then read -r tag a b c <<< "$line" case "$tag" in P) p=$a; [ -n "$b" ] && last=$b ;; S) result="ok $b" ;; E) result="err $a $b $c" ;; esac else rc=$?; [ $rc -gt 128 ] || break; fi [ "$p" -ge 0 ] 2>/dev/null && busy "$name" "" "$(bar "$p") $last" || busy "$name" "" "$last" done exec 4<&- case "$result" in ok*) row "$OK" "$name" "${result#ok }" "installed and started"; DONE=$((DONE+1)) ;; *) result=${result%"${result##*[![:space:]]}"} case "$result" in *python3-unsupported:*) result="err not built for Python ${result##*:} yet" ;; *python3-too-old:*) result="err needs a newer Python than ${result##*:}" ;; *python3-missing*) result="err needs python3" ;; esac row "$BAD" "$name" "" "${result#err }"; FAILED+=("$name: ${result#err } (its Install button in the extension's preferences does the same)") ;; esac } if [ "$ENGINES" = 1 ]; then shown=0 while IFS='|' read -r id name uuid eng what; do [ -n "$id" ] && [ "$eng" != - ] || continue; want "$id" && installed "$uuid" || continue [ $shown = 0 ] && { title "Engines" "small background services, in your home folder"; shown=1; } set -- $(engine_state "$eng" "$uuid") if [ "${1:-}" = present ]; then row "$SAME" "$name engine" "${2:-}" "already up to date"; KEPT=$((KEPT+1)); continue; fi if ! has_typelib GnomeAutoar; then row "$WARN" "$name engine" "" "waiting for the system library"; TODO+=("$name: open its preferences and click Install, once the system packages are there"); continue; fi drive "$name engine" "$eng" "$uuid" done <<< "$MINE" fi # ---------------------------------------------------------------- the end title "Done" printf ' %s%d%s installed %s%d%s updated %s%d%s already up to date %s%d%s failed\n' "$B" "$DONE" "$R" "$B" "$UPDATED" "$R" "$B" "$KEPT" "$R" "$B" "${#FAILED[@]}" "$R" if [ ${#FAILED[@]} -gt 0 ]; then printf '\n'; for f in "${FAILED[@]}"; do printf ' %s %s\n' "$BAD" "$f"; done; printf ' %sdetails: %s%s\n' "$D" "$LOG" "$R"; fi printf '\n %sNext%s\n\n %s1%s Log out and back in: the extensions start with your next session.\n' "$B" "$R" "$B" "$R" i=2; for t in ${TODO[@]+"${TODO[@]}"}; do printf ' %s%d%s %s\n' "$B" "$i" "$R" "$t"; i=$((i+1)); done printf '\n' [ ${#FAILED[@]} -eq 0 ]