#!/bin/sh -efu

. gb-sh-functions

gb-y-task-commit-packages "$GB_REPO_DIR" > logs/commit.log
stamp_echo >&2 'packages update OK'

# Update lists.
install -pm644 -- plan/next.src "$GB_REPO_DIR/files/list/src.list"
install -pm644 -- plan/next.bin "$GB_REPO_DIR/files/list/bin.list"

# Reuse base directories from our prepared repo.
updated=
for arch in $GB_ARCH noarch; do
	old_base="$GB_REPO_DIR/$arch/base"
	new_base="build/repo/$arch/base.next"
	cmp_file=pkglist.$GB_REPO_FLAVOUR.xz
	# If $cmp_file has not changed, skip the update for this $arch.
	if cmp -s "$old_base/$cmp_file" "$new_base/$cmp_file"; then
		continue
	fi
	# NB: no --delete here, contents_index might be optimized out.
	rsync -rlt --no-times --no-perms --no-owner --no-group "$new_base"/ "$old_base"/
	updated="$updated $arch"
done

if [ -f "task/regen" ]; then
	stamp_echo >&2 'regenerate bases'
	updated="$updated bases"
	gb-y-repo-regen-basedir --bloat --force "$GB_REPO_DIR"
fi

stamp_echo >&2 "[${updated## }] update OK"
