#!/bin/sh
#
# Spk-up - Update packages. Read the README before adding or
# modifying any code in spk!
#
# Copyright (C) SliTaz GNU/Linux - BSD License
# Author: See AUTHORS files
#
. /usr/lib/slitaz/libspk.sh

#
# Functions
#

# Help and usage
usage() {
	name=$(basename $0)
	cat << EOT

$(boldify $(gettext "Usage:")) $name [packages|--options]

$(gettext "Update packages lists and upgrade the system")

$(boldify $(gettext "Options:"))
  --list       $(gettext "Recharge the packages lists")
  --check      $(gettext "Report upgradable packages without installing")
  --add        $(gettext "Install upgrades automatically")
  --upgrade    $(gettext "With --raw: upgrade only the named packages")
  --forced     $(gettext "Force recharging the lists")
  --mirror=    $(gettext "Specify a mirror to check")
  --apps       $(gettext "With --list: also fetch apps.list and featured.list")
  --raw        $(gettext "Plain TSV on stdout (one upgradable per line)")
  --trame      $(gettext "JSON object on stdout (--check implied)")

$(boldify $(gettext "Examples:"))
  $name package1 package2
  $name --list --forced --mirror=main

EOT
	exit 0
}

# Headers for system or packages update
up_headers() {
	newline
	boldify $(gettext "Package") \
	$(echo -n $(indent 28 $(gettext "Version"))) \
	$(echo -n $(indent 48 $(gettext "Status"))) \
	$(echo -n $(indent 68 $(gettext "Mirror")))
	separator
}

# Fetch one mirror file into the current directory. Local mirrors
# (file:// scheme or a bare absolute path) are copied — busybox wget
# rejects file:// URLs. Remote mirrors go through wget. Relies on
# $url and $mirror_local set by recharge_lists() for the loop.
mirror_fetch() {
	if [ "$mirror_local" ]; then
		cp -f "${url%/}/$1" "$1" 2>/dev/null
	else
		# -T: a black-holed mirror (SYN accepted, no data) must time out,
		# not hang a cron'ed spk-up forever.
		spk_wget "$1" "${url%/}/$1" "$1" -T 30
	fi
}

# Cheap format sanity check for a freshly downloaded list. Returns 0 when
# the file's first non-empty line matches the shape its type demands, so a
# mirror that answered 200 with an HTML error page (or a truncated body that
# happens to start mid-line) is rejected before it poisons the DB. Only the
# head is inspected — enough to tell a real list from garbage without a full
# O(n) scan of ~6000 lines. Usage: list_format_ok <file> <md5|tsv>
list_format_ok() {
	local f="$1" kind="$2" line
	# A list we intentionally kept from .bak (empty on the mirror) is fine.
	[ -s "$f" ] || return 0
	line=$(grep -m1 . "$f" 2>/dev/null)
	# Any list starting with an HTML/XML tag is an error page, never data.
	case "$line" in '<'*) return 1 ;; esac
	case "$kind" in
		md5) # "d41d8cd98f00b204e9800998ecf8427e  pkg-1.0.tazpkg"
			case "$line" in
				[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"  "*) return 0 ;;
				*) return 1 ;;
			esac ;;
		tsv) # packages.info is TAB-separated; a real line always has one.
			case "$line" in *"	"*) return 0 ;; *) return 1 ;; esac ;;
	esac
	return 0
}

# Upgrade order for a live system: spk first — it ships the static
# rename helper (spk-mv) and the logic the rest of the batch relies on —
# then the packages whose mid-upgrade breakage freezes the machine
# (loader/libc pair, shell, base files), then everything else. Same
# proven order as tazpkg's essential list. Overridable from the
# environment for exotic setups.
UP_PRIORITY="${UP_PRIORITY:-spk tazpkg glibc-base gcc-lib-base busybox-pam busybox slitaz-base-files}"

# Editorial list of recommended packages (spk-ls --featured), kept in the
# spk repository so it can change without a spk release. Overridable from
# the environment like UP_PRIORITY: a local path or file:// URL is copied,
# an empty value disables the fetch.
FEATURED_URL="${FEATURED_URL-https://hg.slitaz.org/spk/raw-file/tip/data/featured.list}"

# --apps (spk-gui Refresh) adds two optional downloads to a recharge:
# apps.list of each mirror (spk-ls --apps) and featured.list. A plain
# recharge (console, VM) never fetches nor touches them.
#
# Refresh apps.list in the current mirror dir (cd'ed by recharge_lists).
# Optional: a mirror that does not serve it (404) is no error and gets
# no wget line, a copy it no longer serves goes away, an error page
# served as 200 (not TSV) is dropped. Never fails the recharge.
fetch_apps() {
	[ "$apps" ] || return 0
	rm -f apps.list
	mirror_fetch apps.list 2>/dev/null
	[ -s apps.list ] || rm -f apps.list
	list_format_ok apps.list tsv || rm -f apps.list
	return 0
}

# Is file $1 a sane featured.list? Not empty, at most 64 KiB, at least
# one package name, and nothing but names, # comments and blank lines.
# awk, not grep -qv: busybox 1.37 grep -qv (and grep -v >/dev/null)
# exits 1 as soon as one line matches, even when others do not.
featured_ok() {
	[ -s "$1" ] && [ "$(wc -c < "$1")" -le 65536 ] || return 1
	LC_ALL=C awk '/^#/ || /^$/ { next }
		/^[A-Za-z0-9+._-]+$/ { n++; next }
		{ bad = 1; exit }
		END { exit bad || !n }' "$1"
}

# Refresh $featuredlist from $FEATURED_URL. Optional: any failure (no
# network, 404 until the file is on hg, garbage) keeps the current copy
# and prints nothing. Only the bare names are stored: comments, blank
# lines and whatever a broken or hostile server sent never reach the DB.
fetch_featured() {
	local new="$featuredlist.new"
	[ "$FEATURED_URL" ] || return 0
	rm -f "$new"
	case "$FEATURED_URL" in
		file://*|/*)
			head -c 65537 "${FEATURED_URL#file://}" > "$new" 2>/dev/null ;;
		*)
			# head bounds what a server can make us store; -T bounds a
			# stalled connection (recharge must never hang on this).
			busybox wget $WGET_OPT -T 10 -q -O - "$FEATURED_URL" \
				2>/dev/null | head -c 65537 > "$new" 2>/dev/null ;;
	esac
	if featured_ok "$new"; then
		LC_ALL=C grep -E '^[A-Za-z0-9+._-]+$' "$new" > "$new.ok" && \
			mv -f "$new.ok" "$featuredlist"
	fi
	rm -f "$new" "$new.ok"
}

# Emit the packages given as arguments with the $UP_PRIORITY members
# first (in priority order), then the rest in their original order.
up_order() {
	local p a
	for p in $UP_PRIORITY; do
		for a in "$@"; do
			[ "$a" = "$p" ] && echo "$a"
		done
	done
	for a in "$@"; do
		case " $UP_PRIORITY " in
			*" $a "*) ;;
			*) echo "$a" ;;
		esac
	done
}

# Package $1 just upgraded in a batch of $2: re-exec the new spk-up for
# the rest? Only when it was spk, once (--resume never hops again), and
# on the running system. Under --root= the spk upgraded is the target's:
# /usr/sbin/spk-up is still the host's, the very code running now, so
# the batch goes on in this process (lock, counts and traps kept).
spk_hop() {
	[ "$1" = spk ] && [ "$2" -gt 1 ] && [ -z "$resume" ] && [ -z "${root%/}" ]
}

# One spk-up at a time: a cron --check truncating packages.up while a
# user --add consumes it (or two recharges interleaving) corrupts both.
# Re-entrant via $have_lock; released on any exit through the traps.
# The lock dir holds the owner PID: a run killed by SIGKILL, a closed
# terminal or a power cut left the dir behind and blocked every later
# spk-up. A lock whose PID is no longer a running spk-up is stale.
spkup_lock() {
	[ "$have_lock" ] && return 0
	lock="$PKGS_DB/.spk-up.lock"
	if ! mkdir "$lock" 2>/dev/null; then
		pid=$(cat "$lock/pid" 2>/dev/null)
		if [ "$pid" ] && grep -q spk-up /proc/$pid/cmdline 2>/dev/null; then
			spkup_busy "$pid"
		fi
		rm -rf "$lock"
		if ! mkdir "$lock" 2>/dev/null; then
			spkup_busy
		fi
	fi
	echo $$ > "$lock/pid"
	have_lock=yes
	trap 'spkup_unlock' EXIT
	# PIPE: the reader of the output went away (a GUI that quit, | head).
	# Untrapped, the next printf killed spk-up and left the lock behind.
	# The handlers never write to stdout: a write there would raise
	# SIGPIPE again inside the handler.
	trap 'spkup_unlock; exit 1' HUP INT TERM PIPE
}

# The lock is held by another run (pid $1 when known): say so, exit 1.
# --raw reports it like its other failures, on stdout in the stream: an
# error line, then the path's done line when it has one ($raw_done, set
# by the --raw --add and --list --raw paths; --check --raw has none).
# The human message there sat in the middle of the TSV, and under
# --raw --add it went to the log: the stream just stopped.
spkup_busy() {
	if [ "$output" = raw ]; then
		printf 'error\tlock\t-\tanother spk-up is running%s\n' \
			"${1:+ (pid $1)}"
		[ "$raw_done" ] && printf 'done\t-\t-\t%s\n' "$raw_done"
	else
		gettext "ERROR: another spk-up is running"
		if [ "$1" ]; then echo " (pid $1)"; else newline; fi
	fi
	exit 1
}

spkup_unlock() {
	rm -rf "$PKGS_DB/.spk-up.lock"
}

# Why a package failed under --raw --add, from spk-add's --raw output
# ($1): the message of its last error line (checksum, download,
# unavailable dependency...), else the generic one.
raw_add_why() {
	local why
	why=$(printf '%s\n' "$1" | awk -F'\t' \
		'$1 == "error" && $4 != "" { m = $4 } END { print m }')
	echo "${why:-failed (see log)}"
}

# --raw --upgrade: keep of the system scan ($pkgsup, $pkgsup.tsv) only
# the packages named in "$@", once each. A blocked one gets an error
# line, one with nothing to upgrade (not installed, up to date, unknown)
# a skip line, as spk-add does. Sets $todo, counts in $r_skip/$r_fail.
up_select() {
	local a seen=" "
	todo=
	for a in "$@"; do
		case "$a" in --*) continue ;; esac
		case "$seen" in *" $a "*) continue ;; esac
		seen="$seen$a "
		if grep -qxF -e "$a" "$pkgsup"; then
			todo="$todo $a"
		elif awk -F'\t' -v p="$a" '$1 == p && $4 == "blocked" { f = 1 }
			END { exit !f }' "$pkgsup.tsv"; then
			printf 'error\tupgrade\t%s\tblocked\n' "$a"
			r_fail=$((r_fail + 1))
		else
			if is_package_installed "$a"; then
				printf 'ok\tskip\t%s\tno upgrade available\n' "$a"
			else
				printf 'ok\tskip\t%s\tnot installed\n' "$a"
			fi
			r_skip=$((r_skip + 1))
		fi
	done
	todo="${todo# }"
}

# Recharges all lists from one mirror or all mirrors
recharge_lists() {
	spkup_lock
	newline
	boldify $(gettext "Updating packages lists")
	separator
	# Snapshot mirror metadata before touching anything. If a recharge
	# trashes the DB (network glitch mid-wget, mirror serving garbage,
	# kernel oops…) the last 3 known-good states stay restorable from
	# /var/cache/spk/snapshots/.
	snapshot_db recharge_lists
	local check="$extradb/*/mirror $PKGS_DB/mirror"
	if [ "$mirror" ]; then
		check=$extradb/$mirror/mirror
		[ "$mirror" = "main" ] && check=$PKGS_DB/mirror
	fi
	for mirror in $check; do
		[ -f "$mirror" ] || continue
		# Skip local mirror (symlinked packages.info)
		[ ! "$(readlink "$(dirname "$mirror")/packages.info")" ] || continue
		# Get want a mirror name and download url
		name=$(basename $(dirname $mirror))
		url=$(cat $mirror)
		# IDs ("<md5> <timestamp>"): tazpkg dates its "Last recharge"
		# from it and says "never" after a refresh done by spk alone.
		lists="packages.list packages.md5 packages.info packages.equiv \
			files-list.lzma files-list.md5 descriptions.txt extra.list IDs"
		[ "$(dirname $mirror)" = "$PKGS_DB" ] && name="main"
		[ "$quiet" ] && quiet="-q"

		# Local mirror? Normalize the file:// scheme to a plain path so
		# mirror_fetch() copies the lists instead of trying to wget them.
		mirror_local=""
		case "$url" in
			file://*) url=${url#file://}; mirror_local=yes ;;
			/*)       mirror_local=yes ;;
		esac

		gettext "Checking mirror:"; colorize 34 " $name"
		cd $(dirname $mirror)

		# ID — fetched quietly (tiny change-detection file, every run).
		# It doubles as the reachability probe: if it can't be fetched
		# (mirror down, DNS failure, HTTP 503…) skip this mirror and keep
		# the current lists. Fetched to ID.new and committed LAST, once
		# the lists are in and validated: an interrupted recharge used to
		# leave the new ID beside stale lists, and every later run said
		# "up-to-date" forever.
		rm -f ID.new
		if [ "$mirror_local" ]; then
			cp -f "${url%/}/ID" ID.new 2>/dev/null
		else
			busybox wget $WGET_OPT -T 10 -q -O ID.new ${url%/}/ID
		fi
		if [ ! -s "ID.new" ]; then
			rm -f ID.new
			colorize 31 "$(gettext 'Mirror is unreachable, keeping current lists')"
			# Propose the known alternatives so the admin can switch:
			#   echo "http://..." > /var/lib/tazpkg/mirror
			if [ -s "$PKGS_DB/mirrors" ]; then
				gettext "Alternative mirrors in"; echo " $PKGS_DB/mirrors:"
				sed 's/^/  /' "$PKGS_DB/mirrors"
			fi
			continue
		fi
		debug "ID: $(cat ID 2>/dev/null)"
		debug "ID.new: $(cat ID.new)"
		if [ "$(cat ID.new)" = "$(cat ID 2>/dev/null)" ] && [ ! "$forced" ]; then
			rm -f ID.new
			gettext "Mirror is up-to-date"; newline
			# Same lists, but the first --apps run must still get it.
			fetch_apps
			continue
		fi

		# Backup and get all lists. A list that fails to download (mirror
		# went down mid-recharge, file missing on mirror) is restored from
		# its backup so the DB never loses a list it had before.
		fetch_fail=0
		for list in $lists
		do
			[ -f "$list" ] && cp -f $list $list.bak
			debug "url: ${url%/}/$list"
			rm -f $list
			mirror_fetch $list
			if [ ! -s "$list" ]; then
				rm -f $list
				[ -f "$list.bak" ] && cp -f $list.bak $list
				# An essential list restored from backup (or missing)
				# means this recharge is NOT current: the new ID must
				# not be committed, or every later run says up-to-date
				# against stale lists. Optional lists don't gate.
				case "$list" in
					packages.md5|packages.info) fetch_fail=1 ;;
				esac
			fi
		done

		# Integrity gate. A mirror behind a captive portal / broken CDN can
		# answer 200 with an HTML error page, and a connection cut mid-fetch
		# leaves a non-empty but truncated list — both slip past the size
		# test above and poison every downstream grep. Validate the FORMAT of
		# the two lists the resolver relies on (scheme-independent: the
		# official mirror's ID is a revision counter, not md5(packages.md5),
		# so ID can't be used as the checksum). packages.md5 must open with a
		# real "32-hex  file" line; packages.info must be tab-separated. If
		# either looks wrong, roll the WHOLE set back to the .bak snapshot so
		# the generations never mix, and skip this mirror.
		if ! list_format_ok packages.md5 md5 || ! list_format_ok packages.info tsv; then
			colorize 31 "$(gettext 'Mirror served malformed lists, rolled back')"
			rm -f ID.new
			for list in $lists; do
				# No backup (first recharge): drop the garbage rather
				# than leave an HTML page where a list is expected.
				if [ -f "$list.bak" ]; then cp -f "$list.bak" "$list"
				else rm -f "$list"; fi
			done
			continue
		fi
		fetch_apps
		# tazpkg (search, recharge) reads the same list as files.list.lzma:
		# one file, two names, else it searches a stale copy.
		[ -s files-list.lzma ] && ln -f files-list.lzma files.list.lzma
		# Everything fetched and sane: commit the new ID (change marker).
		if [ "$fetch_fail" = 0 ]; then
			mv -f ID.new ID
		else
			rm -f ID.new
		fi
	done
	# --apps: once per recharge, whatever the mirrors said (up-to-date
	# or not): the list lives on hg, not on the mirror.
	[ "$apps" ] && fetch_featured
	separator
	# Refresh the optional SQLite index so spk-sql and the fast paths that
	# read it stay in sync with the lists we just pulled. No-op without
	# sqlite3 (the index is a pure accelerator, never required).
	command -v sqlite3 >/dev/null 2>&1 && spk-sql import >/dev/null 2>&1
}

# Repo priority: local, extras then official. Only emit a repo that
# actually carries a packages.md5 — a bare or half-registered dir (e.g.
# extra/local with no lists) would make every fgrep/grep lookup below
# spew "No such file or directory" once per scanned package. Also fixes
# the old loop that collapsed all extras down to the last one.
priority() {
	local d
	[ -f "$extradb/local/packages.$SUM" ] && echo "$extradb/local"
	for d in "$extradb"/*; do
		[ "$d" = "$extradb/local" ] && continue
		[ -f "$d/packages.$SUM" ] && echo "$d"
	done
	echo "$PKGS_DB"
}

# Types: blocked, new build or new version.
# Appends one TSV row to $pkgsup.tsv for downstream rendering:
#   pkg<TAB>cur_ver<TAB>new_ver<TAB>status<TAB>repo
# Status is one of: blocked | new-build | new-version.
up_type() {
	local status new
	# Exact field-1 match, first hit only. A plain grep "^$PACKAGE\t" both
	# treated regex metacharacters in the name as wildcards (gtk+ , gcc-lib.*)
	# and, on a depot carrying two builds of one package, returned two lines
	# so $new became multi-line and broke the TSV row below. The leading
	# "=" tells a package absent from this repo from one with no version.
	new=$(awk -F'	' -v p="$PACKAGE" '$1==p{print "=" $2; exit}' "$dbinfo")

	# Jump to next repository if pkg doesn't exist in this one.
	[ -n "$new" ] || continue
	new="${new#=}"

	# Blocked — whole-line fixed-string match. An unanchored "^$PACKAGE"
	# regex marked vim (and vi) blocked whenever blocked.list held vim-tiny,
	# freezing unrelated packages forever. blocked.list is read once per
	# run (scan_init).
	scan_init
	if case "$blocked_list" in *"$nl$PACKAGE$nl"*) true ;; *) false ;; esac; then
		blocked_count=$(($blocked_count + 1))
		status=blocked
	elif [ "$VERSION" = "$new" ]; then
		build_count=$(($build_count + 1))
		status=new-build
	else
		status=new-version
	fi

	# Always record the structured row (consumed by --check/--trame).
	printf '%s\t%s\t%s\t%s\t%s\n' \
		"$PACKAGE" "$VERSION" "$new" "$status" "$reponame" \
		>> $pkgsup.tsv

	# Human rendering — skipped in raw/trame so stdout stays parseable.
	# (indent/colorize codes written out: six subshells per row before)
	if [ "$output" = human ]; then
		case "$status" in
			blocked)     c=31 st="$l_blocked" ;;
			new-build)   c=34 st="$l_newbuild" ;;
			new-version) c=32 st="$l_new $new" ;;
		esac
		repo_label
		if [ "$notty" ]; then
			printf '%-27s %-19s %-19s %s\n' \
				"$PACKAGE" "$VERSION" "$st" "$rlabel"
		else
			printf '%s\033[28G %s\033[1;%sm\033[48G %s\033[0;39m\033[68G %s\n' \
				"$PACKAGE" "$VERSION" "$c" "$st" "$rlabel"
		fi
	fi

	[ "$status" = blocked ] && break
	echo "$PACKAGE" >> $pkgsup
}

# Read once per run what check_pkgup/up_type used to re-read for every
# package: the repo priority list, blocked.list and the translated labels.
scan_init() {
	[ "$scan_ready" ] && return 0
	scan_ready=yes
	blocked_list="$nl$(cat "$blocked" 2>/dev/null)$nl"
	repos=$(priority)
	l_official=$(gettext "Official")
	l_local=$(gettext "Local")
	l_extra=$(gettext "Extra")
	l_blocked=$(gettext "Blocked")
	l_newbuild=$(gettext "New build")
	l_new=$(gettext "New")
}

# $reponame is a key (Official, Local, Extra): it goes to $pkgsup.tsv,
# hence to the --raw and --trame streams, and must not be translated.
# Its label for the human table, in $rlabel.
repo_label() {
	case "$reponame" in
		Official) rlabel="$l_official" ;;
		Local)    rlabel="$l_local" ;;
		Extra)    rlabel="$l_extra" ;;
		*)        rlabel="$reponame" ;;
	esac
}

# Installed builds the system scan can skip without check_pkgup: their
# installed.md5 line is verbatim in extra/local or in the first repo
# check_pkgup compares against, so it would find them up to date. One
# awk pass instead of a read loop over installed.md5 plus an fgrep per
# package (12 s for 600 packages). Sets $fresh and $stale: newline
# framed lists of installed .tazpkg names whose line is / is not there.
nl='
'
fresh_builds() {
	local repo lists=""
	fresh=""; stale=""
	[ -f "$installed.$SUM" ] || return 0
	[ -f "$extradb/local/packages.$SUM" ] && lists="$extradb/local/packages.$SUM"
	for repo in $(priority); do
		[ -f "$repo/packages.info" ] && [ -f "$repo/packages.$SUM" ] || continue
		lists="$lists $repo/packages.$SUM"
		break
	done
	[ "$lists" ] || return 0
	fresh="$nl$(awk -v inst="$installed.$SUM" -v want=1 "$fresh_awk" \
		$lists "$installed.$SUM")$nl"
	stale="$nl$(awk -v inst="$installed.$SUM" -v want=0 "$fresh_awk" \
		$lists "$installed.$SUM")$nl"
}
fresh_awk='FILENAME != inst { ok[$0] = 1; next }
	{ hit = ($0 in ok) ? 1 : 0; f = $0; sub(/^[^ ]*  /, "", f) }
	hit == want { print f }'

# Check if we have an upgrade for a package
check_pkgup() {
	unset_receipt
	if [ -f "$pkg/receipt" ]; then
		source_receipt $pkg/receipt
		# System scan fast path, see fresh_builds: up to date when
		# every installed.md5 line of this build is fresh.
		local b="$nl$PACKAGE-${VERSION}${EXTRAVERSION}"
		case "$fresh" in *"$b.tazpkg$nl"*|*"$b-"*)
			case "$stale" in
				*"$b.tazpkg$nl"*|*"$b-"*) ;;
				*) return 0 ;;
			esac
		esac
	else
		# Orphaned DB entry: still recorded in installed.info but its
		# receipt directory is gone (half-removed package / corrupt DB).
		# source_receipt would 'continue' the scan loop and skip it
		# silently, hiding a real upgrade — tazpkg reports these. Recover
		# the name+version straight from installed.info instead.
		local oline=$(awk -F'	' -v p="$pkg" '$1==p{print;exit}' \
			"$installed.info" 2>/dev/null)
		[ -n "$oline" ] || return 0
		PACKAGE=$(echo "$oline" | cut -f1)
		VERSION=$(echo "$oline" | cut -f2)
		EXTRAVERSION=
	fi
	localdb=$extradb/local
	# Look up the installed package's checksum by base name, accepting
	# whatever arch suffix is actually recorded (-x86_64, -arm, -any, or
	# none on i486). Reconstructing -$SLITAZ_ARCH missed noarch '-any'.
	# One awk, not a read loop: busybox read takes a byte per syscall and
	# installed.md5 was read that way for every package.
	local base="$PACKAGE-${VERSION}${EXTRAVERSION}"
	scan_init
	sum=$(awk -v b="$base" '$2 == b ".tazpkg" ||
		(index($2, b "-") == 1 && $2 ~ /\.tazpkg$/) { print $1; exit }' \
		"$installed.$SUM" 2>/dev/null)

	# Skip up-to-date local packages. Guard on the list file itself, not
	# just the dir: an empty extra/local would make fgrep error out. Require
	# a non-empty sum: with sum="" (orphan with no installed.md5 line) the
	# pattern collapses to "  $PACKAGE-", a substring that matches ANY build
	# of the package and would falsely mark it up-to-date, hiding the very
	# upgrade the orphan-recovery path above is meant to surface.
	if [ -n "$sum" ] && [ -f "$localdb/packages.$SUM" ] && fgrep -q "$sum  $PACKAGE-" $localdb/packages.$SUM; then
		reponame=Local
		return 0
	fi

	for repo in $repos; do
		dbinfo=$repo/packages.info
		dbsum=$repo/packages.$SUM
		# Skip a repo whose lists are missing (never recharged, or lost to
		# a failed recharge) instead of erroring out once per package.
		[ -f "$dbinfo" ] && [ -f "$dbsum" ] || continue

		# Mirror name
		case $repo in
			$PKGS_DB) reponame=Official ;;
			$localdb) reponame=Local ;;
			*) reponame=Extra ;;
		esac
		# Overwrite: Local has priority
		#if [ -d "$localdb" ] && fgrep -q "$sum  $PACKAGE-" $localdb/packages.$SUM; then
			#reponame=$(gettext "Local")
		#fi

		# Sum match or not ? Repos are scanned by priority (local, extra,
		# official). up_type() jumps to the next repo when the package name
		# is absent here, so the first repo we actually compare against is
		# the highest-priority one that carries the package.
		if [ -z "$sum" ] || ! fgrep -q "$sum  $PACKAGE-" $dbsum; then
			# No installed checksum (orphan) or present under a different
			# build/version → let up_type() compare by version instead of by
			# a checksum we don't have. An empty sum must NOT reach fgrep as
			# a bare "  $PACKAGE-" substring (matches any build → false
			# up-to-date).
			up_type
			break
		else
			# Installed build is current in this repo → up to date. Without
			# this break the loop fell through to lower-priority repos (e.g. a
			# package installed from Extra would then mismatch Official's
			# different build and be reported as a bogus "New version" every
			# run, reinstalling in a loop under --add).
			break
		fi
	done
}

# Pre-download one upgradable package into the cache (no install). The
# batch upgrade fetches every package up front so a mid-run network glitch
# can't leave the system half-upgraded. download() md5-verifies the file.
download_up() {
	mirrored_pkg "$1"
	[ "$mirrored" ] || return 1
	download "$(full_package "$1")" "$mirror"
}

# Log and install an upgradable package. Run after download_up has filled
# the cache: keepcache makes spk-add --forced reuse the verified copy
# instead of fetching it a second time.
install_up() {
	mirrored_pkg $pkg
	vers=$(echo "$mirrored" | awk -F'	' '{print $2}')
	mkdir -p $logdir/$pkg
	echo "$(date '+%Y-%m-%d %H:%M') : Upgrade to $vers" >> $logdir/$pkg/up.log
	# Propagate spk-add's exit status so the caller can tell a real upgrade
	# from a failed one (e.g. unavailable dependencies) instead of counting
	# every queued package as handled.
	keepcache=yes spk-add $pkg --forced
}

# Reserve of the last upgrade: the versions an upgrade replaces stay in
# $previous until the next upgrade, so reprise can roll a bad one back
# offline (the mirror only serves the newest version).
previous="${root}/var/cache/spk/previous"

# Keep the installed version of a package: the cached copy when there
# is one, else a repack of its files as they are on disk (files lost
# since, e.g. trimmed from the ISO, are skipped; changed configs kept).
reserve_pkg() {
	local pkg="$1" dir="$installed/$1" ver f tmp rc
	[ -f "$dir/receipt" ] && [ -f "$dir/files.list" ] || return 1
	ver=$(unset EXTRAVERSION; . "$dir/receipt"; echo "$VERSION$EXTRAVERSION")
	for f in "$CACHE_DIR/$pkg-$ver.tazpkg" "$CACHE_DIR/$pkg-$ver-any.tazpkg" \
		"$CACHE_DIR/$pkg-$ver-$SLITAZ_ARCH.tazpkg"; do
		[ -s "$f" ] && { cp "$f" "$previous/"; return; }
	done
	command -v lzma > /dev/null || return 1
	# Rebuilt from what is installed: with WANTED_LANGS the filtered
	# translations are not in it (mo.list lists them, files.list does
	# not). Still the state to roll back to, but say it is partial.
	[ -f "$dir/mo.list" ] && [ -n "$(awk 'FILENAME == ARGV[1] { h[$0]; next }
		!($0 in h) { print; exit }' "$dir/files.list" "$dir/mo.list")" ] &&
		partial=$((partial + 1))
	tmp=$(mktemp -d) || return 1
	for f in "$dir"/*; do
		case "${f##*/}" in
			volatile.cpio.gz|modifiers|mo.list) ;;
			*) cp "$f" "$tmp/" ;;
		esac
	done
	mkdir "$tmp/fs"
	# cpio -o skips (and warns about) a listed file that is gone, which
	# the old [ -e ] read loop filtered byte by byte.
	sed -n 's|^/||p' "$dir/files.list" | \
		(cd "${root:-/}" && cpio -o -H newc --quiet 2>/dev/null) | \
		(cd "$tmp/fs" && cpio -idm --quiet)
	# -a0 -d20: fast mode, 2.3x quicker than the default for ~13% more
	# bytes; this copy only lives until the next upgrade.
	(cd "$tmp" && find fs | cpio -o -H newc --quiet | \
		lzma e fs.cpio.lzma -si -a0 -d20 && \
		ls | grep -v '^fs$' | cpio -o -H newc --quiet) > "$previous/$pkg-$ver.tazpkg"
	rc=$?
	rm -rf "$tmp"
	[ "$rc" -eq 0 ] || rm -f "$previous/$pkg-$ver.tazpkg"
	return $rc
}

# Empty the reserve of the previous upgrade and fill it for this one.
reserve_previous() {
	local pkg n=0 partial=0
	mkdir -p "$previous" || return 1
	rm -f "$previous"/*.tazpkg
	for pkg in "$@"; do
		is_package_installed "$pkg" || continue
		reserve_pkg "$pkg" && n=$((n + 1))
	done
	gettext "Kept for an offline rollback:"; echo " $n ($previous)"
	[ "$partial" = 0 ] || {
		gettext "Warning: translations filtered, partial packages:"
		echo " $partial"; }
}

# reprise, when installed, snapshots the known good state and fetches
# the fixes database while the network works; after the upgrade it
# checks what the upgrade may have broken (quick, read-only, offline).
# --prepare also runs the quick checks (reprise 0.4+): about a minute on
# a full desktop, so say what we are waiting for.
reprise_prepare() {
	[ -z "$root" ] && [ -x /usr/bin/reprise ] || return 0
	gettext "Preparing reprise snapshot..."
	reprise --prepare
	status
}

# Sort the failing checks of a `reprise --check` output into TSV lines
# "kind<TAB>check<TAB>problem". When reprise compared with the state
# saved by --prepare (prepare.since), kind is new (check.X.new > 0, a
# problem this upgrade brought) or old (all there before). Without that
# comparison (older reprise, no --prepare) kind is any: we can't tell.
# A new problem shows reprise's text as logged (normalized, numbers as
# N, before reprise 0.4.7) and the count: check.X.info is only the first
# problem, maybe an old one.
# Keys come in any order (prepare.since is printed last).
reprise_problems() {
	echo "$1" | awk '
		{ k = $0; sub(/=.*/, "", k); v = substr($0, length(k) + 2) }
		k == "prepare.since" { since = 1; next }
		k ~ /^check\.[a-z0-9_]+$/ && v == "fail" {
			n = substr(k, 7); order[++c] = n; next }
		k ~ /^check\.[a-z0-9_]+\.info$/ {
			n = substr(k, 7); sub(/\.info$/, "", n); info[n] = v; next }
		k ~ /^check\.[a-z0-9_]+\.new$/ {
			n = substr(k, 7); sub(/\.new$/, "", n); new[n] = v; next }
		k ~ /^check\.[a-z0-9_]+\.new\.info$/ {
			n = substr(k, 7); sub(/\.new\.info$/, "", n); newinfo[n] = v }
		END {
			for (i = 1; i <= c; i++) {
				n = order[i]
				if (!since || !(n in new)) print "any\t" n "\t" info[n]
				else if (new[n] + 0 > 0)
					print "new\t" n " (" new[n] + 0 " new)\t" newinfo[n]
				else print "old\t" n "\t" info[n]
			}
		}'
}

# Print the problems of one kind as "  check: problem".
reprise_list() {
	echo "$2" | awk -F'\t' -v k="$1" '$1 == k { print "  " $2 ": " $3 }'
}

# Only problems this upgrade brought get the rollback hint: a broken
# package installed weeks ago is no reason to undo tonight's upgrade.
reprise_after() {
	local out probs
	[ -z "$root" ] && [ -x /usr/bin/reprise ] || return 0
	out=$(reprise --check 2>/dev/null)
	[ "$?" -eq 1 ] || return 0
	probs=$(reprise_problems "$out")
	if echo "$probs" | grep -q '^new'; then
		newline
		colorize 31 "$(gettext 'reprise found new problems since this upgrade:')"
		reprise_list new "$probs"
		gettext "To repair, rollback ready offline:"; echo " reprise --fix"
	fi
	if echo "$probs" | grep -q '^old'; then
		newline
		colorize 33 "$(gettext 'reprise: problems already present before this upgrade:')"
		reprise_list old "$probs"
		gettext "To repair:"; echo " reprise --fix"
	fi
	if echo "$probs" | grep -q '^any'; then
		newline
		colorize 31 "$(gettext 'reprise found problems:')"
		reprise_list any "$probs"
		gettext "To repair:"; echo " reprise --fix"
	fi
}

#
# Output mode + json helpers (kept self-contained, see spk-search).
#

output=human
[ "$raw" ] && output=raw
[ "$trame" ] && output=trame
# --trame implies --check: never mutate the system in machine-output mode.
[ "$trame" ] && check=yes

#
# --raw --add : stream the REAL system upgrade as flat TSV milestones for the
# TUI (state<TAB>step<TAB>pkg<TAB>msg, state = step|info|ok|error|done).
# Recharge the lists, scan installed packages, then upgrade each upgradable
# one via spk-add --forced --raw (its output goes to the log; we judge each
# from its own "done" line). Plain --raw without --add keeps the existing
# non-mutating upgradable report further down untouched.
#
# --raw --upgrade NAME... : the same run restricted to the named packages
# (front-ends upgrading a selection); takes precedence over --add. Never
# pass --add with it: libtaz turns any --word into a variable, so an
# older spk-up ignores --upgrade and, without --add, only reports on the
# names (per-package loop below) instead of upgrading the whole system.
#
if [ "$raw" ] && { [ "$add" ] || [ "$upgrade" ]; }; then
	check_root "$@"
	# Lock first, before any output or redirection: recharge_lists takes
	# it again (re-entrant), but there a held lock went to the log.
	raw_done='0 upgraded, 0 failed'
	spkup_lock
	mkdir -p "$logdir" 2>/dev/null
	if [ "$upgrade" ]; then
		date "+%F %T : spk-up $*" >> "$logdir/raw-up.log"
	else
		date "+%F %T : spk-up --raw --add" >> "$logdir/raw-up.log"
	fi
	printf 'step\trecharge\t-\trefreshing package lists\n'
	recharge_lists >>"$logdir/raw-up.log" 2>&1
	cd $installed
	: > $pkgsup; : > $pkgsup.tsv
	fresh_builds
	build_count=0; blocked_count=0
	for pkg in *
	do
		check_pkgup
	done
	# Plus orphaned DB entries (in installed.info, no receipt dir).
	if [ -f "$installed.info" ]; then
		while IFS='	' read pkg _rest; do
			[ -n "$pkg" ] && [ ! -d "$pkg" ] && check_pkgup
		done < "$installed.info"
	fi
	sed -i /^$/d $pkgsup
	r_ok=0; r_skip=0; r_fail=0
	if [ "$upgrade" ]; then
		# Counts of the run before the self-upgrade hop (see below).
		[ "$resume" ] && [ -n "$SPK_UP_COUNTS" ] && \
			read r_ok r_skip r_fail <<EOT
$SPK_UP_COUNTS
EOT
		up_select "$@"
		upnb=$(echo $todo | wc -w)
	else
		todo=$(cat $pkgsup)
		upnb=$(cat $pkgsup | wc -l)
	fi
	printf 'info\tplan\t-\t%s upgradable\n' "$upnb"

	if [ ! "$resume" ] && [ "$upnb" -gt 0 ]; then
		[ -x /usr/bin/reprise ] && [ -z "$root" ] && \
			printf 'step\tprepare\t-\treprise snapshot and checks\n'
		reprise_prepare >>"$logdir/raw-up.log" 2>&1
		printf 'step\treserve\t-\tkeeping the current versions for a rollback\n'
		reserve_previous $todo >>"$logdir/raw-up.log" 2>&1
	fi
	order=$(up_order $todo); gone=; up_done=
	for pkg in $order
	do
		[ -z "$pkg" ] && continue
		gone="$gone $pkg "
		printf 'step\tupgrade\t%s\tupgrading\n' "$pkg"
		mkdir -p "$logdir/$pkg"
		echo "$(date '+%Y-%m-%d %H:%M') : Upgrade (raw)" >> "$logdir/$pkg/up.log"
		# Capture spk-add's own --raw stream into the log; its final "done"
		# line tells us whether the upgrade landed (… , 0 failed).
		res=$(spk-add "$pkg" --forced --raw 2>>"$logdir/raw-up.log")
		printf '%s\n' "$res" >> "$logdir/raw-up.log"
		case "$res" in
			*", 0 failed")
				printf 'ok\tupgrade\t%s\tupgraded\n' "$pkg"
				r_ok=$((r_ok + 1)); up_done="$up_done $pkg"
				# Same single re-exec hand-over as the human flow: the
				# rest of the batch must run the freshly installed spk.
				if spk_hop "$pkg" "$upnb"; then
					if [ -z "$upgrade" ]; then
						printf 'info\tupgrade\tspk\tresuming with the new spk-up\n'
						spkup_unlock
						exec /usr/sbin/spk-up --raw --add --resume
					fi
					# A selection hands over the names still to do and
					# the counts.
					rest=
					for p in $order; do
						case "$gone" in *" $p "*) ;; *) rest="$rest $p" ;; esac
					done
					printf 'info\tupgrade\tspk\tresuming with the new spk-up\n'
					spkup_unlock
					export SPK_UP_COUNTS="$r_ok $r_skip $r_fail"
					exec /usr/sbin/spk-up --raw --upgrade --resume $rest
				fi ;;
			*)
				printf 'error\tupgrade\t%s\t%s\n' "$pkg" \
					"$(raw_add_why "$res")"
				r_fail=$((r_fail + 1)) ;;
		esac
	done
	if [ "$upgrade" ]; then
		# What was not selected (or failed) is still due: tazpkg-notify
		# reads this list. Only what was just upgraded leaves it.
		awk -v d=" $up_done " 'index(d, " " $0 " ") == 0' $pkgsup \
			> $pkgsup.$$ && mv -f $pkgsup.$$ $pkgsup
	else
		rm -f $pkgsup && touch $pkgsup
	fi
	if [ "$r_ok" -gt 0 ] && [ -z "$root" ] && [ -x /usr/bin/reprise ]; then
		r_out=$(reprise --check 2>/dev/null)
		if [ "$?" -eq 1 ]; then
			r_probs=$(reprise_problems "$r_out")
			if echo "$r_probs" | grep -q '^new'; then
				printf 'error\tcheck\t-\treprise: new problems since this upgrade, run reprise --fix\n'
			elif echo "$r_probs" | grep -q '^any'; then
				printf 'error\tcheck\t-\treprise found problems: run reprise --fix\n'
			else
				printf 'info\tcheck\t-\treprise: problems already present before this upgrade\n'
			fi
		fi
	fi
	done_msg="$r_ok upgraded"
	[ "$r_skip" -gt 0 ] && done_msg="$done_msg, $r_skip skipped"
	printf 'done\t-\t-\t%s, %d failed\n' "$done_msg" "$r_fail"
	exit 0
fi

# --upgrade is a front-end's streamed call: without --raw it would fall
# to the per-name report below and upgrade nothing, without a word.
if [ "$upgrade" ]; then
	gettext "The --upgrade option needs --raw"; newline
	exit 1
fi

# Render the buffered $pkgsup.tsv into the selected output format.
# Caller has already filled it via check_pkgup loops.
render_upgradable() {
	local f="$pkgsup.tsv"
	[ -f "$f" ] || : > "$f"
	case "$output" in
	raw)
		# upgradable<TAB>pkg<TAB>cur<TAB>new<TAB>status<TAB>repo<TAB>size
		# size: download size of the new package (blocked rows too),
		# from the lookup that sums total_size; empty when unknown.
		up_sizes "$f" | awk -F'\t' "$size_awk"'
			$1 != "" { printf "upgradable\t%s\t%s\t%s\t%s\t%s\t%s\n",
				$1, $2, $3, $4, $5,
				($6 ~ /[0-9]/ ? human(tobytes($6)) : "") }'
		# Trailing summary row: download size then required free space.
		local _b=$(upgrade_total_bytes) _p _u
		_p=${_b%% *}; _u=$((${_b##* } + _p))
		printf 'total_size\t%s\t%s\n' "$_p" "$(human_size $_p)"
		printf 'space_needed\t%s\t%s\n' "$_u" "$(human_size $_u)"
		;;
	trame)
		printf '{"command":"up","mode":"check","upgradable":['
		local first=yes nb=0 nblocked=0
		while IFS="	" read pkg cur new st repo; do
			[ -z "$pkg" ] && continue
			if [ "$st" = blocked ]; then
				nblocked=$((nblocked + 1))
				continue
			fi
			[ "$first" ] || printf ','
			first=
			printf '{"package":"%s","from":"%s","to":"%s","reason":"%s","mirror":"%s"}' \
				"$(json_escape "$pkg")" "$(json_escape "$cur")" \
				"$(json_escape "$new")" "$st" "$(json_escape "$repo")"
			nb=$((nb + 1))
		done < "$f"
		printf '],"blocked":['
		first=yes
		while IFS="	" read pkg cur new st repo; do
			[ "$st" = blocked ] || continue
			[ "$first" ] || printf ','
			first=
			printf '"%s"' "$(json_escape "$pkg")"
		done < "$f"
		local _b=$(upgrade_total_bytes) _p _u
		_p=${_b%% *}; _u=$((${_b##* } + _p))
		printf '],"count":%s,"blocked_count":%s,"total_size_bytes":%s,"total_size":"%s","space_needed_bytes":%s,"space_needed":"%s"}\n' \
			"$nb" "$nblocked" "$_p" "$(human_size $_p)" \
			"$_u" "$(human_size $_u)"
		;;
	esac
}

# awk functions for every size the tool prints, so the per-package
# sizes and the totals always agree. tobytes: a packages.info size
# (K/M/G 1024-based) in bytes, empty counts as 0. human: bytes as a
# 1024-based short size, like the receipt sizes.
size_awk='
	function tobytes(s,   n,u){
		n=s; u=s; gsub(/[0-9.]/,"",u); gsub(/[A-Za-z]/,"",n);
		if(n=="") return 0;
		if(u ~ /^[Gg]/) return n*1073741824;
		if(u ~ /^[Mm]/) return n*1048576;
		if(u ~ /^[Kk]/) return n*1024;
		return n;
	}
	function human(t){
		if(t>=1073741824) return sprintf("%.1fG", t/1073741824);
		if(t>=1048576) return sprintf("%.1fM", t/1048576);
		if(t>=1024) return sprintf("%.1fK", t/1024);
		return sprintf("%dB", t);
	}'

# Format a byte count as a human-readable size. Usage: human_size <bytes>
human_size() {
	echo "$1" | awk "$size_awk"'{ print human($1) }'
}

# Copy each line of file $1 with two TAB fields appended: the packed and
# unpacked sizes (packages.info field 7, as written there) of the package
# named in its first TAB field, empty when no list knows it. Extras are
# searched first, then official; the first non-empty size wins. One awk
# pass over the lists instead of one awk per package and list.
up_sizes() {
	local info lists=""
	[ -f "$1" ] || return 0
	for info in $extradb/*/packages.info $pkgsinfo; do
		[ -f "$info" ] && lists="$lists $info"
	done
	awk -F'\t' '
		FILENAME == ARGV[1] { line[++n] = $0; name[n] = $1; want[$1] = 1; next }
		($1 in want) && !($1 in sz) && $7 != "" { sz[$1] = $7 }
		END {
			for (i = 1; i <= n; i++) {
				split(sz[name[i]], s, " ")
				print line[i] "\t" s[1] "\t" s[2]
			}
		}' "$1" $lists
}

# Sum the sizes of every package queued for upgrade ($pkgsup, one name
# per line) and echo them as "<packed> <unpacked>" integer bytes; an
# unknown size counts as 0. Packed = download total; unpacked = free
# space the upgrade needs (what tazpkg reports). Used by every output
# mode so views always agree.
upgrade_total_bytes() {
	up_sizes "$pkgsup" | awk -F'\t' "$size_awk"'
		{ p += tobytes($2); u += tobytes($3) }
		END{ printf "%d %d\n", p, u }'
}

#
# Handle packages and --options
#

count=0

for arg in $@
do
	case "$arg" in
		*usage|*help) usage ;;
		--list)
			check_root "$@"
			if [ "$output" = human ]; then
				recharge_lists
				newline && exit 0
			fi
			# --raw (spk-gui "Refresh"): milestones only, the human
			# output (meters, separators) goes to the log. C locale so
			# the failure test below doesn't depend on translations.
			# Lock here, not in the $(...) below: a held lock failed in
			# that subshell and the run then said "refreshed".
			raw_done='lists not refreshed'
			spkup_lock
			printf 'step\trecharge\t-\trefreshing package lists\n'
			mkdir -p "$logdir"
			out=$(export LC_ALL=C LANGUAGE=C; recharge_lists 2>&1)
			{ date "+%F %T : spk-up --raw (recharge)"; echo "$out"; } \
				>> "$logdir/raw-up.log"
			case "$out" in
				*unreachable*|*malformed*)
					printf 'error\trecharge\t-\tmirror unreachable or bad lists, current lists kept\n'
					printf 'done\t-\t-\tlists not refreshed\n'
					exit 1 ;;
			esac
			printf 'done\t-\t-\tpackage lists refreshed\n'
			exit 0 ;;
		--*) continue ;;
		*)
			pkg="$arg"
			system=no
			check_root "$@"
			if [ "$count" = 0 ]; then
				up_headers
				# Individual query (`spk-up <pkg>`): up_type appends to
				# $pkgsup, and this path — unlike the system-wide scan — never
				# truncates it, so every run left a stale line in the
				# persistent packages.up that tazpkg-notify reads. Point
				# $pkgsup at a throwaway so the notify list stays untouched.
				pkgsup=$(mktemp 2>/dev/null || echo "${pkgsup}.q$$")
				: > "$pkgsup"; : > "$pkgsup.tsv"
			fi
			if is_package_installed $pkg; then
				count=$(($count +1))
				cd $installed
				source_receipt $pkg/receipt
				check=$(check_pkgup)
				if [ "$check" != "" ]; then
					echo "$check"
					# blocked.list applies here too — the system scan
					# skips blocked packages, this path upgraded anyway.
					if [ "$add" ] && ! grep -qxF "$pkg" "$blocked" 2>/dev/null; then
						spkup_lock
						install_up
					fi
				else
					echo -n "$pkg"
					echo -n $(indent 28 "$VERSION")
					echo -n $(colorize 32 $(indent 48 $(gettext "up-to-date")))
					check_pkgup
					repo_label
					indent 68 "$rlabel"
				fi
			fi ;;
	esac
done

# Skip system-wide upgrade if some packages was updated individually.
if [ "$system" ]; then
	# Drop the throwaway query list; the persistent packages.up is untouched.
	rm -f "$pkgsup" "$pkgsup.tsv"
	[ "$add" ] || newline
	exit 0
fi

#
# Check all mirrors list and upgrade system.
#

time=$(date +%s)
build_count=0
blocked_count=0

check_root "$@"
spkup_lock
# --check audits the current state of the lists, without recharging
# from the network. This keeps the audit non-destructive (a failed
# recharge can wipe the local DB) and makes the trame integration
# safe to call from automation / TazPanel.
# The --resume hop (new spk-up after spk upgraded itself) carries on the
# first run: lists just recharged, table, summary and downloads already
# shown. It rescans silently and goes on with the install lines.
[ -z "$check" ] && [ -z "$resume" ] && recharge_lists
[ "$resume" ] && [ "$output" = human ] && exec 3>&1 1>/dev/null
[ "$output" = human ] && up_headers
cd $installed
newline > $pkgsup
: > $pkgsup.tsv
fresh_builds

# Check all installed packages
for pkg in *
do
	check_pkgup
done
# Plus orphaned DB entries: recorded in installed.info but with no
# receipt dir, so the glob above can't see them (check_pkgup recovers
# them from installed.info).
if [ -f "$installed.info" ]; then
	while IFS='	' read pkg _rest; do
		[ -n "$pkg" ] && [ ! -d "$pkg" ] && check_pkgup
	done < "$installed.info"
fi
[ "$resume" ] && [ "$output" = human ] && exec 1>&3 3>&-

# Remove empty line and count
sed -i /^$/d $pkgsup
upnb=$(cat $pkgsup | wc -l)
pkgs=$(ls | wc -l)
time=$(($(date +%s) - $time))

# --check / raw / trame: render the structured upgrade list and exit
# BEFORE the human summary / install prompt would kick in.
if [ "$check" ] || [ "$output" != human ]; then
	render_upgradable
	exit 0
fi

if [ ! "$resume" ]; then
	if [ "$upnb" = 0 ] && [ "$blocked_count" = 0 ]; then
		gettext "System is up-to-date..."; newline
	fi
	separator
	echo -n "$pkgs "; gettext "installed packages scanned in"; echo " ${time}s"
	newline

	# Summary
	boldify $(gettext "Packages upgrade summary")
	separator
	up_bytes=$(upgrade_total_bytes)
	gettext "New version :"; colorize 32 " $(($upnb - $build_count))"
	gettext "New build   :"; colorize 34 " $build_count"
	gettext "Blocked     :"; colorize 31 " $blocked_count"
	gettext "Download    :"; colorize 34 " $(human_size ${up_bytes%% *})"
	# Cache copy + unpacked payload, same convention as tazpkg.
	gettext "Space needed:"; colorize 34 " $(human_size $((${up_bytes##* } + ${up_bytes%% *})))"
	separator
	newline
fi

# Pkgs to upgrade ? --add/--install skip the confirm and install all.
if [ "$upnb" -gt 0 ]; then
	if [ ! "$add" ] && [ ! "$install" ] && [ ! "$resume" ]; then
		# Read the answer directly instead of libtaz confirm(): its
		# im()/`tty -s` guard mis-detects the SliTaz chroot pty (fd 0 is a
		# real /dev/pts node yet `tty -s` returns false), so confirm()
		# silently skips the read and cancels every interactive upgrade.
		# A plain read works on that same terminal; EOF (piped/cron) still
		# falls through to the safe default of not upgrading. --add stays
		# the explicit non-interactive path.
		gettext "Do you wish to upgrade now"; printf ' [y/N] ? '
		read answer
		case "$answer" in
			y|Y|yes|Yes) ;;
			*) gettext "Upgrade cancelled"; echo -e "\n"; exit 0 ;;
		esac
	fi
	# Clean the cache so the prefetch lands fresh copies — except on a
	# --resume hop: the first run's verified downloads are still there
	# and phase 1 revalidates every cache hit against packages.md5.
	# Before the cache is cleaned: its copies of the installed versions
	# go to the reserve first. The --resume hop keeps the first run's.
	if [ ! "$resume" ]; then
		reprise_prepare
		reserve_previous $(cat $pkgsup)
		spk clean
	fi
	# Phase 1 — download every upgrade up front (nothing installed yet).
	# A network glitch here aborts before any package is swapped, so the
	# system never ends up half-upgraded. On --resume everything is in
	# the cache: keepcache keeps those hits silent.
	if [ "$resume" ]; then
		keepcache=yes
	else
		newline
		boldify $(gettext "Downloading packages")
		separator
	fi
	dl_ok=""; dl_fail=""
	for pkg in $(cat $pkgsup)
	do
		# Subshell contains check_download's hard `exit 1` on an md5
		# mismatch (e.g. a package whose file drifted from packages.md5
		# while the mirror was being regenerated). Without it, one bad
		# package would kill the whole batch before anything installs.
		# A failed download is skipped and reported; the rest proceed.
		if ( download_up "$pkg" ); then
			dl_ok="$dl_ok $pkg"
		else
			dl_fail="$dl_fail $pkg"
		fi
	done
	# New versions can pull dependencies that are not installed yet:
	# prefetch their closure too, or a network cut during phase 2 leaves
	# exactly the half-upgraded system phase 1 exists to prevent. They
	# are only cached here — spk-add installs them as deps in phase 2.
	dep_fail=""; queue=""; seen=""
	for pkg in $dl_ok; do
		queue="$queue $(awk -F'\t' -v p="$pkg" '$1==p{print $8; exit}' "$pkgsinfo")"
	done
	while [ -n "${queue# }" ]; do
		queue="${queue# }"
		dep="${queue%% *}"
		[ "$dep" = "$queue" ] && queue="" || queue="${queue#* }"
		case " $seen " in *" $dep "*) continue ;; esac
		seen="$seen $dep"
		equiv_of "$dep"; dep=$equiv
		is_package_installed "$dep" && continue
		case " $dl_ok $dl_fail " in *" $dep "*) continue ;; esac
		if ( download_up "$dep" ); then
			[ "$resume" ] || {
				gettext "Prefetched new dependency:"; colorize 32 " $dep"; }
			queue="$queue $(awk -F'\t' -v p="$dep" '$1==p{print $8; exit}' "$pkgsinfo")"
		else
			dep_fail="$dep_fail $dep"
		fi
	done
	# Phase 2 — install from the cache. spk goes first: a newer spk may
	# understand package formats/features the rest of the batch needs (same
	# reason tazpkg upgrades itself before everything else). install_up
	# passes keepcache so --forced reuses the prefetched, verified files.
	if [ ! "$resume" ]; then
		newline
		boldify $(gettext "Installing upgrades")
		separator
	fi
	up_ok=0; up_fail=0; failed=""
	# Only install packages that actually downloaded (dl_ok). spk goes
	# first; the case guard skips spk if it wasn't in the downloaded set.
	for pkg in $(up_order $dl_ok)
	do
		case " $dl_ok " in *" $pkg "*) ;; *) continue ;; esac
		if install_up; then
			up_ok=$(($up_ok + 1))
			# spk just replaced itself: hand over to the NEW spk-up for
			# the rest of the batch — the running script mixing its old
			# logic with freshly installed spk-add is exactly what a
			# toolchain upgrade cannot afford. Single hop (--resume
			# never re-execs); lock released, the new run re-takes it.
			# Not under --root=, see spk_hop.
			if spk_hop "$pkg" "$(echo $dl_ok | wc -w)"; then
				gettext "spk upgraded, resuming with the new spk-up"
				newline
				spkup_unlock
				exec /usr/sbin/spk-up --add --resume
			fi
		else
			up_fail=$(($up_fail + 1))
			failed="$failed $pkg"
		fi
	done
	# List is generated each time and must be cleaned so
	# tazpkg-notify doesn't find upgrades anymore.
	rm $pkgsup && touch $pkgsup
	newline
	gettext "Handled upgrades:"; colorize 32 " $up_ok"
	if [ "$up_fail" -gt 0 ]; then
		gettext "Failed upgrades:"; colorize 31 " $up_fail"
		gettext "Not upgraded:"; colorize 31 "$failed"
	fi
	# Packages that never made it past download (bad md5, missing on
	# mirror): reported here, not silently dropped. The upgrade list is
	# regenerated fresh on the next run, so they reappear then.
	if [ -n "$dl_fail" ]; then
		gettext "Failed downloads:"; colorize 31 "$dl_fail"
	fi
	if [ -n "$dep_fail" ]; then
		gettext "Failed dependency prefetch:"; colorize 31 "$dep_fail"
	fi
	[ "$up_ok" -gt 0 ] && reprise_after
	newline
fi

exit 0
