testsAndMisc/linux_configuration/scripts/periodic_background/digital_wellbeing/install_leechblock.sh
Krzysztof kuhy Rudnicki 1dceca6600 install_leechblock: split into lib/ under the 250-line cap
485 -> 114 lines, with the six install phases moved into four sourced
libs: leechblock_fetch.sh, leechblock_config.sh, leechblock_browsers.sh
and leechblock_firefox.sh.

detect_browsers, replace_browser_in_place and wire_up_browsers stay in
one lib on purpose: detect_browsers writes the BROWSERS/FIREFOXES arrays
that wire_up_browsers reads, and splitting them would create a
write-only cross-file global.

Four justified body changes, all recorded in the evidence:

- Quoted the six hyphenated associative-array keys. shfmt 3.13.1 parses
  an unquoted [google-chrome-stable] as arithmetic and rewrites it to
  [google - chrome - stable], which would break browser detection
  entirely. The pre-commit hook runs shfmt -w on staged files and
  re-stages them, so this corruption would have landed in the commit
  unseen -- this is what caused the previous attempt at this split to be
  reverted. Behaviour-preserving: quoted and unquoted are the same key.

- SCRIPT_DIR is assigned in the entry script rather than inside
  inject_default_config. Left in the lib, ${BASH_SOURCE[0]} resolves to
  lib/, so the defaults JSON lookup would miss and default blocking
  rules would silently never be seeded.

- install_firefox_policy takes ff_found as a parameter instead of
  reading it as a cross-file global, fixing a genuine SC2154 without a
  suppression.

- Added require_cmd unzip. It was used bare under set -Eeuo pipefail, so
  a missing unzip would abort after the version dir was populated but
  before the 'current' symlink was made, leaving a half-installed tree.

verify_shell_split.sh reports additions only -- no -/+ pair -- so
get_latest_tag and replace_browser_in_place moved byte-identically.
--help and unrecognized-flag output are byte-identical to the baseline.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-21 15:32:48 +02:00

120 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# LeechBlockNG installer for Arch Linux (and derivatives)
# - Downloads the latest release from GitHub
# - Extracts it under ~/.local/share/leechblockng/<version>
# - Wires Chromium-based browsers to auto-load the extension via --load-extension
# - For Firefox-based browsers, prints safe next steps (stable Firefox requires signed XPI)
set -Eeuo pipefail
# Each phase of the install lives in a lib beside this file; this script
# keeps the flag parsing, the shared configuration and the order the phases
# run in.
# Resolved here, in the entry script, so it points at the directory holding
# leechblock_defaults.json and seed_leechblock_storage.js. Resolving it
# inside lib/ would yield lib/ itself and silently skip default seeding.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="$SCRIPT_DIR/lib"
# shellcheck source=lib/leechblock_fetch.sh
. "$LIB_DIR/leechblock_fetch.sh"
# shellcheck source=lib/leechblock_config.sh
. "$LIB_DIR/leechblock_config.sh"
# shellcheck source=lib/leechblock_browsers.sh
. "$LIB_DIR/leechblock_browsers.sh"
# shellcheck source=lib/leechblock_firefox.sh
. "$LIB_DIR/leechblock_firefox.sh"
SCRIPT_NAME=${0##*/}
info() { printf "\033[1;34m[INFO]\033[0m %s\n" "$*"; }
warn() { printf "\033[1;33m[WARN]\033[0m %s\n" "$*"; }
err() { printf "\033[1;31m[ERR ]\033[0m %s\n" "$*"; }
require_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
err "Missing dependency: $1"
MISSING=1
fi
}
usage() {
cat <<EOF
${SCRIPT_NAME} — Download and wire up LeechBlockNG from GitHub
Usage: ${SCRIPT_NAME} [--version vX.Y[.Z]] [--force] [--install-firefox]
Options:
--version vX.Y Use a specific tag (default: latest from GitHub)
--force Reinstall even if the same version is already present
--install-firefox Auto-install from AMO for detected Firefox-based browsers (requires sudo)
Notes:
- Chromium-based browsers are integrated via a wrapper that passes --load-extension.
A desktop entry "(LeechBlock)" is created so you can launch the browser with the extension.
- Firefox stable requires signed add-ons; GitHub source cannot be permanently installed there.
We'll print safe steps to install from AMO or use Developer Edition for testing.
EOF
}
VERSION=""
FORCE=0
AUTO_FIREFOX=0
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
VERSION="$2"
shift 2
;;
--force)
FORCE=1
shift
;;
--install-firefox)
AUTO_FIREFOX=1
shift
;;
-h | --help)
usage
exit 0
;;
*)
err "Unrecognized option: $1"
usage
exit 2
;;
esac
done
# Dependencies
MISSING=0
require_cmd curl
require_cmd tar
require_cmd find
require_cmd sed
require_cmd awk
require_cmd unzip
if ! command -v jq >/dev/null 2>&1; then
warn "jq not found — will fall back to a simpler tag detection method."
fi
[[ $MISSING -eq 1 ]] && {
err "Please install missing tools and re-run."
exit 1
}
REPO_OWNER="proginosko"
REPO_NAME_CHROME="LeechBlockNG-chrome"
# Firefox repo (for reference): LeechBlockNG
# Use Chrome repo for Chromium-based browsers (the default target)
REPO_NAME="$REPO_NAME_CHROME"
resolve_version
download_extension
EXT_PATH="$CURRENT_LINK" # stable path used by wrappers
inject_default_config
detect_browsers
wire_up_browsers
install_firefox_policy "$ff_found"