#!/bin/sh -efu

arepo_arch="$1"; shift
[ -s plan/arepo-add-$arepo_arch ] || exit 0
[ -s arepo/$arepo_arch/todo.list -o -s plan/arepo-copy-$arepo_arch ] || exit 0
native_arch="${arepo_arch%%-*}"
arch="${arepo_arch##*-}"

. gb-sh-functions

if [ -s plan/arepo-copy-$arepo_arch -a \
   ! -s arepo/$arepo_arch/todo.list -a \
   ! -d arepo/$arepo_arch/rpms ]; then
	(umask 002; mkdir arepo/$arepo_arch/rpms) || false
	# copy what needs to be copied
	while read -r N B P; do
		ln "$P" arepo/$arepo_arch/rpms/
	done < plan/arepo-copy-$arepo_arch
fi

[ -s arepo/$arepo_arch/todo.list ] || exit 0

fail()
{
	stamp_echo >&2 "[$arepo_arch] $*"
	exit 1
}

# remove copied packages
while read -r N B P; do
	rm -f "arepo/$arepo_arch/rpms/$B"
done < plan/arepo-copy-$arepo_arch

(umask 002; mkdir -p arepo/$arepo_arch/rpms) || false

. shell-quote
qvv() { quote_shell "$1=\"$(quote_shell "$2")\""; }

# prepare remote node for arepo build
# note that now we use $remote_host:gb-repo, not $GB_REPO_DIR
cp -f "$GB_REPO_SOURCES" "$GB_HOME/remote/apt/sources-$GB_REPO_NAME.list"
gb-x-rsync-loop -a "$GB_HOME/remote/" "$remote_host:"
remote_home=$(ssh "$remote_host" pwd)
gb-x-ssh "$remote_host" ./gb-remote-plant \
	"\"$(quote_shell "$(cat task/owner)")\"" \
	"\"$(quote_shell "$GB_REPO_NAME")\"" \
	"\"$(quote_shell "$remote_home/apt/sources-$GB_REPO_NAME.list")\"" \
	"\"$(quote_shell "$GB_REPO_FLAVOUR")\"" \
	"\"$(quote_shell "$GB_NPROCS")\"" \
	"\"$(quote_shell "$remote_home/gb-repo")\"" \
	"\"$(quote_shell "$arch")\"" \
	"\"$(quote_shell "$GB_REPO_FLAVOUR")\"" \
	"\"$(quote_shell "$GB_APT_CACHE_LIMIT")\"" \
	"\"$(qvv rpmargs "$GB_REPO_RPMARGS")\"" \
	cache_dir=cache.arepo

# copy the temporary repo generated by gb-task-gen-testrepo
gb-x-rsync-loop -a \
	--exclude '*list.*.bz2' \
	--exclude 'srclist.*' \
	--exclude 'contents_index' \
	"$GB_TMP_REPO_DIR"/{$arch,$native_arch,noarch} "$remote_host:gb-repo/"

# copy input lists
gb-x-rsync-loop -q -- arepo/$arepo_arch/{pkg,todo}.list "$remote_host:tmp/"

# run the build on remote side
gb-x-ssh "$remote_host" ./gb-remote-arepo-build \
	"\"$(quote_shell "$GB_REPO_FLAVOUR")\"" \
	"\"$(quote_shell "$arch")\"" || {
		rsync -q -- "$remote_host:build/arepo.log" arepo/$arepo_arch/ ||:
		fail 'remote build FAILED'
	}

# copy build results from remote side
gb-x-rsync-loop -qrlt -- "$remote_host:build/" arepo/$arepo_arch/

# purge temporary repo
gb-x-rsync-loop -a --delete /var/empty/ "$remote_host:gb-repo/"

# check the result
. gb-sh-tmpdir
find arepo/$arepo_arch/rpms -mindepth 1 -maxdepth 1 -type f -name '*.rpm' -printf '%f\n' > "$tmpdir"/out
sort -o "$tmpdir"/out{,}
join -v1 -o1.3 plan/arepo-add-$arepo_arch plan/arepo-copy-$arepo_arch > "$tmpdir"/in
sort -o "$tmpdir"/in{,}
cmp -s "$tmpdir"/in "$tmpdir"/out ||
	fail 'built packages do not match expectations'

sisyphus_check --no-check=arepo,gpg,python arepo/$arepo_arch/rpms ||
	fail 'sisyphus_check FAILED'

# sign the result
if ! out=$(find arepo/$arepo_arch/rpms -mindepth 1 -maxdepth 1 -type f -name '*.rpm' -execdir gb-x-rpmaddsign '{}' '+'); then
	printf '%s\n' "$out"
	fail 'rpmsign FAILED'
fi

# copy what needs to be copied
while read -r N B P; do
	ln -v "$P" arepo/$arepo_arch/rpms/ >> arepo/$arepo_arch/arepo.log 2>&1
done < plan/arepo-copy-$arepo_arch

# mark the job as done
cat plan/arepo-add-$arepo_arch > arepo/$arepo_arch/done.list

stamp_echo >&2 "[$arepo_arch] arepo build OK"
