#!/bin/sh
# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
# relang - TazPkg module
# Apply WANTED_LANGS to the translations of installed packages (bug 1064)
#
# Candidates are the packages with a mo.list. Only removals: done in
# place, no archive needed (offline right after an install). A .mo to
# add: forced reinstall without deps, from the cache, else the mirror.
# Same on-disk result as spk relang.


# Connect function libraries
. /lib/libtaz.sh

# Get TazPkg working environment
. @@MODULES@@/getenv


tmp=$(mktemp -d)
npkg=0; nadd=0; ndel=0; failed=''
opts='--forced --nodeps --quiet --relang'
[ -n "$root" ] && opts="$opts --root=$root"

title 'Translations: %s' "${WANTED_LANGS:-all}"

# Lines of $2 not in $1
only_in() { awk -v l="$1" 'FILENAME == l { x[$0]; next } !($0 in x)' "$1" "$2"; }

for ml in "$INSTALLED"/*/mo.list; do
	[ -f "$ml" ] || continue
	d="${ml%/mo.list}"; pkg="${d##*/}"
	mo_filter keep "$ml" | sort > "$tmp/want"
	mo_list "$d/files.list" | sort > "$tmp/have"
	cmp -s "$tmp/want" "$tmp/have" && continue

	only_in "$tmp/have" "$tmp/want" > "$tmp/add"
	only_in "$tmp/want" "$tmp/have" > "$tmp/del"

	if [ ! -s "$tmp/add" ]; then
		# Removals only: in place. A path another package also lists stays
		# on disk, it just leaves this package's lists.
		action 'Removing translations of "%s"...' "$pkg"
		awk -v del="$tmp/del" -v me="$d/files.list" '
			FILENAME == del { x[$0]; next }
			FILENAME != me && ($0 in x) { delete x[$0] }
			END { for (f in x) print f }' "$tmp/del" "$INSTALLED"/*/files.list \
			> "$tmp/rm"
		mo_delete "$tmp/rm" "$root"
		mo_unlist "$tmp/del" "$d/files.list" "$d/$CHECKSUM"
		status
	else
		# Additions: reinstall THE INSTALLED VERSION, from the cache, else
		# from the mirror when it has that version. Never an upgrade,
		# blocked package or not: another version is a failure.
		ver=$(unset EXTRAVERSION; . "$d/receipt"; echo "$VERSION$EXTRAVERSION")
		nv="$pkg-$ver"; mver=""
		pkgfile=$(find "$CACHE_DIR" \( -name "$nv.tazpkg" -o \
			-name "$nv-$SLITAZ_ARCH.tazpkg" -o -name "$nv-any.tazpkg" \) \
			2>/dev/null | head -n1)
		if [ -z "$pkgfile" ]; then
			# Mirror version, first repository listing it (as get_pkg)
			for rep in $PRIORITY; do
				pi="$rep/packages-$SLITAZ_ARCH.info"
				[ -e "$pi" ] || pi="$rep/packages.info"
				mver=$(awk -F$'\t' -v p="$pkg" '$1 == p { print $2; exit }' "$pi" 2>/dev/null)
				[ -n "$mver" ] && break
			done
			# get module directly: get-install refuses a blocked package
			[ "$mver" = "$ver" ] &&
				pkgfile=$(@@MODULES@@/get "$pkg" ${root:+--root=$root} 2>/dev/null)
		fi
		[ -f "$pkgfile" ] && tazpkg install "$pkgfile" $opts
		mo_list "$d/files.list" | sort > "$tmp/have2"
		if ! cmp -s "$tmp/want" "$tmp/have2"; then
			echo "$pkg: version $ver not in cache or mirror (mirror: ${mver:-none})"
			failed="$failed $pkg"
			continue
		fi
	fi
	npkg=$((npkg + 1))
	nadd=$((nadd + $(wc -l < "$tmp/add")))
	ndel=$((ndel + $(wc -l < "$tmp/del")))
done
rm -rf "$tmp"

footer "$(_ '%s packages reapplied, %s .mo added, %s .mo removed' "$npkg" "$nadd" "$ndel")"

if [ -n "$failed" ]; then
	_ 'Failed:%s' "$failed"
	exit 1
fi
